From c2998e5833da46bbe5539bb4ce49fb3f10867a1d Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 30 Apr 2021 18:09:01 +0800 Subject: refactor: ... --- .../Timeline/Models/Validation/NameValidator.cs | 9 +- .../Models/Validation/NicknameValidator.cs | 3 +- .../Models/Validation/Resource.Designer.cs | 153 +++++++++++++++++++++ BackEnd/Timeline/Models/Validation/Resource.resx | 150 ++++++++++++++++++++ BackEnd/Timeline/Models/Validation/Validator.cs | 11 +- 5 files changed, 313 insertions(+), 13 deletions(-) create mode 100644 BackEnd/Timeline/Models/Validation/Resource.Designer.cs create mode 100644 BackEnd/Timeline/Models/Validation/Resource.resx (limited to 'BackEnd/Timeline/Models/Validation') diff --git a/BackEnd/Timeline/Models/Validation/NameValidator.cs b/BackEnd/Timeline/Models/Validation/NameValidator.cs index b74c40b7..2220de6f 100644 --- a/BackEnd/Timeline/Models/Validation/NameValidator.cs +++ b/BackEnd/Timeline/Models/Validation/NameValidator.cs @@ -1,6 +1,5 @@ using System.Linq; using System.Text.RegularExpressions; -using static Timeline.Resources.Models.Validation.NameValidator; namespace Timeline.Models.Validation { @@ -14,26 +13,26 @@ namespace Timeline.Models.Validation { if (value.Length == 0) { - return (false, MessageEmptyString); + return (false, Resource.NameCantBeEmpty); } if (value.Length > MaxLength) { - return (false, MessageTooLong); + return (false, string.Format(Resource.NameCantBeLongerThan, MaxLength)); } foreach ((char c, int i) in value.Select((c, i) => (c, i))) { if (!(char.IsLetterOrDigit(c) || c == '-' || c == '_')) { - return (false, MessageInvalidChar); + return (false, Resource.NameInvalidChar); } } // Currently name can't be longer than 26. So this is not needed. But reserve it for future use. if (UniqueIdRegex.IsMatch(value)) { - return (false, MessageUnqiueId); + return (false, Resource.NameCantBeUniqueIdPattern); } return (true, GetSuccessMessage()); diff --git a/BackEnd/Timeline/Models/Validation/NicknameValidator.cs b/BackEnd/Timeline/Models/Validation/NicknameValidator.cs index 1d6ab163..43365dce 100644 --- a/BackEnd/Timeline/Models/Validation/NicknameValidator.cs +++ b/BackEnd/Timeline/Models/Validation/NicknameValidator.cs @@ -1,5 +1,4 @@ using System; -using static Timeline.Resources.Models.Validation.NicknameValidator; namespace Timeline.Models.Validation { @@ -8,7 +7,7 @@ namespace Timeline.Models.Validation protected override (bool, string) DoValidate(string value) { if (value.Length > 25) - return (false, MessageTooLong); + return (false, Resource.NicknameTooLong); return (true, GetSuccessMessage()); } diff --git a/BackEnd/Timeline/Models/Validation/Resource.Designer.cs b/BackEnd/Timeline/Models/Validation/Resource.Designer.cs new file mode 100644 index 00000000..47ad4248 --- /dev/null +++ b/BackEnd/Timeline/Models/Validation/Resource.Designer.cs @@ -0,0 +1,153 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Timeline.Models.Validation { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resource { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resource() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Timeline.Models.Validation.Resource", typeof(Resource).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to it can't be null. + /// + internal static string CantBeNull { + get { + return ResourceManager.GetString("CantBeNull", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to name can't be empty. + /// + internal static string NameCantBeEmpty { + get { + return ResourceManager.GetString("NameCantBeEmpty", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to name can't be longer than {0}. + /// + internal static string NameCantBeLongerThan { + get { + return ResourceManager.GetString("NameCantBeLongerThan", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to name can't has the same pattern of unique id. + /// + internal static string NameCantBeUniqueIdPattern { + get { + return ResourceManager.GetString("NameCantBeUniqueIdPattern", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to name can only consists of alphabet, number, '_' and '-' . + /// + internal static string NameInvalidChar { + get { + return ResourceManager.GetString("NameInvalidChar", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to nickname can't be longer than 25. + /// + internal static string NicknameTooLong { + get { + return ResourceManager.GetString("NicknameTooLong", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to it is not of type {0}. + /// + internal static string NotOfType { + get { + return ResourceManager.GetString("NotOfType", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Failed to create validator instance.. + /// + internal static string ValidateWithAttributeExceptionCreateFail { + get { + return ResourceManager.GetString("ValidateWithAttributeExceptionCreateFail", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Given type is not a IValidator instance.. + /// + internal static string ValidateWithAttributeExceptionNotValidator { + get { + return ResourceManager.GetString("ValidateWithAttributeExceptionNotValidator", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to validation passed. + /// + internal static string ValidationPassed { + get { + return ResourceManager.GetString("ValidationPassed", resourceCulture); + } + } + } +} diff --git a/BackEnd/Timeline/Models/Validation/Resource.resx b/BackEnd/Timeline/Models/Validation/Resource.resx new file mode 100644 index 00000000..68ba3810 --- /dev/null +++ b/BackEnd/Timeline/Models/Validation/Resource.resx @@ -0,0 +1,150 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + it can't be null + + + name can't be empty + + + name can't be longer than {0} + + + name can't has the same pattern of unique id + + + name can only consists of alphabet, number, '_' and '-' + + + nickname can't be longer than 25 + + + it is not of type {0} + + + Failed to create validator instance. + + + Given type is not a IValidator instance. + + + validation passed + + \ No newline at end of file diff --git a/BackEnd/Timeline/Models/Validation/Validator.cs b/BackEnd/Timeline/Models/Validation/Validator.cs index ec6cc0af..d334960e 100644 --- a/BackEnd/Timeline/Models/Validation/Validator.cs +++ b/BackEnd/Timeline/Models/Validation/Validator.cs @@ -1,6 +1,5 @@ using System; using System.ComponentModel.DataAnnotations; -using static Timeline.Resources.Models.Validation.Validator; namespace Timeline.Models.Validation { @@ -56,7 +55,7 @@ namespace Timeline.Models.Validation if (PermitNull) return (true, GetSuccessMessage()); else - return (false, ValidatorMessageNull); + return (false, Resource.CantBeNull); } if (value is T v) @@ -65,11 +64,11 @@ namespace Timeline.Models.Validation } else { - return (false, ValidatorMessageBadType); + return (false, string.Format(Resource.NotOfType, typeof(T).Name)); } } - protected static string GetSuccessMessage() => ValidatorMessageSuccess; + protected static string GetSuccessMessage() => Resource.ValidationPassed; protected abstract (bool, string) DoValidate(T value); } @@ -99,7 +98,7 @@ namespace Timeline.Models.Validation throw new ArgumentNullException(nameof(validatorType)); if (!typeof(IValidator).IsAssignableFrom(validatorType)) - throw new ArgumentException(ValidateWithAttributeExceptionNotValidator, nameof(validatorType)); + throw new ArgumentException(Resource.ValidateWithAttributeExceptionNotValidator, nameof(validatorType)); try { @@ -107,7 +106,7 @@ namespace Timeline.Models.Validation } catch (Exception e) { - throw new ArgumentException(ValidateWithAttributeExceptionCreateFail, e); + throw new ArgumentException(Resource.ValidateWithAttributeExceptionCreateFail, e); } } -- cgit v1.2.3