FluentValidation默认的严重级别 是错误。如果我们想把它改成Warning
的话
我们可以使用 WithServerity
RuleFor(x => x.Surname).NotNull().WithSeverity(Severity.Warning);
总共有三个级别
完整的示例代码如下
public class Person
{
public string Surname { get; set; }
public string Forename { get; set; }
}
public class PersonValidator : AbstractValidator<Person>
{
public PersonValidator()
{
RuleFor(person => person.Forename).NotNull();
RuleFor(x => x.Surname).NotNull().WithSeverity(Severity.Warning);
}
}
class Program
{
static void Main(string[] args)
{
var validator = new PersonValidator();
var result = validator.Validate(new Person());
foreach (var failure in result.Errors)
{
Console.WriteLine($"Property: {failure.PropertyName} Severity: {failure.Severity}");
}
}
}
// 输出结果
// Property: Surname Severity: Warning
// Property: Forename Severity: Error
我们还可以设置全局的严重级别。
ValidatorOptions.Global.Severity = Severity.Info;