Skip to content

Commit

Permalink
add more payload validation
Browse files Browse the repository at this point in the history
  • Loading branch information
yceruto committed Nov 12, 2023
1 parent ddab470 commit 58c4d09
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
#[Schema(writeOnly: true)]
class PostResourceBody
{
#[Property]
#[Property(minLength: 3)]
public string $name;
}
5 changes: 3 additions & 2 deletions tests/Functional/App/PostResourceAction/Output/doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
],
"properties": {
"name": {
"type": "string"
"type": "string",
"minLength": 3
}
},
"type": "object",
Expand Down Expand Up @@ -197,4 +198,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"https:\/\/symfony.com\/errors\/validation","title":"Validation Failed","status":422,"detail":"name: This value is too short. It should have 3 characters or more.","violations":[{"propertyPath":"name","title":"This value is too short. It should have 3 characters or more.","template":"This value is too short. It should have {{ limit }} character or more.|This value is too short. It should have {{ limit }} characters or more.","parameters":{"{{ value }}":"\"fo\"","{{ limit }}":"3","{{ value_length }}":"2"},"type":"urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45"}]}
18 changes: 16 additions & 2 deletions tests/Functional/PostResourceActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testEndpoint(): void
$this->assertApiResponse($content);
}

public function testValidation(): void
public function testValidation1(): void
{
$client = self::createClient();
$client->jsonRequest('POST', '/resources', [
Expand All @@ -40,6 +40,20 @@ public function testValidation(): void

self::assertResponseIsUnprocessable();
$this->assertJson($content);
$this->assertApiResponse($content, filename: 'validation_error.json');
$this->assertApiResponse($content, filename: 'validation_error1.json');
}

public function testValidation2(): void
{
$client = self::createClient();
$client->jsonRequest('POST', '/resources', [
'name' => 'fo',
]);

$content = $client->getResponse()->getContent();

self::assertResponseIsUnprocessable();
$this->assertJson($content);
$this->assertApiResponse($content, filename: 'validation_error2.json');
}
}

0 comments on commit 58c4d09

Please sign in to comment.