简体   繁体   English

FluentValidation与WithMessage一起使用枚举?

[英]FluentValidation to use enum with WithMessage?

I have a class: 我有一堂课:

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

And an enum for validation errors: 还有一个验证错误的枚举:

public enum CustomerTypeError
{
    None,
    NotNumber,
}

And then I have the validator class which currently looks like this: 然后,我有当前看起来像这样的验证器类:

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

I don't want to hard-code the message, and I'm not using resource files as this is the server-side of the application. 我不想对消息进行硬编码,并且我不使用资源文件,因为这是应用程序的服务器端。 I would like to return a CustomerTypeError value. 我想返回一个CustomerTypeError值。

Is this possible? 这可能吗?

I'm using .WithState() and then reading CUstomState from the ValidationFailure object. 我正在使用.WithState(),然后从ValidationFailure对象读取CUstomState。 This does the trick. 这可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM