diff --git a/src/Tempest/Validation/src/Rules/IsFloat.php b/src/Tempest/Validation/src/Rules/IsFloat.php index c454af60b..49aa6b6b9 100644 --- a/src/Tempest/Validation/src/Rules/IsFloat.php +++ b/src/Tempest/Validation/src/Rules/IsFloat.php @@ -20,7 +20,12 @@ public function isValid(mixed $value): bool return true; } - return is_float($value); + if ($value === null || $value === false || $value === '' || $value === []) { + return false; + } + + // @mago-expect strictness/require-identity-comparison + return is_float($value) || floatval($value) == $value; } public function message(): string diff --git a/src/Tempest/Validation/src/Rules/IsInteger.php b/src/Tempest/Validation/src/Rules/IsInteger.php index 5c2e6d3f6..1a8fbfc64 100644 --- a/src/Tempest/Validation/src/Rules/IsInteger.php +++ b/src/Tempest/Validation/src/Rules/IsInteger.php @@ -20,7 +20,12 @@ public function isValid(mixed $value): bool return true; } - return is_int($value); + if ($value === null || $value === false || $value === '' || $value === []) { + return false; + } + + // @mago-expect strictness/require-identity-comparison + return is_int($value) || intval($value) == $value; } public function message(): string diff --git a/src/Tempest/Validation/src/Validator.php b/src/Tempest/Validation/src/Validator.php index 524448100..991764936 100644 --- a/src/Tempest/Validation/src/Validator.php +++ b/src/Tempest/Validation/src/Validator.php @@ -13,7 +13,6 @@ use Tempest\Validation\Rules\IsInteger; use Tempest\Validation\Rules\IsString; use Tempest\Validation\Rules\NotNull; -use TypeError; use function Tempest\Support\arr; diff --git a/src/Tempest/Validation/tests/Rules/BooleanTest.php b/src/Tempest/Validation/tests/Rules/IsBooleanTest.php similarity index 95% rename from src/Tempest/Validation/tests/Rules/BooleanTest.php rename to src/Tempest/Validation/tests/Rules/IsBooleanTest.php index 2e209b31c..87cc9b380 100644 --- a/src/Tempest/Validation/tests/Rules/BooleanTest.php +++ b/src/Tempest/Validation/tests/Rules/IsBooleanTest.php @@ -10,7 +10,7 @@ /** * @internal */ -final class BooleanTest extends TestCase +final class IsBooleanTest extends TestCase { public function test_boolean(): void { diff --git a/src/Tempest/Validation/tests/Rules/IsFloatTest.php b/src/Tempest/Validation/tests/Rules/IsFloatTest.php new file mode 100644 index 000000000..46ea03b37 --- /dev/null +++ b/src/Tempest/Validation/tests/Rules/IsFloatTest.php @@ -0,0 +1,28 @@ +assertTrue($rule->isValid(1)); + $this->assertTrue($rule->isValid(0.1)); + $this->assertTrue($rule->isValid('0.1')); + $this->assertFalse($rule->isValid('a')); + $this->assertFalse($rule->isValid('')); + $this->assertFalse($rule->isValid(null)); + $this->assertFalse($rule->isValid(false)); + $this->assertFalse($rule->isValid([])); + } +} diff --git a/src/Tempest/Validation/tests/Rules/IsIntegerTest.php b/src/Tempest/Validation/tests/Rules/IsIntegerTest.php new file mode 100644 index 000000000..d9be85ff8 --- /dev/null +++ b/src/Tempest/Validation/tests/Rules/IsIntegerTest.php @@ -0,0 +1,27 @@ +assertTrue($rule->isValid(1)); + $this->assertTrue($rule->isValid('1')); + $this->assertFalse($rule->isValid('a')); + $this->assertFalse($rule->isValid('')); + $this->assertFalse($rule->isValid(null)); + $this->assertFalse($rule->isValid(false)); + $this->assertFalse($rule->isValid([])); + } +} diff --git a/tests/Integration/Route/Fixtures/RequestWithTypedQueryParam.php b/tests/Integration/Route/Fixtures/RequestWithTypedQueryParam.php new file mode 100644 index 000000000..600ad52eb --- /dev/null +++ b/tests/Integration/Route/Fixtures/RequestWithTypedQueryParam.php @@ -0,0 +1,19 @@ +assertSame('hello', $request->queryParam); } + + public function test_query_params_with_types(): void + { + $request = map(new GenericRequest( + method: Method::GET, + uri: '/books?stringParam=a&intParam=1&floatParam=0.1&boolParam=1', + body: ['title' => 'Timeline Taxi'], + ))->with( + RequestToObjectMapper::class, + )->to(RequestWithTypedQueryParam::class); + + $this->assertSame(1, $request->intParam); + $this->assertSame('a', $request->stringParam); + $this->assertSame(true, $request->boolParam); + $this->assertSame(0.1, $request->floatParam); + } }