diff --git a/CHANGELOG-3.0.md b/CHANGELOG-3.0.md index cb8db4e85..c2d29e4e6 100644 --- a/CHANGELOG-3.0.md +++ b/CHANGELOG-3.0.md @@ -1,5 +1,9 @@ # v3.0.28 - TBD +## Added + +- [#5895](https://github.com/hyperf/hyperf/pull/5895) Added strict mode support for `Integer` and `Boolean`. + # v3.0.27 - 2023-06-30 ## Fixed diff --git a/src/validation/src/Concerns/ValidatesAttributes.php b/src/validation/src/Concerns/ValidatesAttributes.php index 99de105fc..edc22259e 100755 --- a/src/validation/src/Concerns/ValidatesAttributes.php +++ b/src/validation/src/Concerns/ValidatesAttributes.php @@ -201,10 +201,14 @@ trait ValidatesAttributes * * @param mixed $value */ - public function validateBoolean(string $attribute, $value): bool + public function validateBoolean(string $attribute, $value, array $parameters = []): bool { $acceptable = [true, false, 0, 1, '0', '1']; + if (isset($parameters[0]) && strtolower($parameters[0]) == 'strict') { + $acceptable = [true, false]; + } + return in_array($value, $acceptable, true); } @@ -681,8 +685,12 @@ trait ValidatesAttributes * * @param mixed $value */ - public function validateInteger(string $attribute, $value): bool + public function validateInteger(string $attribute, $value, array $parameters = []): bool { + if (isset($parameters[0]) && strtolower($parameters[0]) == 'strict' && gettype($value) != 'integer') { + return false; + } + return filter_var($value, FILTER_VALIDATE_INT) !== false; } diff --git a/src/validation/tests/Cases/ValidationValidatorTest.php b/src/validation/tests/Cases/ValidationValidatorTest.php index 59f0b2c21..833b6a680 100755 --- a/src/validation/tests/Cases/ValidationValidatorTest.php +++ b/src/validation/tests/Cases/ValidationValidatorTest.php @@ -1469,6 +1469,116 @@ class ValidationValidatorTest extends TestCase $this->assertTrue($v->passes()); } + public function testValidateIntStrict() + { + $translator = $this->getIlluminateArrayTranslator(); + + $validator = new Validator($translator, ['foo' => '1'], ['foo' => 'Int:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => '-1'], ['foo' => 'Int:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => '1.23'], ['foo' => 'Int:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => 1.23], ['foo' => 'Int:strict']); + $this->assertFalse($validator->passes()); + } + + public function testValidateIntegerStrict() + { + $translator = $this->getIlluminateArrayTranslator(); + + $validator = new Validator($translator, ['foo' => '1'], ['foo' => 'Integer:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => '-1'], ['foo' => 'Integer:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => '1.23'], ['foo' => 'Integer:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => 1.23], ['foo' => 'Integer:strict']); + $this->assertFalse($validator->passes()); + } + + public function testValidateBoolStrict() + { + $translator = $this->getIlluminateArrayTranslator(); + + $validator = new Validator($translator, ['foo' => 'no'], ['foo' => 'Bool:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => 'yes'], ['foo' => 'Bool:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => 'false'], ['foo' => 'Bool:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => 'true'], ['foo' => 'Bool:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, [], ['foo' => 'Bool:strict']); + $this->assertTrue($validator->passes()); + + $validator = new Validator($translator, ['foo' => false], ['foo' => 'Bool:strict']); + $this->assertTrue($validator->passes()); + + $validator = new Validator($translator, ['foo' => true], ['foo' => 'Bool:strict']); + $this->assertTrue($validator->passes()); + + $validator = new Validator($translator, ['foo' => '1'], ['foo' => 'Bool:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => 1], ['foo' => 'Bool:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => '0'], ['foo' => 'Bool:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => 0], ['foo' => 'Bool:strict']); + $this->assertFalse($validator->passes()); + } + + public function testValidateBooleanStrict() + { + $translator = $this->getIlluminateArrayTranslator(); + + $validator = new Validator($translator, ['foo' => 'no'], ['foo' => 'Boolean:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => 'yes'], ['foo' => 'Boolean:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => 'false'], ['foo' => 'Boolean:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => 'true'], ['foo' => 'Boolean:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, [], ['foo' => 'Boolean:strict']); + $this->assertTrue($validator->passes()); + + $validator = new Validator($translator, ['foo' => false], ['foo' => 'Boolean:strict']); + $this->assertTrue($validator->passes()); + + $validator = new Validator($translator, ['foo' => true], ['foo' => 'Boolean:strict']); + $this->assertTrue($validator->passes()); + + $validator = new Validator($translator, ['foo' => '1'], ['foo' => 'Boolean:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => 1], ['foo' => 'Boolean:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => '0'], ['foo' => 'Boolean:strict']); + $this->assertFalse($validator->passes()); + + $validator = new Validator($translator, ['foo' => 0], ['foo' => 'Boolean:strict']); + $this->assertFalse($validator->passes()); + } + public function testValidateDigits() { $trans = $this->getIlluminateArrayTranslator();