diff options
author | 杨宇千 <crupest@outlook.com> | 2019-10-22 20:49:52 +0800 |
---|---|---|
committer | 杨宇千 <crupest@outlook.com> | 2019-10-22 20:49:52 +0800 |
commit | 9c9762b4ecbd816be98ee0dd606fe15cc253b415 (patch) | |
tree | cd8dea96c88e9784ceeb7342f486160ceadc6a58 /Timeline/Models/Validation/UsernameValidator.cs | |
parent | c442b7ad597f430b186dd8019de70332b574c4ba (diff) | |
download | timeline-9c9762b4ecbd816be98ee0dd606fe15cc253b415.tar.gz timeline-9c9762b4ecbd816be98ee0dd606fe15cc253b415.tar.bz2 timeline-9c9762b4ecbd816be98ee0dd606fe15cc253b415.zip |
...
Diffstat (limited to 'Timeline/Models/Validation/UsernameValidator.cs')
-rw-r--r-- | Timeline/Models/Validation/UsernameValidator.cs | 37 |
1 files changed, 15 insertions, 22 deletions
diff --git a/Timeline/Models/Validation/UsernameValidator.cs b/Timeline/Models/Validation/UsernameValidator.cs index ecc3b5b3..65d4da71 100644 --- a/Timeline/Models/Validation/UsernameValidator.cs +++ b/Timeline/Models/Validation/UsernameValidator.cs @@ -1,46 +1,39 @@ -using Microsoft.Extensions.Localization;
-using System.Linq;
-using System.Text.RegularExpressions;
+using System.Linq;
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, IStringLocalizerFactory localizerFactory, out string message)
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "Already checked in base class.")]
+ protected override (bool, ValidationMessageGenerator) DoValidate(string value)
{
if (value.Length == 0)
{
- message = "An empty string is not permitted.";
- return false;
+ return (false, factory =>
+ factory?.Create(typeof(UsernameValidator))?["ValidationMessageEmptyString"]
+ ?? Resources.Models.Validation.UsernameValidator.InvariantValidationMessageEmptyString);
}
if (value.Length > 26)
{
- message = $"Too long, more than 26 characters is not premitted, found {value.Length}.";
- return false;
+ return (false, factory =>
+ factory?.Create(typeof(UsernameValidator))?["ValidationMessageTooLong"]
+ ?? Resources.Models.Validation.UsernameValidator.InvariantValidationMessageTooLong);
}
foreach ((char c, int i) in value.Select((c, i) => (c, i)))
- if (char.IsWhiteSpace(c))
+ {
+ if (!(char.IsLetterOrDigit(c) || c == '-' || c == '_'))
{
- message = $"A whitespace is found at {i} . Whitespace is not permited.";
- return false;
+ return (false, factory =>
+ factory?.Create(typeof(UsernameValidator))?["ValidationMessageInvalidChar"]
+ ?? Resources.Models.Validation.UsernameValidator.InvariantValidationMessageInvalidChar);
}
-
- var match = _regex.Match(value);
- if (!match.Success)
- {
- message = "Regex match failed.";
- return false;
}
- message = ValidationConstants.SuccessMessage;
- return true;
+ return (true, SuccessMessageGenerator);
}
}
}
|