I develop an REST API using Lumen. I thought using details key inside JSON object is convenient to show errors. See the response:
{"detail": "Something bad happened."}
Lumen has validation that you can call inside a controller as in $this->validate(["foo" => "required", "bar" => "required"}). If I do not set foo value in the request body, the server naturally responds:
{
"foo": [
"The foo field is required."
]
}
However, I also want these to be inside details key so that it would be convenient for consumer. See the desired repsonse:
{
"details": {
"foo": [
"The foo field is required."
]
}
}
Is it possible to do this, if so, how?
Further Investigation
As Validation section in Lumen documentation states:
Should validation fail, the $this->validate helper will throw Illuminate\Validation\ValidationException with embedded JSON response that includes all relevant error messages.
That means somehow I need to extend ValidationException, but I still do not have any idea about how to trigger my CustomValidationException in $this->validate method.
Environment
- PHP 7.3.5
- Lumen 5.8