aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Models
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-08-21 23:55:42 +0800
committer杨宇千 <crupest@outlook.com>2019-08-21 23:55:42 +0800
commite7714754d8f59bb8dc29aeb6340380e93310175b (patch)
tree7797613e18dd50665ac50881541f645413dbca97 /Timeline/Models
parentdbc8d78dc0f21f423a0fe2db9dd782da43b5b468 (diff)
downloadtimeline-e7714754d8f59bb8dc29aeb6340380e93310175b.tar.gz
timeline-e7714754d8f59bb8dc29aeb6340380e93310175b.tar.bz2
timeline-e7714754d8f59bb8dc29aeb6340380e93310175b.zip
Add validators.
Diffstat (limited to 'Timeline/Models')
-rw-r--r--Timeline/Models/UserDetail.cs7
-rw-r--r--Timeline/Models/Validation/UserDetailValidator.cs113
2 files changed, 116 insertions, 4 deletions
diff --git a/Timeline/Models/UserDetail.cs b/Timeline/Models/UserDetail.cs
index 91439c6a..4af88450 100644
--- a/Timeline/Models/UserDetail.cs
+++ b/Timeline/Models/UserDetail.cs
@@ -1,12 +1,19 @@
using Timeline.Entities;
+using Timeline.Models.Validation;
namespace Timeline.Models
{
public class UserDetail
{
+ [ValidateWith(typeof(UserDetailValidators.QQValidator))]
public string QQ { get; set; }
+
+ [ValidateWith(typeof(UserDetailValidators.EMailValidator))]
public string EMail { get; set; }
+
+ [ValidateWith(typeof(UserDetailValidators.PhoneNumberValidator))]
public string PhoneNumber { get; set; }
+
public string Description { get; set; }
private static string CoerceEmptyToNull(string value)
diff --git a/Timeline/Models/Validation/UserDetailValidator.cs b/Timeline/Models/Validation/UserDetailValidator.cs
index 5fdaec00..19c82edb 100644
--- a/Timeline/Models/Validation/UserDetailValidator.cs
+++ b/Timeline/Models/Validation/UserDetailValidator.cs
@@ -1,11 +1,116 @@
using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Threading.Tasks;
+using System.Net.Mail;
namespace Timeline.Models.Validation
{
- public class UserDetailValidator
+ public abstract class OptionalStringValidator : IValidator
{
+ public bool Validate(object value, out string message)
+ {
+ if (value == null)
+ {
+ message = ValidationConstants.SuccessMessage;
+ return true;
+ }
+
+ if (value is string s)
+ {
+ if (s.Length == 0)
+ {
+ message = ValidationConstants.SuccessMessage;
+ return true;
+ }
+ return DoValidate(s, out message);
+ }
+ else
+ {
+ message = "Value is not of type string.";
+ return false;
+ }
+ }
+
+ protected abstract bool DoValidate(string value, out string message);
+ }
+
+ public static class UserDetailValidators
+ {
+
+ public class QQValidator : OptionalStringValidator
+ {
+ protected override bool DoValidate(string value, out string message)
+ {
+ if (value.Length < 5)
+ {
+ message = "QQ is too short.";
+ return false;
+ }
+
+ if (value.Length > 11)
+ {
+ message = "QQ is too long.";
+ return false;
+ }
+
+ foreach (var c in value)
+ {
+ if (!char.IsDigit(c))
+ {
+ message = "QQ must only contain digit.";
+ return false;
+ }
+ }
+
+ message = ValidationConstants.SuccessMessage;
+ return true;
+ }
+ }
+
+ public class EMailValidator : OptionalStringValidator
+ {
+ protected override bool DoValidate(string value, out string message)
+ {
+ if (value.Length > 50)
+ {
+ message = "E-Mail is too long.";
+ return false;
+ }
+
+ try
+ {
+ var _ = new MailAddress(value);
+ }
+ catch (FormatException)
+ {
+ message = "The format of E-Mail is bad.";
+ return false;
+ }
+ message = ValidationConstants.SuccessMessage;
+ return true;
+ }
+ }
+
+ public class PhoneNumberValidator : OptionalStringValidator
+ {
+ protected override bool DoValidate(string value, out string message)
+ {
+ if (value.Length > 14)
+ {
+ message = "Phone number is too long.";
+ return false;
+ }
+
+ foreach (var c in value)
+ {
+ if (!char.IsDigit(c))
+ {
+ message = "Phone number can only contain digit.";
+ return false;
+ }
+ }
+
+ message = ValidationConstants.SuccessMessage;
+ return true;
+ }
+ }
}
}