Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'detail' in validation error response #29

Merged
merged 3 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,9 @@ Example response for validation failed:
{
"status": "400",
"title": "Validation failed",
"detail": "Request parameter is missing or not valid.",
"detail": "This value should not be blank.",
"source": {
"parameter": "foo",
"message": "This value should not be blank."
},
"links": {
"about": "http://docs.docs/errors/missing-parameter"
Expand All @@ -153,10 +152,9 @@ Example response for validation failed:
{
"status": "400",
"title": "Validation failed",
"detail": "Request parameter is missing or not valid.",
"detail": "This value has to be larger than 30.",
"source": {
"parameter": "bar",
"message": "This value has to be larger than 30."
},
"links": {
"about": "http://docs.docs/errors/range"
Expand Down
2 changes: 2 additions & 0 deletions src/Model/ErrorWithViolation.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ class ErrorWithViolation extends AbstractError
public static function create(ConstraintViolationInterface $violation, string $title = 'Validation failed')
{
$model = new static($title, 400);
$model->setDescription((string) $violation->getMessage());
$model->setSource([
'parameter' => $violation->getPropertyPath(),
// @deprecated The 'message' will be removed in a future version
'message' => $violation->getMessage(),
]);

Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Model/ErrorWithViolationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function testCreate(): void
[
'status' => '400',
'title' => 'someTitle',
'detail' => 'someMessage',
'source' => [
'parameter' => 'someProperty',
'message' => 'someMessage',
Expand Down