From d0b404257ece5624ba9c5a3c831259572a87a89a Mon Sep 17 00:00:00 2001 From: Mihail Nikolov Date: Wed, 31 Oct 2018 14:12:03 +0200 Subject: [PATCH 1/3] Validation of array of multiple identical objects --- src/Validation.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Validation.php b/src/Validation.php index 3a31572..8547127 100644 --- a/src/Validation.php +++ b/src/Validation.php @@ -124,7 +124,17 @@ private function validate($params = [], $validators = [], $actualKeys = []) $actualKeys[] = $key; $param = $this->getNestedParam($params, $actualKeys); if (is_array($validator)) { - $this->validate($params, $validator, $actualKeys); + if ($key === "*") { + $arrayKeys = array_keys($params[$actualKeys[0]]); + $innerActualKeys = array_splice($actualKeys, 0, -1); + foreach ($arrayKeys as $arrayKey) { + $innerActualKeys[] = $arrayKey; + $this->validate($params, $validator, $innerActualKeys); + array_pop($innerActualKeys); + } + } else { + $this->validate($params, $validator, $actualKeys); + } } else { try { $validator->assert($param); From d7fa8ea7fbcce76a5c7b45e1ddef4ac9c5c36226 Mon Sep 17 00:00:00 2001 From: Mihail Nikolov Date: Wed, 31 Oct 2018 14:12:27 +0200 Subject: [PATCH 2/3] [TESTS] Validation of array of multiple identical objects --- tests/ValidationTest.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/ValidationTest.php b/tests/ValidationTest.php index cb3e7f3..6d4f356 100644 --- a/tests/ValidationTest.php +++ b/tests/ValidationTest.php @@ -417,6 +417,34 @@ function ($message) { ), ), ), + //Multiple nested JSON validation with errors + array( + array( + 'messages' => array( + '*' => array( + 'title' => v::stringType()->length(6, null)->setName("messageTitle") + ) + ), + ), + null, + true, + array( + 'messages.0.title' => array( + 'messageTitle must have a length greater than 6', + ), + ), + 'JSON', + array( + 'messages' => array( + array( + 'title' => 'Title' + ), + array( + 'title' => 'Long title' + ), + ), + ), + ), //XML validation without errors array( From 091c3add421568ccf6838ba1e8268f0b1a85b1f3 Mon Sep 17 00:00:00 2001 From: Mihail Nikolov Date: Wed, 31 Oct 2018 14:13:26 +0200 Subject: [PATCH 3/3] Readme updated with asterisk sign support --- README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/README.md b/README.md index 7ed0daa..7d96ebc 100644 --- a/README.md +++ b/README.md @@ -227,7 +227,44 @@ Array ) */ ``` +And you can also validate multiple identical objects into array using asterisk sign in this way: +```php +use Respect\Validation\Validator as v; + +$app = new \Slim\App(); + +//Create the validators +$nameValidator = v::alnum()->length(1, 100); +$ageValidator = v::numeric()->between(0, 120); +$validators = array( + 'people' => array( + '*' => array( + 'name' => $nameValidator, + 'age' => $ageValidator, + ) + ), +); +``` + +If you'll have an error in some of objects, the result would be like this where **2** is the index of the object into *people* array: + +```php +//In your route +$errors = $req->getAttribute('errors'); + +print_r($errors); +/* +Array +( + [people.2.age] => Array + ( + [0] => "180 must be less than or equal to 120" + ) + +) +*/ +``` ### XML requests