Does FluentValidation have error levels out of the box?

Viewed 758

I would like to use the FluentValidation like this:

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.

1 Answers

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

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.

Related