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

Allow to fix validation for parameters with a format #130

Merged
merged 3 commits into from
Jan 19, 2017
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
5 changes: 5 additions & 0 deletions src/Handler/ValidatedDescriptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public function __invoke(callable $handler)

foreach ($operation->getParams() as $name => $schema) {
$value = $command[$name];

if ($value) {
$value = $schema->filter($value);
}

if (! $this->validator->validate($schema, $value)) {
$errors = array_merge($errors, $this->validator->getErrors());
} elseif ($value !== $command[$name]) {
Expand Down
2 changes: 1 addition & 1 deletion src/SchemaFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function dateFormatter($dateTime, $format)
$dateTime = new \DateTime($dateTime);
}

if ($dateTime instanceof \DateTime) {
if ($dateTime instanceof \DateTimeInterface) {
static $utc;
if (!$utc) {
$utc = new \DateTimeZone('UTC');
Expand Down
23 changes: 23 additions & 0 deletions tests/Handler/ValidatedDescriptionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,27 @@ public function testValidatesAdditionalParameters()
$client = new GuzzleClient(new HttpClient(), $description);
$client->foo(['bar' => new \stdClass()]);
}

public function testFilterBeforeValidate()
{
$description = new Description([
'operations' => [
'foo' => [
'uri' => 'http://httpbin.org',
'httpMethod' => 'GET',
'parameters' => [
'bar' => [
'location' => 'uri',
'type' => 'string',
'format' => 'date-time',
'required' => true
]
]
]
]
]);

$client = new GuzzleClient(new HttpClient(), $description);
$client->foo(['bar' => new \DateTimeImmutable()]); // Should not throw any exception
}
}