diff --git a/src/Validation/RequestValidator.php b/src/Validation/RequestValidator.php index 719cea6..239e04d 100644 --- a/src/Validation/RequestValidator.php +++ b/src/Validation/RequestValidator.php @@ -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 */ diff --git a/tests/RequestValidatorTest.php b/tests/RequestValidatorTest.php index 3b074af..9ca98f9 100644 --- a/tests/RequestValidatorTest.php +++ b/tests/RequestValidatorTest.php @@ -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(); }