c# - FluentValidation 如何创建公共(public)部分

标签 c# fluentvalidation

我有一个抽象(没关系)类:

public abstract class CarrierAbstractFormAPI
{
    public string Name { get; set; }
    public string Fein { get; set; }
    public string McNumber { get; set; }
    public string DotNumber { get; set; }

    public AddressCreateAPI Address { get; set; }
}

和 AddressCreateAPI 类:

public class AddressCreateAPI
{
    public string Street { get; set; }
    public string City { get; set; }
    public string ZipPostalCode { get; set; }
    public int StateProvinceId { get; set; }
    public string ContactName { get; set; }
    public string ContactPhone { get; set; }
    public string ContactFaxNumber { get; set; }
    public string ContactEmail { get; set; }
}

我的验证器:

public abstract class CarrierAbstractFluentValidation<T> : AbstractValidator<T> where T : CarrierAbstractFormAPI
{
    public CarrierAbstractFluentValidation()
    {
        RuleFor(d => d.Name)
            .NotEmpty().WithMessage("Name is required");

        RuleFor(d => d.Fein)
            .NotEmpty().WithMessage("Fein is required");

        RuleFor(d => d.McNumber)
            .NotEmpty().WithMessage("McNumber is required");

        RuleFor(d => d.DotNumber)
            .NotEmpty().WithMessage("DotNumber is required");

        RuleFor(d => d.Address.Street)
            .NotEmpty().WithMessage("Address Street is required");

        RuleFor(d => d.Address.City)
            .NotEmpty().WithMessage("Address City is required");

        RuleFor(d => d.Address.StateProvinceId)
            .InclusiveBetween(0, int.MaxValue).WithMessage("Address State is required");

    }
}

它工作正常。 但是我有一些额外的类,可以有

public AddressCreateAPI Address { get; set; }

属性(property)。 我想移动一个部分:

        RuleFor(d => d.Address.Street)
            .NotEmpty().WithMessage("Address Street is required");

        RuleFor(d => d.Address.City)
            .NotEmpty().WithMessage("Address City is required");

        RuleFor(d => d.Address.StateProvinceId)
            .InclusiveBetween(0, int.MaxValue).WithMessage("Address State is required");

到普通类并将其应用于每个我的流利验证器,它具有属性地址。怎么做?

最佳答案

想想看,您需要做的就是重用验证器类。

class AddressCreateAPIValidator : AbstractValidator<AddressCreateAPI>
{
    public AddressCreateAPIValidator()
    {
        RuleFor(d => d.Street)
        .NotEmpty().WithMessage("Address Street is required");

        RuleFor(d => d.City)
            .NotEmpty().WithMessage("Address City is required");

        RuleFor(d => d.StateProvinceId)
            .InclusiveBetween(0, int.MaxValue).WithMessage("Address State is required");
    }
}

class SomeClass
{
    public AddressCreateAPI Prop { get; set; }
}

class SomeClassValidator : AbstractValidator<SomeClass>
{
    public SomeClassValidator()
    {
        RuleFor(d => d.Prop).SetValidator(new AddressCreateAPIValidator());
    }
}

请注意 AddressCreateAPIValidator 如何提取用于验证 AddressCreateAPI 类的通用逻辑,然后通过调用 SetValidator 将其重新用于属性。

如果您想创建通用验证器,您可以将其与其他答案中基于反射的方法混合搭配。

关于c# - FluentValidation 如何创建公共(public)部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52011130/

相关文章:

javascript - 在 TreeView 加载后隐藏 Div 及其在更新面板中

c# - 将 Excel 文件导入 DataGridView

c# - 向 XElement 添加空白行以保留其他格式

c# - 如何知道 Windows 应用商店应用类型可能抛出什么类型的异常?

asp.net-mvc-3 - 流畅的验证 - MVC 3

c# - 使用 fluentvalidation 和 asp.net mvc LessThanOrEqualTo 不触发的不显眼的客户端验证

c# - View 消失后 AVAudioRecorder 崩溃

unit-testing - 来自 commonlibnet 的 FakeItEasy 和 FluentValidation 的假验证码

c# - FluentValidation 自动验证请求 .NET 7 Minimal API

c# - 具有流畅验证的集成测试api