Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public static class FluentValidationExtensions
public static IRuleBuilderOptions<T, string?> ChineseLetterNumber<T>(this IRuleBuilder<T, string?> ruleBuilder)
=> ruleBuilder.SetValidator(new ChineseLetterNumberValidator<T>());

public static IRuleBuilderOptions<T, string?> LetterNumberUnderline<T>(this IRuleBuilder<T, string?> ruleBuilder)
=> ruleBuilder.SetValidator(new LetterNumberUnderlineValidator<T>());

public static IRuleBuilderOptions<T, string?> Phone<T>(this IRuleBuilder<T, string?> ruleBuilder, string? culture = null)
=> ruleBuilder.SetValidator(new PhoneValidator<T>(culture));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]*";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}'必须是中文、字母。",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public MasaLanguageManager()
languageProvider.GetTranslation(nameof(ChineseLetterNumberValidator<string>)));
AddTranslation(language, nameof(ChineseLetterNumberUnderlineValidator<string>),
languageProvider.GetTranslation(nameof(ChineseLetterNumberUnderlineValidator<string>)));
AddTranslation(language, nameof(LetterNumberUnderlineValidator<string>),
languageProvider.GetTranslation(nameof(LetterNumberUnderlineValidator<string>)));
AddTranslation(language, nameof(ChineseLetterValidator<string>),
languageProvider.GetTranslation(nameof(ChineseLetterValidator<string>)));
AddTranslation(language, nameof(ChineseLetterUnderlineValidator<string>),
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> : MasaRegularExpressionValidator<T>
{
public override string Name => nameof(LetterNumberUnderlineValidator<T>);

public LetterNumberUnderlineValidator() : base(RegularHelper.LETTER_NUMBER_UNDERLINE)
{
}
}
Original file line number Diff line number Diff line change
@@ -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<RegisterUserEvent>
{
public RegisterUserEventValidator()
{
RuleFor(r => r.Identity).LetterNumberUnderline();
}
}
}