Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Abid's avatar
Level 11

[Help] API Response for Validation errors \w Rule name

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 ?

0 likes
6 replies
Abid's avatar
Level 11

Wow, nice!

This is a good start. It works, but for now have to do this manually in every controller methods. Have to find a way to do this either globally or by scope (ex. only for /api/v2).

Thanks. I will update further on the solution.

Abid's avatar
Level 11

So I checked the Validator related classes and found ways to extend it.

I created a middleware on which I set my own validator as the validator resolver. The validator also extends the laravel's validator. With your help from example above, I modified my validator to get my desired response.

Currently it works well for API responses. But may not work well in blade view files due to MessageBag class. Maybe someone finds a better implementation someday. Or maybe even added in Core.

Thank for the help @frankielee .

You can find the implementation in the gist.

martinbean's avatar

@abid It’s possible, but I‘d tell your client you won’t be changing your responses based on their requests.

The reason being, this is an API. If you change API response formats for one client, then you’re going to likely break integrations for other clients if they’re relying on responses being a particular format.

You’re the API vendor. You dictate what your API returns; not clients. I’d be a bit pissed if I was using say, Stripe’s API and one day errors started coming back in a completely different format because Merchant A had requested it and Stripe just complied with the request.

3 likes
Abid's avatar
Level 11

Yes. I did told them we wont be changing it soon as there are others who depends on it.

For future enhancement, maybe say /api/v2, will implement something like it. Thou I have to check how much of it is possible supporting both v1 & v2.

1 like

Please or to participate in this conversation.