i'm using fluent validation (both 9 and 10 now..) to request validation in asp.net core 5 web application, i'm having some issues with custom messages and error codes...
public class FooRequest
{
public string foo1 { get; set; }
public string foo2 { get; set; }
}
public class FooValidator : AbstractValidator<FooRequest>
{
public FooValidator() : base()
{
RuleFor(r => r.foo1)
.NotEmpty()
.WithMessage($"my message 1")
.WithErrorCode("MY_ERROR_CODE_1");
}
}
//controller's action with DI
[HttpPost("fooValidator")]
public void fooValidator([FromBody] FooRequest fooRequest)
{
}
but i receive and unexpected response (without custom errorCode, and with customMessage inside the errorCode), where i'm wrong??
{
"message": "Bad Request",
"errorCode": "my message 1"
}
P.S. if i change the validator, to set just customErorr
RuleFor(r => r.foo1)
.NotEmpty()
.WithErrorCode("MY_ERROR_CODE_1");
i receive the default one:
{
"message": "Bad Request",
"errorCode": "'foo1' must not be empty."
}
P.S.2 if i try to set the customMessage:
RuleFor(r => r.foo1)
.NotEmpty()
//.WithErrorCode("MY_ERROR_CODE_1")
.WithMessage("my error message 1");
i receive the default message, with errorCode
{
"message": "Bad Request",
"errorCode": "my error message 1"
}