流畅的验证 - 如何重用属性规则?

问题描述 投票:0回答:1

我正在使用 MediatR 库将 Fluent Validations 与 CQRS 结合使用。我的解决方案中使用了以下 Fluent 包版本以及 .NET 7 和 C#。

FluentValidation - 11.8.1

FluentValidation.DependencyInjectionExtensions - 11.8.1

我有以下“Group”属性验证器,

CreateGroupRequest
UpdateGroupRequest
中的“字符串”数据类型。

public class CreateGroupRequest : IRequest<BaseResponse<CreateGroupResponse>>
{
    public string Group { get; set; }
}

public class CreateGroupValidator : AbstractValidator<CreateGroupRequest>
{
    public CreateGroupValidator(IOptions<FeatureOptions> options)
    {
        var featureOptions = options.Value;

        ClassLevelCascadeMode = CascadeMode.Stop;

        RuleFor(r => r.Group).NotNull().WithMessage("Group can't be null.");

        RuleFor(r => r.Group).NotEmpty().WithMessage("Group must not be empty.");

        RuleFor(r => r.Group).Length(featureOptions.GroupMinLength, featureOptions.GroupMaxLength)
                             .WithMessage("Group length must be between 1 and 15.");

        RuleFor(r => r.Group).Matches(featureOptions.GroupAllowedCharactersRegex)
                             .WithMessage("Group must start and end with lower case letters or numbers. It may contain \"_\" and \" - \".");
    }
}


public class UpdateGroupValidator : AbstractValidator<UpdateGroupRequest>
{
    public UpdateGroupValidator(IOptions<FeatureOptions> options)
    {
        var featureOptions = options.Value;

        ClassLevelCascadeMode = CascadeMode.Stop;

        RuleFor(r => r.Group).NotNull().WithMessage("Group can't be null.");

        RuleFor(r => r.Group).NotEmpty().WithMessage("Group must not be empty.");

        RuleFor(r => r.Group).Length(featureOptions.GroupMinLength, featureOptions .GroupMaxLength)
                             .WithMessage("Group length must be between 1 and 15.");

        RuleFor(r => r.Group).Matches(labelingOptions.GroupAllowedCharactersRegex)
                             .WithMessage("Group must start and end with lower case letters or numbers. It may contain \"_\" and \" - \".");
    }
}

我在验证器中使用

ClassLevelCascadeMode = CascadeMode.Stop
设置。

还有其他需要验证“Group”属性的请求。有没有办法移动“Group”属性上的所有验证,以便在单个位置定义规则并在验证器中重用?

如有任何帮助,我们将不胜感激。

c# .net cqrs fluentvalidation mediatr
1个回答
0
投票

首先,您可以简化代码,将所有规则分组在一个声明中:

public CreateGroupValidator(IOptions<FeatureOptions> options)
{
    var featureOptions = options.Value;

    ClassLevelCascadeMode = CascadeMode.Stop;

    RuleFor(r => r.Group).NotNull().WithMessage("Group can't be null.")
                         .NotEmpty().WithMessage("Group must not be empty.")
                         .Length(featureOptions.GroupMinLength, featureOptions.GroupMaxLength).WithMessage("Group length must be between 1 and 15.")
                         .Matches(featureOptions.GroupAllowedCharactersRegex).WithMessage("Group must start and end with lower case letters or numbers. It may contain \"_\" and \" - \".");

}

同样的事情

UpdateGroupValidator


也就是说,您可以使用扩展方法进行重构:

public static class ValidationExtensions
{
    public static IRuleBuilderOptions<TCommand, TProperty?> MustBeAValidGroup<TCommand, TProperty>(this IRuleBuilder<TCommand, TProperty?> group)
    {
        return group.NotNull().WithMessage("Group can't be null.")
                         .NotEmpty().WithMessage("Group must not be empty.")
                         .Length(featureOptions.GroupMinLength, featureOptions.GroupMaxLength).WithMessage("Group length must be between 1 and 15.")
                         .Matches(featureOptions.GroupAllowedCharactersRegex).WithMessage("Group must start and end with lower case letters or numbers. It may contain \"_\" and \" - \".");
    }
}

然后,添加

ValidationExtensions
的使用,您可以在
CreateGroupValidator
UpdateGroupValidator
中使用它,如下所示:

[...]
RuleFor(r => r.Group).MustBeAValidGroup();
[...]
© www.soinside.com 2019 - 2024. All rights reserved.