I'm working on a project where I provide API services for the clients / consumers. In my APIs with storing or updating, I validate input data with the Laravel Validator. By default, when validation exception is thrown, it responds with a JSON format like below (with status code 422):
{
"message": "The given data was invalid.",
"errors": {
"name" : ["The name field is required."],
"age" : ["The age must be a number."]
}
}
Now some of my clients / consumers are asking if I can provide the rule key / name along with the error message. Their requirement was that they wanted to show their own message based on the rule error happening in my API. They wanted the response to be something like this:
{
"message": "The given data was invalid.",
"errors": {
"name" : [
[
"rule": "required",
"message": "The name field is required."
]
],
"age" : [
[
"rule": "numeric",
"message": "The age must be a number."
]
]
}
}
Now is it possible to change the response of validation error easily? Like by updating the ValidationException class or any package available ?