I created a custom validation rule in FluentValidation, but I am unable to figure out how to add a custom state to the error:
RuleFor(x => x.MyField).Custom((myField, context) =>
{
switch (myField)
{
// ...
default:
context.AddFailure($"Unexpected value for MyField");
// want to add custom state to the error here
break;
}
});
Background: I apply my validation to hundreds of items, so having a custom state indicating on which of these items the validation failed would be quite useful.
Got it, one needs to manually create the ValidationFailure
:
var failure = new ValidationFailure
{
ErrorMessage = "Unexpected value for ...",
CustomState = "your custom state"
};
context.AddFailure(failure);