Skip to content

Commit 3a3048e

Browse files
authored
Merge pull request #43 from rakit/strict-in-and-notin
Add method to enable strict checking for rules 'in' and 'not_in'
2 parents 16443e3 + 7dd06aa commit 3a3048e

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,29 @@ The field under this rule may have alpha-numeric characters, as well as dashes a
417417

418418
The field under this rule must be included in the given list of values.
419419

420+
This rule is using `in_array` to check the value.
421+
By default `in_array` disable strict checking.
422+
So it doesn't check data type.
423+
If you want enable strict checking, you can invoke validator like this:
424+
425+
```php
426+
$validation = $validator->validate($data, [
427+
'enabled' => [
428+
'required',
429+
$validator('in', [true, 1])->strict()
430+
]
431+
]);
432+
```
433+
434+
Then 'enabled' value should be boolean `true`, or int `1`.
435+
420436
<a id="rule-not_in"></a>
421437
#### not_in:value_1,value_2,...
422438

423439
The field under this rule must not be included in the given list of values.
424440

441+
This rule also using `in_array`. You can enable strict checking by invoking validator and call `strict()` like example in rule `in` above.
442+
425443
<a id="rule-min"></a>
426444
#### min:number
427445

src/Rules/In.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class In extends Rule
99

1010
protected $message = "The :attribute is not allowing :value";
1111

12+
protected $strict = false;
13+
1214
public function fillParameters(array $params)
1315
{
1416
if (count($params) == 1 AND is_array($params[0])) {
@@ -18,12 +20,17 @@ public function fillParameters(array $params)
1820
return $this;
1921
}
2022

23+
public function strict($strict = true)
24+
{
25+
$this->strict = $strict;
26+
}
27+
2128
public function check($value)
2229
{
2330
$this->requireParameters(['allowed_values']);
2431

2532
$allowed_values = $this->parameter('allowed_values');
26-
return in_array($value, $allowed_values);
33+
return in_array($value, $allowed_values, $this->strict);
2734
}
2835

2936
}

src/Rules/NotIn.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class NotIn extends Rule
99

1010
protected $message = "The :attribute is not allowing :value";
1111

12+
protected $strict = false;
13+
1214
public function fillParameters(array $params)
1315
{
1416
if (count($params) == 1 AND is_array($params[0])) {
@@ -18,11 +20,16 @@ public function fillParameters(array $params)
1820
return $this;
1921
}
2022

23+
public function strict($strict = true)
24+
{
25+
$this->strict = $strict;
26+
}
27+
2128
public function check($value)
2229
{
2330
$this->requireParameters(['disallowed_values']);
2431
$disallowed_values = (array) $this->parameter('disallowed_values');
25-
return !in_array($value, $disallowed_values);
32+
return !in_array($value, $disallowed_values, $this->strict);
2633
}
2734

2835
}

tests/Rules/InTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,16 @@ public function testInvalids()
2121
$this->assertFalse($this->rule->fillParameters([1,2,3])->check(4));
2222
}
2323

24+
public function testStricts()
25+
{
26+
// Not strict
27+
$this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(1));
28+
$this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(true));
29+
30+
// Strict
31+
$this->rule->strict();
32+
$this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(1));
33+
$this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(1));
34+
}
35+
2436
}

tests/Rules/NotInTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,16 @@ public function testInvalids()
2121
$this->assertFalse($this->rule->fillParameters(['bar', 'baz', 'qux'])->check('bar'));
2222
}
2323

24+
public function testStricts()
25+
{
26+
// Not strict
27+
$this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(1));
28+
$this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(true));
29+
30+
// Strict
31+
$this->rule->strict();
32+
$this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(1));
33+
$this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(1));
34+
}
35+
2436
}

0 commit comments

Comments
 (0)