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 | 05ccb4d8f1bbe3fb64e117136b4a89bcfb0b0b33 (patch) | |
tree | 929e514de85eb82a5acb96ecffc6e6d2d95f878f /BackEnd/Timeline/Models/Validation/Validator.cs | |
parent | 986c6f2e3b858d6332eba0b42acc6861cd4d0227 (diff) | |
download | timeline-05ccb4d8f1bbe3fb64e117136b4a89bcfb0b0b33.tar.gz timeline-05ccb4d8f1bbe3fb64e117136b4a89bcfb0b0b33.tar.bz2 timeline-05ccb4d8f1bbe3fb64e117136b4a89bcfb0b0b33.zip |
Split front and back end.
Diffstat (limited to 'BackEnd/Timeline/Models/Validation/Validator.cs')
-rw-r--r-- | BackEnd/Timeline/Models/Validation/Validator.cs | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/BackEnd/Timeline/Models/Validation/Validator.cs b/BackEnd/Timeline/Models/Validation/Validator.cs new file mode 100644 index 00000000..aef7891c --- /dev/null +++ b/BackEnd/Timeline/Models/Validation/Validator.cs @@ -0,0 +1,127 @@ +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);
+ }
+ }
+ }
+}
|