简体   繁体   English

FluentValidation 是否具有开箱即用的错误级别?

[英]Does FluentValidation have error levels out of the box?

I would like to use the FluentValidation like this:我想像这样使用 FluentValidation:

public class CustomValidator : AbstractValidator<Customer> {
  public CustomValidator()
  {
    RuleFor(obj => obj.Prop).NotNull().Level(ErrorLevels.Error);
    RuleFor(obj => obj.Prop).NotEqual("foo").Level(ErrorLevels.Warning);
  }
}

Are there any tools for this?有什么工具可以做到这一点吗? The documentation does not contain information about this.该文档不包含有关此的信息。

As you say, the docs don't seem to mention it but it looks like you can use .WithSeverity(Severity.Error) where Severity is an enumeration (enum) with values of Error , Warning and Info正如你所说,文档似乎没有提到它,但看起来你可以使用.WithSeverity(Severity.Error)其中Severity是一个枚举(enum),其值为ErrorWarningInfo

public class CustomValidator : AbstractValidator<Customer> {
  public CustomValidator()
  {
    RuleFor(obj => obj.Prop).NotNull().WithSeverity(Severity.Error);
    RuleFor(obj => obj.Prop).NotEqual("foo").WithSeverity(Severity.Warning);
  }
}

Hope this helps!希望这可以帮助!

Note that Severity is only informational ;请注意,严重性只是信息性的 IsValid will still return false for Warning and Info severity.对于警告和信息严重性,IsValid 仍将返回false

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

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