I'm using this Lib to validate my models, but if a mode is not properly assigned it returns a error message, I just want to retrieve the StatusCode
but with no message.
RuleFor(x => x.Email).NotEmpty().EmailAddress().WithMessage("");
If I put a null or "" in the .WithMessage
method I get an Exception!
It seems to me that if you want to return an empty error, then something is wrong with your architecture.
However, you can do it like this:
RuleFor(x => x.Email).NotEmpty().EmailAddress().WithMessage((_) => "");
Such a design can be useful if you don't know in advance what the error message should be. Like this:
string error = string.Empty;
RuleFor(x => x.SomeProperty)
.Must(x =>
{
if (someСondition)
{
error = "error message 1";
return false;
}
if (anotherСondition)
{
error = $"error message 2: {x.ToString()}";
return false;
}
return true;
})
.WithMessage((_) =>
{
return error;
});