diff options
author | crupest <crupest@outlook.com> | 2020-10-27 19:21:35 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-10-27 19:21:35 +0800 |
commit | ac769e656b122ff569c3f1534701b71e00fed586 (patch) | |
tree | 72966645ff1e25139d3995262e1c4349f2c14733 /Timeline/Models/Validation | |
parent | 14e5848c23c643cea9b5d709770747d98c3d75e2 (diff) | |
download | timeline-ac769e656b122ff569c3f1534701b71e00fed586.tar.gz timeline-ac769e656b122ff569c3f1534701b71e00fed586.tar.bz2 timeline-ac769e656b122ff569c3f1534701b71e00fed586.zip |
Split front and back end.
Diffstat (limited to 'Timeline/Models/Validation')
-rw-r--r-- | Timeline/Models/Validation/GeneralTimelineNameValidator.cs | 33 | ||||
-rw-r--r-- | Timeline/Models/Validation/NameValidator.cs | 42 | ||||
-rw-r--r-- | Timeline/Models/Validation/NicknameValidator.cs | 25 | ||||
-rw-r--r-- | Timeline/Models/Validation/TimelineNameValidator.cs | 19 | ||||
-rw-r--r-- | Timeline/Models/Validation/UsernameValidator.cs | 19 | ||||
-rw-r--r-- | Timeline/Models/Validation/Validator.cs | 127 |
6 files changed, 0 insertions, 265 deletions
diff --git a/Timeline/Models/Validation/GeneralTimelineNameValidator.cs b/Timeline/Models/Validation/GeneralTimelineNameValidator.cs deleted file mode 100644 index e1c96fbd..00000000 --- a/Timeline/Models/Validation/GeneralTimelineNameValidator.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System;
-
-namespace Timeline.Models.Validation
-{
- public class GeneralTimelineNameValidator : Validator<string>
- {
- private readonly UsernameValidator _usernameValidator = new UsernameValidator();
- private readonly TimelineNameValidator _timelineNameValidator = new TimelineNameValidator();
-
- protected override (bool, string) DoValidate(string value)
- {
- if (value.StartsWith('@'))
- {
- return _usernameValidator.Validate(value.Substring(1));
- }
- else
- {
- return _timelineNameValidator.Validate(value);
- }
- }
- }
-
- [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter,
- AllowMultiple = false)]
- public class GeneralTimelineNameAttribute : ValidateWithAttribute
- {
- public GeneralTimelineNameAttribute()
- : base(typeof(GeneralTimelineNameValidator))
- {
-
- }
- }
-}
diff --git a/Timeline/Models/Validation/NameValidator.cs b/Timeline/Models/Validation/NameValidator.cs deleted file mode 100644 index b74c40b7..00000000 --- a/Timeline/Models/Validation/NameValidator.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Linq;
-using System.Text.RegularExpressions;
-using static Timeline.Resources.Models.Validation.NameValidator;
-
-namespace Timeline.Models.Validation
-{
- public class NameValidator : Validator<string>
- {
- private static Regex UniqueIdRegex { get; } = new Regex(@"^[a-zA-Z0-9]{32}$");
-
- public const int MaxLength = 26;
-
- protected override (bool, string) DoValidate(string value)
- {
- if (value.Length == 0)
- {
- return (false, MessageEmptyString);
- }
-
- if (value.Length > MaxLength)
- {
- return (false, MessageTooLong);
- }
-
- foreach ((char c, int i) in value.Select((c, i) => (c, i)))
- {
- if (!(char.IsLetterOrDigit(c) || c == '-' || c == '_'))
- {
- return (false, MessageInvalidChar);
- }
- }
-
- // Currently name can't be longer than 26. So this is not needed. But reserve it for future use.
- if (UniqueIdRegex.IsMatch(value))
- {
- return (false, MessageUnqiueId);
- }
-
- return (true, GetSuccessMessage());
- }
- }
-}
diff --git a/Timeline/Models/Validation/NicknameValidator.cs b/Timeline/Models/Validation/NicknameValidator.cs deleted file mode 100644 index 1d6ab163..00000000 --- a/Timeline/Models/Validation/NicknameValidator.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System;
-using static Timeline.Resources.Models.Validation.NicknameValidator;
-
-namespace Timeline.Models.Validation
-{
- public class NicknameValidator : Validator<string>
- {
- protected override (bool, string) DoValidate(string value)
- {
- if (value.Length > 25)
- return (false, MessageTooLong);
-
- return (true, GetSuccessMessage());
- }
- }
-
- [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
- public class NicknameAttribute : ValidateWithAttribute
- {
- public NicknameAttribute() : base(typeof(NicknameValidator))
- {
-
- }
- }
-}
diff --git a/Timeline/Models/Validation/TimelineNameValidator.cs b/Timeline/Models/Validation/TimelineNameValidator.cs deleted file mode 100644 index f1ab54e8..00000000 --- a/Timeline/Models/Validation/TimelineNameValidator.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System;
-
-namespace Timeline.Models.Validation
-{
- public class TimelineNameValidator : NameValidator
- {
- }
-
- [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter,
- AllowMultiple = false)]
- public class TimelineNameAttribute : ValidateWithAttribute
- {
- public TimelineNameAttribute()
- : base(typeof(TimelineNameValidator))
- {
-
- }
- }
-}
diff --git a/Timeline/Models/Validation/UsernameValidator.cs b/Timeline/Models/Validation/UsernameValidator.cs deleted file mode 100644 index 87bbf85f..00000000 --- a/Timeline/Models/Validation/UsernameValidator.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System;
-
-namespace Timeline.Models.Validation
-{
- public class UsernameValidator : NameValidator
- {
- }
-
- [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter,
- AllowMultiple = false)]
- public class UsernameAttribute : ValidateWithAttribute
- {
- public UsernameAttribute()
- : base(typeof(UsernameValidator))
- {
-
- }
- }
-}
diff --git a/Timeline/Models/Validation/Validator.cs b/Timeline/Models/Validation/Validator.cs deleted file mode 100644 index aef7891c..00000000 --- a/Timeline/Models/Validation/Validator.cs +++ /dev/null @@ -1,127 +0,0 @@ -using System;
-using System.ComponentModel.DataAnnotations;
-using static Timeline.Resources.Models.Validation.Validator;
-
-namespace Timeline.Models.Validation
-{
- /// <summary>
- /// A validator to validate value.
- /// </summary>
- public interface IValidator
- {
- /// <summary>
- /// Validate given value.
- /// </summary>
- /// <param name="value">The value to validate.</param>
- /// <returns>Validation success or not and message.</returns>
- (bool, string) Validate(object? value);
- }
-
- public static class ValidatorExtensions
- {
- public static bool Validate(this IValidator validator, object? value, out string message)
- {
- if (validator == null)
- throw new ArgumentNullException(nameof(validator));
-
- var (r, m) = validator.Validate(value);
- message = m;
- return r;
- }
- }
-
- /// <summary>
- /// Convenient base class for validator.
- /// </summary>
- /// <typeparam name="T">The type of accepted value.</typeparam>
- /// <remarks>
- /// Subclass should override <see cref="DoValidate(T)"/> to do the real validation.
- /// This class will check the nullity and type of value.
- /// If value is null, it will pass or fail depending on <see cref="PermitNull"/>.
- /// If value is not null and not of type <typeparamref name="T"/>
- /// it will fail and not call <see cref="DoValidate(T)"/>.
- ///
- /// <see cref="PermitNull"/> is true by default.
- ///
- /// If you want some other behaviours, write the validator from scratch.
- /// </remarks>
- public abstract class Validator<T> : IValidator
- {
- protected bool PermitNull { get; set; } = true;
-
- public (bool, string) Validate(object? value)
- {
- if (value == null)
- {
- if (PermitNull)
- return (true, GetSuccessMessage());
- else
- return (false, ValidatorMessageNull);
- }
-
- if (value is T v)
- {
- return DoValidate(v);
- }
- else
- {
- return (false, ValidatorMessageBadType);
- }
- }
-
- protected static string GetSuccessMessage() => ValidatorMessageSuccess;
-
- protected abstract (bool, string) DoValidate(T value);
- }
-
- [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter,
- AllowMultiple = false)]
- public class ValidateWithAttribute : ValidationAttribute
- {
- private readonly IValidator _validator;
-
- /// <summary>
- /// Create with a given validator.
- /// </summary>
- /// <param name="validator">The validator used to validate.</param>
- public ValidateWithAttribute(IValidator validator)
- {
- _validator = validator ?? throw new ArgumentNullException(nameof(validator));
- }
-
- /// <summary>
- /// Create the validator with default constructor.
- /// </summary>
- /// <param name="validatorType">The type of the validator.</param>
- public ValidateWithAttribute(Type validatorType)
- {
- if (validatorType == null)
- throw new ArgumentNullException(nameof(validatorType));
-
- if (!typeof(IValidator).IsAssignableFrom(validatorType))
- throw new ArgumentException(ValidateWithAttributeExceptionNotValidator, nameof(validatorType));
-
- try
- {
- _validator = (Activator.CreateInstance(validatorType) as IValidator)!;
- }
- catch (Exception e)
- {
- throw new ArgumentException(ValidateWithAttributeExceptionCreateFail, e);
- }
- }
-
- protected override ValidationResult IsValid(object value, ValidationContext validationContext)
- {
- var (result, message) = _validator.Validate(value);
- if (result)
- {
- return ValidationResult.Success;
- }
- else
- {
- return new ValidationResult(message);
- }
- }
- }
-}
|