Skip to content

Commit 7705cbc

Browse files
Merge pull request #280 from apapsch/master
Create arrayHasKeys validator.
2 parents c6893fc + 2dc2f29 commit 7705cbc

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ V::lang('ar');
167167
* `creditCard` - Field is a valid credit card number
168168
* `instanceOf` - Field contains an instance of the given class
169169
* `optional` - Value does not need to be included in data array. If it is however, it must pass validation.
170+
* `arrayHasKeys` - Field is an array and contains all specified keys.
170171

171172
**NOTE**: If you are comparing floating-point numbers with min/max validators, you
172173
should install the [BCMath](http://us3.php.net/manual/en/book.bc.php)
@@ -938,6 +939,23 @@ $v->rules([
938939
$v->validate();
939940
```
940941

942+
## arrayHasKeys fields usage
943+
944+
The `arrayHasKeys` rule ensures that the field is an array and that it contains all the specified keys.
945+
Returns false if the field is not an array or if no required keys are specified or if some key is missing.
946+
947+
```php
948+
$v = new Valitron\Validator([
949+
'address' => [
950+
'name' => 'Jane Doe',
951+
'street' => 'Doe Square',
952+
'city' => 'Doe D.C.'
953+
]
954+
]);
955+
$v->rule(['arrayHasKeys', 'address', ['name', 'street', 'city']);
956+
$v->validate();
957+
```
958+
941959
## Adding Custom Validation Rules
942960

943961
To add your own validation rule, use the `addRule` method with a rule

lang/en.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@
3535
'instanceOf' => "must be an instance of '%s'",
3636
'containsUnique' => "must contain unique elements only",
3737
'subset' => "contains an item that is not in the list",
38+
'arrayHasKeys' => "does not contain all required keys",
3839
);

src/Valitron/Validator.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,23 @@ protected function validateOptional($field, $value, $params)
911911
return true;
912912
}
913913

914+
protected function validateArrayHasKeys($field, $value, $params)
915+
{
916+
if (!is_array($value) || !isset($params[0])) {
917+
return false;
918+
}
919+
$requiredFields = $params[0];
920+
if (count($requiredFields) === 0) {
921+
return false;
922+
}
923+
foreach ($requiredFields as $fieldName) {
924+
if (!array_key_exists($fieldName, $value)) {
925+
return false;
926+
}
927+
}
928+
return true;
929+
}
930+
914931
/**
915932
* Get array of fields and data
916933
*

tests/Valitron/ValidateTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,6 +2483,72 @@ public function testRequiredArrayPartsAreNotIgnored()
24832483
$v->rule('date', 'data.*.foo');
24842484
$this->assertFalse($v->validate());
24852485
}
2486+
2487+
public function testArrayHasKeysTrueIfAllFieldsExist()
2488+
{
2489+
$v = new Validator(array(
2490+
'address' => array(
2491+
'name' => 'Jane Doe',
2492+
'street' => 'Doe Square',
2493+
'city' => 'Doe D.C.'
2494+
)
2495+
));
2496+
$v->rule('arrayHasKeys', 'address', array('name', 'street', 'city'));
2497+
$this->assertTrue($v->validate());
2498+
}
2499+
2500+
public function testArrayHasKeysFalseOnMissingField()
2501+
{
2502+
$v = new Validator(array(
2503+
'address' => array(
2504+
'name' => 'Jane Doe',
2505+
'street' => 'Doe Square'
2506+
)
2507+
));
2508+
$v->rule('arrayHasKeys', 'address', array('name', 'street', 'city'));
2509+
$this->assertFalse($v->validate());
2510+
}
2511+
2512+
public function testArrayHasKeysFalseOnNonArray()
2513+
{
2514+
$v = new Validator(array(
2515+
'address' => 'Jane Doe, Doe Square'
2516+
));
2517+
$v->rule('arrayHasKeys', 'address', array('name', 'street', 'city'));
2518+
$this->assertFalse($v->validate());
2519+
}
2520+
2521+
public function testArrayHasKeysFalseOnEmptyRequiredFields()
2522+
{
2523+
$v = new Validator(array(
2524+
'address' => array(
2525+
'lat' => 77.547,
2526+
'lon' => 16.337
2527+
)
2528+
));
2529+
$v->rule('arrayHasKeys', 'address', array());
2530+
$this->assertFalse($v->validate());
2531+
}
2532+
2533+
public function testArrayHasKeysFalseOnUnspecifiedRequiredFields()
2534+
{
2535+
$v = new Validator(array(
2536+
'address' => array(
2537+
'lat' => 77.547,
2538+
'lon' => 16.337
2539+
)
2540+
));
2541+
$v->rule('arrayHasKeys', 'address');
2542+
$this->assertFalse($v->validate());
2543+
}
2544+
2545+
public function testArrayHasKeysTrueIfMissingAndOptional()
2546+
{
2547+
$v = new Validator(array());
2548+
$v->rule('arrayHasKeys', 'address', array('name', 'street', 'city'));
2549+
$v->rule('optional', 'address');
2550+
$this->assertTrue($v->validate());
2551+
}
24862552
}
24872553

24882554
function sampleFunctionCallback($field, $value, array $params)

0 commit comments

Comments
 (0)