From a057b3d3bf7b663fe8153dc2b41432af7b50c53b Mon Sep 17 00:00:00 2001 From: Bert Ramakers Date: Mon, 7 Oct 2024 16:19:01 +0200 Subject: [PATCH 1/2] Fix `multipleOf` bugs in Number type Fixes the multipleOf validation crashing when trying to use the modulo operator on a value that is zero, and an InvalidArgumentException when calling multipleOf() with `null` to reset it --- src/Schema/Type/Number.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Schema/Type/Number.php b/src/Schema/Type/Number.php index e52c6ab..000fbfe 100644 --- a/src/Schema/Type/Number.php +++ b/src/Schema/Type/Number.php @@ -50,7 +50,7 @@ public function validate(mixed $value, callable $fail): void } } - if ($this->multipleOf !== null && $value % $this->multipleOf !== 0) { + if ($this->multipleOf !== null && (float) $value !== 0.0 && $value % $this->multipleOf !== 0) { $fail(sprintf('must be a multiple of %d', $this->multipleOf)); } } @@ -85,7 +85,7 @@ public function maximum(?float $maximum, bool $exclusive = false): static public function multipleOf(?float $number): static { - if ($number <= 0) { + if ($number !== null && $number <= 0) { throw new InvalidArgumentException('multipleOf must be a positive number'); } From 22800912c5775544507816b959835c3b025508bf Mon Sep 17 00:00:00 2001 From: bertramakers Date: Mon, 7 Oct 2024 14:19:24 +0000 Subject: [PATCH 2/2] Run Prettier --- src/Schema/Type/Number.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Schema/Type/Number.php b/src/Schema/Type/Number.php index 000fbfe..d909dc1 100644 --- a/src/Schema/Type/Number.php +++ b/src/Schema/Type/Number.php @@ -50,7 +50,11 @@ public function validate(mixed $value, callable $fail): void } } - if ($this->multipleOf !== null && (float) $value !== 0.0 && $value % $this->multipleOf !== 0) { + if ( + $this->multipleOf !== null && + (float) $value !== 0.0 && + $value % $this->multipleOf !== 0 + ) { $fail(sprintf('must be a multiple of %d', $this->multipleOf)); } }