2

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

2
  • 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 Commented Jul 12, 2022 at 17:58
  • maybe this will be helpful stackoverflow.com/a/58537687/3608449 Commented Jul 12, 2022 at 18:01

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.