Error validation [Answered]

Bbarcode10/14/2022
   [Required]
    [MinLength(8, ErrorMessage = "Password too short")]
    [MaxLength(32, ErrorMessage ="Password too long")]
    public string Password { get; set; }


I have this in my Dto and when validation is invalid I get this for example

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-7e6431d39d15726ab988a4d46bcfec16-79d8864992a8a975-00",
    "errors": {
        "Password": [
            "Password too short"
        ]
    }
}
Bbarcode10/14/2022
Is there a way to change Password to password in response
Bbarcode10/14/2022
because when I try retuning capital case myself like this
                return BadRequest(new {Errors = new {Email = new string[]{"That email is already registered."}}});
it gets snakecased
Bbarcode10/14/2022
and I can't have a consistent error check in my react app
Bbarcode10/14/2022
Image
Bbarcode10/14/2022
response
Bbarcode10/14/2022
when I return it
DDuke10/14/2022
You should return a problemdetails response instead of your own custom object
DDuke10/14/2022
So it's uniform across all responses
Bbarcode10/14/2022
ty
Bbarcode10/14/2022
Image
Bbarcode10/14/2022
it still get lowercased :[
Bbarcode10/14/2022
      var problemDetails = new ProblemDetails {
                    Detail = "Email already registered",
                };
                problemDetails.Extensions.Add("Errors", new List<Dictionary<string, string[]>> ()
                {
                   new Dictionary<string, string[]>() {{"Email", new string[]{ "Email is already registered"}}}
                });
                return BadRequest(problemDetails);
Bbarcode10/14/2022
my code
Bbarcode10/14/2022
Image
Bbarcode10/14/2022
i just need the Email to be uppercase
Bbarcode10/14/2022
any way to enforce this ..?
Bbarcode10/14/2022
or to enforce it to be lowercase in validation dto
DDuke10/14/2022
not sure
DDuke10/14/2022
I use a unified exception handler
DDuke10/14/2022
and handle validation with fluentvalidation instead of asp.net directly
AAccord10/19/2022
✅ This post has been marked as answered!