Skip to content

Commit 228d334

Browse files
committed
Specify a custom HttpResponseException when validation rules fails
1 parent ef43c3e commit 228d334

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/ResourceRequest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace HDSSolutions\Laravel\API;
44

5+
use Illuminate\Contracts\Validation\Validator;
56
use Illuminate\Foundation\Http\FormRequest;
7+
use Illuminate\Http\Exceptions\HttpResponseException;
8+
use Illuminate\Http\Response;
69
use RuntimeException;
710

811
class ResourceRequest extends FormRequest implements Contracts\ResourceRequest {
@@ -49,4 +52,11 @@ protected function destroy(): array {
4952
return [];
5053
}
5154

55+
protected function failedValidation(Validator $validator): void {
56+
throw new HttpResponseException(response()->json([
57+
'message' => 'Request validation failed',
58+
'data' => $validator->errors(),
59+
], Response::HTTP_UNPROCESSABLE_ENTITY));
60+
}
61+
5262
}

0 commit comments

Comments
 (0)