Merge pull request #280 from apapsch/master

Create arrayHasKeys validator.
This commit is contained in:
Willem Wollebrants 2019-03-21 21:02:08 +01:00 committed by GitHub
commit 7705cbc5a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 102 additions and 0 deletions

View File

@ -167,6 +167,7 @@ V::lang('ar');
* `creditCard` - Field is a valid credit card number
* `instanceOf` - Field contains an instance of the given class
* `optional` - Value does not need to be included in data array. If it is however, it must pass validation.
* `arrayHasKeys` - Field is an array and contains all specified keys.
**NOTE**: If you are comparing floating-point numbers with min/max validators, you
should install the [BCMath](http://us3.php.net/manual/en/book.bc.php)
@ -938,6 +939,23 @@ $v->rules([
$v->validate();
```
## arrayHasKeys fields usage
The `arrayHasKeys` rule ensures that the field is an array and that it contains all the specified keys.
Returns false if the field is not an array or if no required keys are specified or if some key is missing.
```php
$v = new Valitron\Validator([
'address' => [
'name' => 'Jane Doe',
'street' => 'Doe Square',
'city' => 'Doe D.C.'
]
]);
$v->rule(['arrayHasKeys', 'address', ['name', 'street', 'city']);
$v->validate();
```
## Adding Custom Validation Rules
To add your own validation rule, use the `addRule` method with a rule

View File

@ -35,4 +35,5 @@ return array(
'instanceOf' => "must be an instance of '%s'",
'containsUnique' => "must contain unique elements only",
'subset' => "contains an item that is not in the list",
'arrayHasKeys' => "does not contain all required keys",
);

View File

@ -911,6 +911,23 @@ class Validator
return true;
}
protected function validateArrayHasKeys($field, $value, $params)
{
if (!is_array($value) || !isset($params[0])) {
return false;
}
$requiredFields = $params[0];
if (count($requiredFields) === 0) {
return false;
}
foreach ($requiredFields as $fieldName) {
if (!array_key_exists($fieldName, $value)) {
return false;
}
}
return true;
}
/**
* Get array of fields and data
*

View File

@ -2483,6 +2483,72 @@ class ValidateTest extends BaseTestCase
$v->rule('date', 'data.*.foo');
$this->assertFalse($v->validate());
}
public function testArrayHasKeysTrueIfAllFieldsExist()
{
$v = new Validator(array(
'address' => array(
'name' => 'Jane Doe',
'street' => 'Doe Square',
'city' => 'Doe D.C.'
)
));
$v->rule('arrayHasKeys', 'address', array('name', 'street', 'city'));
$this->assertTrue($v->validate());
}
public function testArrayHasKeysFalseOnMissingField()
{
$v = new Validator(array(
'address' => array(
'name' => 'Jane Doe',
'street' => 'Doe Square'
)
));
$v->rule('arrayHasKeys', 'address', array('name', 'street', 'city'));
$this->assertFalse($v->validate());
}
public function testArrayHasKeysFalseOnNonArray()
{
$v = new Validator(array(
'address' => 'Jane Doe, Doe Square'
));
$v->rule('arrayHasKeys', 'address', array('name', 'street', 'city'));
$this->assertFalse($v->validate());
}
public function testArrayHasKeysFalseOnEmptyRequiredFields()
{
$v = new Validator(array(
'address' => array(
'lat' => 77.547,
'lon' => 16.337
)
));
$v->rule('arrayHasKeys', 'address', array());
$this->assertFalse($v->validate());
}
public function testArrayHasKeysFalseOnUnspecifiedRequiredFields()
{
$v = new Validator(array(
'address' => array(
'lat' => 77.547,
'lon' => 16.337
)
));
$v->rule('arrayHasKeys', 'address');
$this->assertFalse($v->validate());
}
public function testArrayHasKeysTrueIfMissingAndOptional()
{
$v = new Validator(array());
$v->rule('arrayHasKeys', 'address', array('name', 'street', 'city'));
$v->rule('optional', 'address');
$this->assertTrue($v->validate());
}
}
function sampleFunctionCallback($field, $value, array $params)