Skip to content

Commit 5064292

Browse files
authored
Allows for strict boolean validation (#56313)
1 parent 2784ca1 commit 5064292

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,12 +486,17 @@ public function validateBetween($attribute, $value, $parameters)
486486
*
487487
* @param string $attribute
488488
* @param mixed $value
489+
* @param array{0: 'strict'} $parameters
489490
* @return bool
490491
*/
491-
public function validateBoolean($attribute, $value)
492+
public function validateBoolean($attribute, $value, $parameters)
492493
{
493494
$acceptable = [true, false, 0, 1, '0', '1'];
494495

496+
if (($parameters[0] ?? null) === 'strict') {
497+
$acceptable = [true, false];
498+
}
499+
495500
return in_array($value, $acceptable, true);
496501
}
497502

tests/Validation/ValidationValidatorTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3116,6 +3116,44 @@ public function testValidateBoolean()
31163116
$this->assertTrue($v->passes());
31173117
}
31183118

3119+
public function testValidateBooleanStrict()
3120+
{
3121+
$trans = $this->getIlluminateArrayTranslator();
3122+
3123+
$v = new Validator($trans, ['foo' => false], ['foo' => 'Boolean:strict']);
3124+
$this->assertTrue($v->passes());
3125+
3126+
$v = new Validator($trans, ['foo' => true], ['foo' => 'Boolean:strict']);
3127+
$this->assertTrue($v->passes());
3128+
3129+
$v = new Validator($trans, ['foo' => 'no'], ['foo' => 'Boolean:strict']);
3130+
$this->assertFalse($v->passes());
3131+
3132+
$v = new Validator($trans, ['foo' => 'yes'], ['foo' => 'Boolean:strict']);
3133+
$this->assertFalse($v->passes());
3134+
3135+
$v = new Validator($trans, ['foo' => 'false'], ['foo' => 'Boolean:strict']);
3136+
$this->assertFalse($v->passes());
3137+
3138+
$v = new Validator($trans, ['foo' => 'true'], ['foo' => 'Boolean:strict']);
3139+
$this->assertFalse($v->passes());
3140+
3141+
$v = new Validator($trans, [], ['foo' => 'Boolean:strict']);
3142+
$this->assertTrue($v->passes());
3143+
3144+
$v = new Validator($trans, ['foo' => '1'], ['foo' => 'Boolean:strict']);
3145+
$this->assertFalse($v->passes());
3146+
3147+
$v = new Validator($trans, ['foo' => 1], ['foo' => 'Boolean:strict']);
3148+
$this->assertFalse($v->passes());
3149+
3150+
$v = new Validator($trans, ['foo' => '0'], ['foo' => 'Boolean:strict']);
3151+
$this->assertFalse($v->passes());
3152+
3153+
$v = new Validator($trans, ['foo' => 0], ['foo' => 'Boolean:strict']);
3154+
$this->assertFalse($v->passes());
3155+
}
3156+
31193157
public function testValidateBool()
31203158
{
31213159
$trans = $this->getIlluminateArrayTranslator();
@@ -3153,6 +3191,44 @@ public function testValidateBool()
31533191
$this->assertTrue($v->passes());
31543192
}
31553193

3194+
public function testValidateBoolStrict()
3195+
{
3196+
$trans = $this->getIlluminateArrayTranslator();
3197+
3198+
$v = new Validator($trans, ['foo' => false], ['foo' => 'Bool:strict']);
3199+
$this->assertTrue($v->passes());
3200+
3201+
$v = new Validator($trans, ['foo' => true], ['foo' => 'Bool:strict']);
3202+
$this->assertTrue($v->passes());
3203+
3204+
$v = new Validator($trans, ['foo' => 'no'], ['foo' => 'Bool:strict']);
3205+
$this->assertFalse($v->passes());
3206+
3207+
$v = new Validator($trans, ['foo' => 'yes'], ['foo' => 'Bool']);
3208+
$this->assertFalse($v->passes());
3209+
3210+
$v = new Validator($trans, ['foo' => 'false'], ['foo' => 'Bool:strict']);
3211+
$this->assertFalse($v->passes());
3212+
3213+
$v = new Validator($trans, ['foo' => 'true'], ['foo' => 'Bool:strict']);
3214+
$this->assertFalse($v->passes());
3215+
3216+
$v = new Validator($trans, [], ['foo' => 'Bool:strict']);
3217+
$this->assertTrue($v->passes());
3218+
3219+
$v = new Validator($trans, ['foo' => '1'], ['foo' => 'Bool:strict']);
3220+
$this->assertFalse($v->passes());
3221+
3222+
$v = new Validator($trans, ['foo' => 1], ['foo' => 'Bool:strict']);
3223+
$this->assertFalse($v->passes());
3224+
3225+
$v = new Validator($trans, ['foo' => '0'], ['foo' => 'Bool:strict']);
3226+
$this->assertFalse($v->passes());
3227+
3228+
$v = new Validator($trans, ['foo' => 0], ['foo' => 'Bool:strict']);
3229+
$this->assertFalse($v->passes());
3230+
}
3231+
31563232
public function testValidateNumeric()
31573233
{
31583234
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)