mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Support strict mode for Integer
and Boolean
(#5895)
Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
This commit is contained in:
parent
c679a703d6
commit
2cba3cfc2f
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user