aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Models/Validation/UsernameValidator.cs
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-08-17 20:43:24 +0800
committerGitHub <noreply@github.com>2019-08-17 20:43:24 +0800
commit2d672de8ea109c923d5a8697e44d5cbbf976ead2 (patch)
treececc5b93150f7b1e3be792de5c058433b5e40982 /Timeline/Models/Validation/UsernameValidator.cs
parent7bbd66270597c7f6f07f1ab5ec6c45b3dcfa388e (diff)
parent67f42c40c3d4108620119e163a67f2686dd4a6f2 (diff)
downloadtimeline-2d672de8ea109c923d5a8697e44d5cbbf976ead2.tar.gz
timeline-2d672de8ea109c923d5a8697e44d5cbbf976ead2.tar.bz2
timeline-2d672de8ea109c923d5a8697e44d5cbbf976ead2.zip
Merge pull request #42 from crupest/change-username
Add change username api.
Diffstat (limited to 'Timeline/Models/Validation/UsernameValidator.cs')
-rw-r--r--Timeline/Models/Validation/UsernameValidator.cs45
1 files changed, 45 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;
+ }
+ }
+}