FluentValidation将枚举与WithMessage一起使用

本文关键字:一起 WithMessage 枚举 FluentValidation | 更新日期: 2023-09-27 18:29:22

我有一个类:

public class CustomerType
{
    public int Number { get; set; }
}

以及验证错误的枚举:

public enum CustomerTypeError
{
    None,
    NotNumber,
}

然后我有了验证器类,它现在看起来像这样:

public class CustomerTypeValidator : AbstractValidator<CustomerType>
{
    public CustomerTypeValidator()
    {
        RuleFor(x => x.Number).GreaterThanOrEqualTo("").WithMessage("Number must be greater than 0.");
    }
}

我不想硬编码消息,也不使用资源文件,因为这是应用程序的服务器端。我希望返回CustomerTypeError值。

这可能吗?

FluentValidation将枚举与WithMessage一起使用

我正在使用.WithState(),然后从ValidationFailure对象读取CUstomState。这就奏效了。