Skip to content

Commit

Permalink
Check data before merging (#91)
Browse files Browse the repository at this point in the history
* try to map parameters to their types

* Update RequestValidator.php

* check data before casting

* Refactoring

* style ci

Co-authored-by: Adam Campbell <adam@hotmeteor.com>
Co-authored-by: Bastien Philippe <bastien.philippe@soyhuce.fr>
  • Loading branch information
3 people authored Oct 24, 2022
1 parent 7109add commit 6fdc6df
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Validation/RequestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,30 @@ protected function validateParameters()
}
}

/**
* @param mixed $parameter
* @param string|null $type
* @return mixed
*/
private function castParameter($parameter, ?string $type)
{
if ($type === null) {
return $parameter;
}

if ($type === 'integer' && filter_var($parameter, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE) !== null) {
return (int) $parameter;
} elseif ($type === 'number' && filter_var($parameter, FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE) !== null) {
return (float) $parameter;
} elseif ($type === 'boolean' && filter_var($parameter, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null) {
return filter_var($parameter, FILTER_VALIDATE_BOOLEAN);
} elseif ($type === 'string') {
return (string) $parameter;
}

return $parameter;
}

/**
* @throws RequestValidationException|SchemaValidationException
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/RequestValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,9 @@ public function test_handles_query_parameters_int(): void
return [];
})->middleware(Middleware::class);

$this->getJson('/users-by-id/foo')
->assertInvalidRequest();

$this->getJson('/users-by-id/1')
->assertValidRequest();
}
Expand Down

0 comments on commit 6fdc6df

Please sign in to comment.