diff --git a/README.md b/README.md index 9eb0f82..65c247d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lang/en.php b/lang/en.php index 0c9e5a1..564238a 100644 --- a/lang/en.php +++ b/lang/en.php @@ -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", ); diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 93c06be..5647517 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -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 * diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index 169c0e6..fc9f229 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -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)