diff options
author | 杨宇千 <crupest@outlook.com> | 2019-10-21 20:47:31 +0800 |
---|---|---|
committer | 杨宇千 <crupest@outlook.com> | 2019-10-21 20:47:31 +0800 |
commit | c442b7ad597f430b186dd8019de70332b574c4ba (patch) | |
tree | e6a9d4204e4fcd047cfcf5acd4ff566cf8bb69ff /Timeline/Models | |
parent | 4c52f2d8b7aae43f391f54fb67fe91b8bc64e0c5 (diff) | |
download | timeline-c442b7ad597f430b186dd8019de70332b574c4ba.tar.gz timeline-c442b7ad597f430b186dd8019de70332b574c4ba.tar.bz2 timeline-c442b7ad597f430b186dd8019de70332b574c4ba.zip |
...
Diffstat (limited to 'Timeline/Models')
-rw-r--r-- | Timeline/Models/Http/Common.cs | 8 | ||||
-rw-r--r-- | Timeline/Models/PutResult.cs | 4 | ||||
-rw-r--r-- | Timeline/Models/UserDetail.cs | 45 | ||||
-rw-r--r-- | Timeline/Models/Validation/UserDetailValidator.cs | 116 | ||||
-rw-r--r-- | Timeline/Models/Validation/UsernameValidator.cs | 5 | ||||
-rw-r--r-- | Timeline/Models/Validation/Validator.cs | 44 |
6 files changed, 39 insertions, 183 deletions
diff --git a/Timeline/Models/Http/Common.cs b/Timeline/Models/Http/Common.cs index 130439d3..c741837a 100644 --- a/Timeline/Models/Http/Common.cs +++ b/Timeline/Models/Http/Common.cs @@ -86,13 +86,13 @@ namespace Timeline.Models.Http internal static CommonPutResponse Create(IStringLocalizerFactory localizerFactory)
{
- var localizer = localizerFactory.Create("Http.Common");
+ var localizer = localizerFactory.Create("Models.Http.Common");
return new CommonPutResponse(0, localizer["ResponsePutCreate"], true);
}
internal static CommonPutResponse Modify(IStringLocalizerFactory localizerFactory)
{
- var localizer = localizerFactory.Create("Http.Common");
+ var localizer = localizerFactory.Create("Models.Http.Common");
return new CommonPutResponse(0, localizer["ResponsePutModify"], false);
}
@@ -123,13 +123,13 @@ namespace Timeline.Models.Http internal static CommonDeleteResponse Delete(IStringLocalizerFactory localizerFactory)
{
- var localizer = localizerFactory.Create("Http.Common");
+ var localizer = localizerFactory.Create("Models.Http.Common");
return new CommonDeleteResponse(0, localizer["ResponseDeleteDelete"], true);
}
internal static CommonDeleteResponse NotExist(IStringLocalizerFactory localizerFactory)
{
- var localizer = localizerFactory.Create("Http.Common");
+ var localizer = localizerFactory.Create("Models.Models.Http.Common");
return new CommonDeleteResponse(0, localizer["ResponseDeleteNotExist"], false);
}
}
diff --git a/Timeline/Models/PutResult.cs b/Timeline/Models/PutResult.cs index 544602eb..cecf86e6 100644 --- a/Timeline/Models/PutResult.cs +++ b/Timeline/Models/PutResult.cs @@ -8,10 +8,10 @@ namespace Timeline.Models /// <summary>
/// Indicates the item did not exist and now is created.
/// </summary>
- Created,
+ Create,
/// <summary>
/// Indicates the item exists already and is modified.
/// </summary>
- Modified
+ Modify
}
}
diff --git a/Timeline/Models/UserDetail.cs b/Timeline/Models/UserDetail.cs deleted file mode 100644 index 302e3bb1..00000000 --- a/Timeline/Models/UserDetail.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System.ComponentModel.DataAnnotations;
-using Timeline.Entities;
-using Timeline.Models.Validation;
-using Newtonsoft.Json;
-
-namespace Timeline.Models
-{
- public class UserDetail
- {
- [MaxLength(10)]
- public string? Nickname { get; set; }
-
- [ValidateWith(typeof(UserDetailValidators.QQValidator))]
- [JsonProperty(PropertyName = "qq")]
- 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)
- {
- if (string.IsNullOrEmpty(value))
- return null;
- else
- return value;
- }
-
- public static UserDetail From(UserDetailEntity entity)
- {
- return new UserDetail
- {
- Nickname = CoerceEmptyToNull(entity.Nickname),
- QQ = CoerceEmptyToNull(entity.QQ),
- Email = CoerceEmptyToNull(entity.Email),
- PhoneNumber = CoerceEmptyToNull(entity.PhoneNumber),
- Description = CoerceEmptyToNull(entity.Description)
- };
- }
- }
-}
diff --git a/Timeline/Models/Validation/UserDetailValidator.cs b/Timeline/Models/Validation/UserDetailValidator.cs deleted file mode 100644 index 19c82edb..00000000 --- a/Timeline/Models/Validation/UserDetailValidator.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System;
-using System.Net.Mail;
-
-namespace Timeline.Models.Validation
-{
- 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;
- }
- }
- }
-}
diff --git a/Timeline/Models/Validation/UsernameValidator.cs b/Timeline/Models/Validation/UsernameValidator.cs index e4891400..ecc3b5b3 100644 --- a/Timeline/Models/Validation/UsernameValidator.cs +++ b/Timeline/Models/Validation/UsernameValidator.cs @@ -1,4 +1,5 @@ -using System.Linq;
+using Microsoft.Extensions.Localization;
+using System.Linq;
using System.Text.RegularExpressions;
namespace Timeline.Models.Validation
@@ -10,7 +11,7 @@ namespace Timeline.Models.Validation private readonly Regex _regex = new Regex(RegexPattern);
- protected override bool DoValidate(string value, out string message)
+ protected override bool DoValidate(string value, IStringLocalizerFactory localizerFactory, out string message)
{
if (value.Length == 0)
{
diff --git a/Timeline/Models/Validation/Validator.cs b/Timeline/Models/Validation/Validator.cs index a1acbed9..a3800b71 100644 --- a/Timeline/Models/Validation/Validator.cs +++ b/Timeline/Models/Validation/Validator.cs @@ -1,11 +1,14 @@ -using System;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Localization;
+using System;
using System.ComponentModel.DataAnnotations;
+using Timeline.Helpers;
namespace Timeline.Models.Validation
{
/// <summary>
/// A validator to validate value.
- /// See <see cref="Validate(object, out string)"/>.
+ /// See <see cref="Validate(object?, out string)"/>.
/// </summary>
public interface IValidator
{
@@ -15,7 +18,7 @@ namespace Timeline.Models.Validation /// <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);
+ bool Validate(object? value, IStringLocalizerFactory localizerFactory, out string message);
}
public static class ValidationConstants
@@ -36,27 +39,36 @@ namespace Timeline.Models.Validation /// </remarks>
public abstract class Validator<T> : IValidator
{
- public bool Validate(object value, out string message)
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "<Pending>")]
+ public bool Validate(object? value, IStringLocalizerFactory localizerFactory, out string message)
{
if (value == null)
{
- message = "Value is null.";
+ var localizer = localizerFactory.Create("Models.Validation.Validator");
+ message = localizer["ValidatorMessageNull"];
return false;
}
if (value is T v)
{
-
- return DoValidate(v, out message);
+ return DoValidate(v, localizerFactory, out message);
}
else
{
- message = $"Value is not of type {typeof(T).Name}";
+ var localizer = localizerFactory.Create("Models.Validation.Validator");
+ message = localizer["ValidatorMessageBadType", typeof(T).FullName];
return false;
}
}
- protected abstract bool DoValidate(T value, out string message);
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods")]
+ protected static string GetSuccessMessage(IStringLocalizerFactory factory)
+ {
+ var localizer = factory.Create("Models.Validation.Validator");
+ return localizer["ValidatorMessageSuccess"];
+ }
+
+ protected abstract bool DoValidate(T value, IStringLocalizerFactory localizerFactory, out string message);
}
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter,
@@ -84,24 +96,28 @@ namespace Timeline.Models.Validation throw new ArgumentNullException(nameof(validatorType));
if (!typeof(IValidator).IsAssignableFrom(validatorType))
- throw new ArgumentException("Given type is not assignable to IValidator.", nameof(validatorType));
+ throw new ArgumentException(
+ Resources.Models.Validation.Validator.ValidateWithAttributeNotValidator,
+ nameof(validatorType));
try
{
- _validator = Activator.CreateInstance(validatorType) as IValidator;
+ _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);
+ throw new ArgumentException(
+ Resources.Models.Validation.Validator.ValidateWithAttributeCreateFail, e);
}
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
- if (_validator.Validate(value, out var message))
+ var localizerFactory = validationContext.GetRequiredService<IStringLocalizerFactory>();
+ if (_validator.Validate(value, localizerFactory, out var message))
return ValidationResult.Success;
else
- return new ValidationResult(string.Format("Field {0} is bad. {1}", validationContext.DisplayName, message));
+ return new ValidationResult(message);
}
}
}
|