1

When for example an invalid date has been entered the response message will be the following.

{
     "errors": {
         "AccountDto": [
             "The value '77-77-7777' is invalid for target location."
          ]
    },
    "title": "One or more validation errors occurred.",
    "status": 400
}

I want to customize these errors by returning for example $"{propertyName} input value '77-77-7777' is an invalid date". For every DateTime property

user3261212
  • 391
  • 2
  • 15
  • you could use FluentValidator but im not sure there's a clean way to combine it with a json patch document before applying the changes to the object – LLL Jul 12 '22 at 17:58
  • maybe this will be helpful https://stackoverflow.com/a/58537687/3608449 – LLL Jul 12 '22 at 18:01

1 Answers1

1

Add this method to your controller:

public async Task<IActionResult> Patch([FromBody] JsonPatchDocument<AccountDto> patchDoc)
{
     if (patchDoc is null) 
         return BadRequest("patchDoc is null.");

    // This is an example, you most likely have
    // a service to get the account model
    var account = new Account();
    
    patchDoc.ApplyTo(account, ModelState);
    
    TryValidateModel(account);

    if (!ModelState.IsValid) 
        return UnprocessableEntity(ModelState);
    
    //Save changes...
    
    return NoContent();
}
ggeorge
  • 1,496
  • 2
  • 13
  • 19