FluentValidation 是否有开箱即用的错误级别?
Does FluentValidation have error levels out of the box?
我想像这样使用 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);
}
}
有这方面的工具吗?该文档不包含有关此的信息。
正如您所说,文档似乎没有提到它,但看起来您可以使用 .WithSeverity(Severity.Error)
,其中 Severity
是一个枚举(枚举),其值为 Error
, Warning
和 Info
public class CustomValidator : AbstractValidator<Customer> {
public CustomValidator()
{
RuleFor(obj => obj.Prop).NotNull().WithSeverity(Severity.Error);
RuleFor(obj => obj.Prop).NotEqual("foo").WithSeverity(Severity.Warning);
}
}
希望对您有所帮助!
请注意,严重性仅供参考;对于警告和信息严重性,IsValid 仍将 return false。
我想像这样使用 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);
}
}
有这方面的工具吗?该文档不包含有关此的信息。
正如您所说,文档似乎没有提到它,但看起来您可以使用 .WithSeverity(Severity.Error)
,其中 Severity
是一个枚举(枚举),其值为 Error
, Warning
和 Info
public class CustomValidator : AbstractValidator<Customer> {
public CustomValidator()
{
RuleFor(obj => obj.Prop).NotNull().WithSeverity(Severity.Error);
RuleFor(obj => obj.Prop).NotEqual("foo").WithSeverity(Severity.Warning);
}
}
希望对您有所帮助!
请注意,严重性仅供参考;对于警告和信息严重性,IsValid 仍将 return false。