From 580206ebdcbedb2ce761ed64870a61426410492f Mon Sep 17 00:00:00 2001 From: Mayue Date: Thu, 29 Jun 2023 10:01:10 +0800 Subject: [PATCH] feat:add LetterNumberUnderlineValidator --- .../FluentValidationExtensions.cs | 3 ++ .../Internal/RegularHelper.cs | 1 + .../Resources/ChineseSimplifiedLanguage.cs | 1 + .../Resources/EnglishLanguage.cs | 1 + .../Resources/MasaLanguageManager.cs | 2 + .../LetterNumberUnderlineValidator.cs | 15 +++++++ .../LetterNumberUnderlineValidatorTest.cs | 43 +++++++++++++++++++ 7 files changed, 66 insertions(+) create mode 100644 src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Validators/LetterNumberUnderlineValidator.cs create mode 100644 src/Utils/Extensions/Validations/Tests/Masa.Utils.Extensions.Validations.FluentValidation.Tests/LetterNumberUnderlineValidatorTest.cs diff --git a/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/FluentValidationExtensions.cs b/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/FluentValidationExtensions.cs index 6021bdacb..57cc5583c 100644 --- a/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/FluentValidationExtensions.cs +++ b/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/FluentValidationExtensions.cs @@ -37,6 +37,9 @@ public static class FluentValidationExtensions public static IRuleBuilderOptions ChineseLetterNumber(this IRuleBuilder ruleBuilder) => ruleBuilder.SetValidator(new ChineseLetterNumberValidator()); + public static IRuleBuilderOptions LetterNumberUnderline(this IRuleBuilder ruleBuilder) + => ruleBuilder.SetValidator(new LetterNumberUnderlineValidator()); + public static IRuleBuilderOptions Phone(this IRuleBuilder ruleBuilder, string? culture = null) => ruleBuilder.SetValidator(new PhoneValidator(culture)); diff --git a/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Internal/RegularHelper.cs b/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Internal/RegularHelper.cs index a606fdb21..60819d48d 100644 --- a/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Internal/RegularHelper.cs +++ b/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Internal/RegularHelper.cs @@ -17,6 +17,7 @@ internal static class RegularHelper internal const string CHINESE_LETTER_NUMBER = "^[\u4e00-\u9fa5a-zA-Z0-9]+$"; internal const string CHINESE_LETTER = "^[\u4e00-\u9fa5a-zA-Z]+$"; internal const string CHINESE_LETTER_NUMBER_UNDERLINE = "^[\u4e00-\u9fa5_a-zA-Z0-9]+$"; + internal const string LETTER_NUMBER_UNDERLINE = "^[_a-zA-Z0-9]+$"; internal const string CHINESE_LETTER_UNDERLINE = "^[\u4e00-\u9fa5_a-zA-Z]+$"; internal const string IDCARD = "(^\\d{15}$)|(^\\d{17}([0-9]|X|x)$)"; internal const string URL = "[a-zA-z]+://[^s]*"; diff --git a/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Resources/ChineseSimplifiedLanguage.cs b/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Resources/ChineseSimplifiedLanguage.cs index 82629ebb4..4ef86d5b2 100644 --- a/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Resources/ChineseSimplifiedLanguage.cs +++ b/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Resources/ChineseSimplifiedLanguage.cs @@ -11,6 +11,7 @@ internal class ChineseSimplifiedLanguage : ILanguageProvider public string GetTranslation(string key) => key switch { "ChineseLetterNumberUnderlineValidator" => "'{PropertyName}'必须是中文、数字、字母或下划线。", + "LetterNumberUnderlineValidator" => "'{PropertyName}'必须是数字、字母或下划线。", "ChineseLetterNumberValidator" => "'{PropertyName}'必须是中文、数字、字母。", "ChineseLetterUnderlineValidator" => "'{PropertyName}'必须是中文、字母或下划线。", "ChineseLetterValidator" => "'{PropertyName}'必须是中文、字母。", diff --git a/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Resources/EnglishLanguage.cs b/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Resources/EnglishLanguage.cs index 35807fd7d..19f2fb686 100644 --- a/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Resources/EnglishLanguage.cs +++ b/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Resources/EnglishLanguage.cs @@ -11,6 +11,7 @@ internal class EnglishLanguage : ILanguageProvider public string GetTranslation(string key) => key switch { "ChineseLetterNumberUnderlineValidator" => "'{PropertyName}' must be Chinese, numbers, letters or underscores.", + "LetterNumberUnderlineValidator" => "'{PropertyName}' must be numbers, letters or underscores.", "ChineseLetterNumberValidator" => "'{PropertyName}' must be Chinese, numbers, letters.", "ChineseLetterUnderlineValidator" => "'{PropertyName}' must be Chinese, letters or underscores.", "ChineseLetterValidator" => "'{PropertyName}' must be Chinese, letters.", diff --git a/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Resources/MasaLanguageManager.cs b/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Resources/MasaLanguageManager.cs index f63511975..1b0fb5d71 100644 --- a/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Resources/MasaLanguageManager.cs +++ b/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Resources/MasaLanguageManager.cs @@ -22,6 +22,8 @@ public MasaLanguageManager() languageProvider.GetTranslation(nameof(ChineseLetterNumberValidator))); AddTranslation(language, nameof(ChineseLetterNumberUnderlineValidator), languageProvider.GetTranslation(nameof(ChineseLetterNumberUnderlineValidator))); + AddTranslation(language, nameof(LetterNumberUnderlineValidator), + languageProvider.GetTranslation(nameof(LetterNumberUnderlineValidator))); AddTranslation(language, nameof(ChineseLetterValidator), languageProvider.GetTranslation(nameof(ChineseLetterValidator))); AddTranslation(language, nameof(ChineseLetterUnderlineValidator), diff --git a/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Validators/LetterNumberUnderlineValidator.cs b/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Validators/LetterNumberUnderlineValidator.cs new file mode 100644 index 000000000..c99e4c669 --- /dev/null +++ b/src/Utils/Extensions/Validations/Masa.Utils.Extensions.Validations.FluentValidation/Validators/LetterNumberUnderlineValidator.cs @@ -0,0 +1,15 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +// ReSharper disable once CheckNamespace + +namespace FluentValidation.Validators; + +public class LetterNumberUnderlineValidator : MasaRegularExpressionValidator +{ + public override string Name => nameof(LetterNumberUnderlineValidator); + + public LetterNumberUnderlineValidator() : base(RegularHelper.LETTER_NUMBER_UNDERLINE) + { + } +} diff --git a/src/Utils/Extensions/Validations/Tests/Masa.Utils.Extensions.Validations.FluentValidation.Tests/LetterNumberUnderlineValidatorTest.cs b/src/Utils/Extensions/Validations/Tests/Masa.Utils.Extensions.Validations.FluentValidation.Tests/LetterNumberUnderlineValidatorTest.cs new file mode 100644 index 000000000..360451005 --- /dev/null +++ b/src/Utils/Extensions/Validations/Tests/Masa.Utils.Extensions.Validations.FluentValidation.Tests/LetterNumberUnderlineValidatorTest.cs @@ -0,0 +1,43 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +namespace Masa.Utils.Extensions.Validations.FluentValidation.Tests; + +[TestClass] +public class LetterNumberUnderlineValidatorTest : ValidatorBaseTest +{ + public override string Message => "'Identity' must be numbers, letters or underscores."; + + [DataRow("团队123", false)] + [DataRow("Masa团队", false)] + [DataRow("masastack", true)] + [DataRow("123", true)] + [DataRow("masastack123", true)] + [DataRow(".", false)] + [DataRow("123.", false)] + [DataRow("123_", true)] + [DataRow(null, true)] + [DataRow("", false)] + [DataTestMethod] + public void TestLetterNumberUnderline(string identity, bool expectedResult) + { + var validator = new RegisterUserEventValidator(); + var result = validator.Validate(new RegisterUserEvent() + { + Identity = identity + }); + Assert.AreEqual(expectedResult, result.IsValid); + if (!expectedResult) + { + Assert.AreEqual(Message, result.Errors[0].ErrorMessage); + } + } + + public class RegisterUserEventValidator : MasaAbstractValidator + { + public RegisterUserEventValidator() + { + RuleFor(r => r.Identity).LetterNumberUnderline(); + } + } +}