aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Models
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-08-17 19:25:08 +0800
committer杨宇千 <crupest@outlook.com>2019-08-17 19:25:08 +0800
commitec74a3c491d361bbaf9354b7f17be750b7b8823c (patch)
treee773849e410c494e1a8be157547f71bf7d187c68 /Timeline/Models
parent7bbd66270597c7f6f07f1ab5ec6c45b3dcfa388e (diff)
downloadtimeline-ec74a3c491d361bbaf9354b7f17be750b7b8823c.tar.gz
timeline-ec74a3c491d361bbaf9354b7f17be750b7b8823c.tar.bz2
timeline-ec74a3c491d361bbaf9354b7f17be750b7b8823c.zip
Add validation.
Diffstat (limited to 'Timeline/Models')
-rw-r--r--Timeline/Models/Validation/UsernameValidator.cs45
-rw-r--r--Timeline/Models/Validation/Validator.cs107
2 files changed, 152 insertions, 0 deletions
diff --git a/Timeline/Models/Validation/UsernameValidator.cs b/Timeline/Models/Validation/UsernameValidator.cs
new file mode 100644
index 00000000..e4891400
--- /dev/null
+++ b/Timeline/Models/Validation/UsernameValidator.cs
@@ -0,0 +1,45 @@
+using System.Linq;
+using System.Text.RegularExpressions;
+
+namespace Timeline.Models.Validation
+{
+ public class UsernameValidator : Validator<string>
+ {
+ public const int MaxLength = 26;
+ public const string RegexPattern = @"^[a-zA-Z0-9_][a-zA-Z0-9-_]*$";
+
+ private readonly Regex _regex = new Regex(RegexPattern);
+
+ protected override bool DoValidate(string value, out string message)
+ {
+ if (value.Length == 0)
+ {
+ message = "An empty string is not permitted.";
+ return false;
+ }
+
+ if (value.Length > 26)
+ {
+ message = $"Too long, more than 26 characters is not premitted, found {value.Length}.";
+ return false;
+ }
+
+ foreach ((char c, int i) in value.Select((c, i) => (c, i)))
+ if (char.IsWhiteSpace(c))
+ {
+ message = $"A whitespace is found at {i} . Whitespace is not permited.";
+ return false;
+ }
+
+ var match = _regex.Match(value);
+ if (!match.Success)
+ {
+ message = "Regex match failed.";
+ return false;
+ }
+
+ message = ValidationConstants.SuccessMessage;
+ return true;
+ }
+ }
+}
diff --git a/Timeline/Models/Validation/Validator.cs b/Timeline/Models/Validation/Validator.cs
new file mode 100644
index 00000000..a1acbed9
--- /dev/null
+++ b/Timeline/Models/Validation/Validator.cs
@@ -0,0 +1,107 @@
+using System;
+using System.ComponentModel.DataAnnotations;
+
+namespace Timeline.Models.Validation
+{
+ /// <summary>
+ /// A validator to validate value.
+ /// See <see cref="Validate(object, out string)"/>.
+ /// </summary>
+ public interface IValidator
+ {
+ /// <summary>
+ /// Validate given value.
+ /// </summary>
+ /// <param name="value">The value to validate.</param>
+ /// <param name="message">The validation message.</param>
+ /// <returns>True if validation passed. Otherwise false.</returns>
+ bool Validate(object value, out string message);
+ }
+
+ public static class ValidationConstants
+ {
+ public const string SuccessMessage = "Validation succeeded.";
+ }
+
+ /// <summary>
+ /// Convenient base class for validator.
+ /// </summary>
+ /// <typeparam name="T">The type of accepted value.</typeparam>
+ /// <remarks>
+ /// Subclass should override <see cref="DoValidate(T, out string)"/> to do the real validation.
+ /// This class will check the nullity and type of value. If value is null or not of type <typeparamref name="T"/>
+ /// it will return false and not call <see cref="DoValidate(T, out string)"/>.
+ ///
+ /// If you want some other behaviours, write the validator from scratch.
+ /// </remarks>
+ public abstract class Validator<T> : IValidator
+ {
+ public bool Validate(object value, out string message)
+ {
+ if (value == null)
+ {
+ message = "Value is null.";
+ return false;
+ }
+
+ if (value is T v)
+ {
+
+ return DoValidate(v, out message);
+ }
+ else
+ {
+ message = $"Value is not of type {typeof(T).Name}";
+ return false;
+ }
+ }
+
+ protected abstract bool DoValidate(T value, out string message);
+ }
+
+ [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("Given type is not assignable to IValidator.", nameof(validatorType));
+
+ try
+ {
+ _validator = Activator.CreateInstance(validatorType) as IValidator;
+ }
+ catch (Exception e)
+ {
+ throw new ArgumentException("Failed to create a validator instance from default constructor. See inner exception.", e);
+ }
+ }
+
+ protected override ValidationResult IsValid(object value, ValidationContext validationContext)
+ {
+ if (_validator.Validate(value, out var message))
+ return ValidationResult.Success;
+ else
+ return new ValidationResult(string.Format("Field {0} is bad. {1}", validationContext.DisplayName, message));
+ }
+ }
+}