Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,29 @@ The field under this rule may have alpha-numeric characters, as well as dashes a

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

This rule is using `in_array` to check the value.
By default `in_array` disable strict checking.
So it doesn't check data type.
If you want enable strict checking, you can invoke validator like this:

```php
$validation = $validator->validate($data, [
'enabled' => [
'required',
$validator('in', [true, 1])->strict()
]
]);
```

Then 'enabled' value should be boolean `true`, or int `1`.

<a id="rule-not_in"></a>
#### not_in:value_1,value_2,...

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

This rule also using `in_array`. You can enable strict checking by invoking validator and call `strict()` like example in rule `in` above.

<a id="rule-min"></a>
#### min:number

Expand Down
9 changes: 8 additions & 1 deletion src/Rules/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class In extends Rule

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

protected $strict = false;

public function fillParameters(array $params)
{
if (count($params) == 1 AND is_array($params[0])) {
Expand All @@ -18,12 +20,17 @@ public function fillParameters(array $params)
return $this;
}

public function strict($strict = true)
{
$this->strict = $strict;
}

public function check($value)
{
$this->requireParameters(['allowed_values']);

$allowed_values = $this->parameter('allowed_values');
return in_array($value, $allowed_values);
return in_array($value, $allowed_values, $this->strict);
}

}
9 changes: 8 additions & 1 deletion src/Rules/NotIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class NotIn extends Rule

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

protected $strict = false;

public function fillParameters(array $params)
{
if (count($params) == 1 AND is_array($params[0])) {
Expand All @@ -18,11 +20,16 @@ public function fillParameters(array $params)
return $this;
}

public function strict($strict = true)
{
$this->strict = $strict;
}

public function check($value)
{
$this->requireParameters(['disallowed_values']);
$disallowed_values = (array) $this->parameter('disallowed_values');
return !in_array($value, $disallowed_values);
return !in_array($value, $disallowed_values, $this->strict);
}

}
12 changes: 12 additions & 0 deletions tests/Rules/InTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,16 @@ public function testInvalids()
$this->assertFalse($this->rule->fillParameters([1,2,3])->check(4));
}

public function testStricts()
{
// Not strict
$this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(1));
$this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(true));

// Strict
$this->rule->strict();
$this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(1));
$this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(1));
}

}
12 changes: 12 additions & 0 deletions tests/Rules/NotInTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,16 @@ public function testInvalids()
$this->assertFalse($this->rule->fillParameters(['bar', 'baz', 'qux'])->check('bar'));
}

public function testStricts()
{
// Not strict
$this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(1));
$this->assertFalse($this->rule->fillParameters(['1', '2', '3'])->check(true));

// Strict
$this->rule->strict();
$this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(1));
$this->assertTrue($this->rule->fillParameters(['1', '2', '3'])->check(1));
}

}