diff --git a/src/Validation.php b/src/Validation.php index 01dcc84..f9b3d33 100644 --- a/src/Validation.php +++ b/src/Validation.php @@ -160,7 +160,7 @@ private function getNestedParam($params = [], $keys = []) return $params; } else { $firstKey = array_shift($keys); - if (array_key_exists($firstKey, $params)) { + if ($this->isArrayLike($params) && array_key_exists($firstKey, $params)) { $params = (array) $params; $paramValue = $params[$firstKey]; @@ -171,6 +171,18 @@ private function getNestedParam($params = [], $keys = []) } } + /** + * Check if the given $params is an array like variable. + * + * @param array $params The variable to check. + * + * @return boolean Returns true if the given $params parameter is array like. + */ + private function isArrayLike($params) + { + return is_array($params) || $params instanceof \SimpleXMLElement; + } + /** * Check if there are any errors. * diff --git a/tests/ValidationTest.php b/tests/ValidationTest.php index 84e484c..5ffa81a 100644 --- a/tests/ValidationTest.php +++ b/tests/ValidationTest.php @@ -387,6 +387,37 @@ function ($message) { ), ), ), + //Nested complex JSON validation with errors + array( + array( + 'message' => array( + 'notification' => array( + 'title' => v::stringType()->length(1, null)->setName("notificationTitle"), + 'body' => v::stringType()->length(1, null)->setName("notificationBody"), + 'actionName' => v::optional(v::stringType()->length(1, null))->setName("notificationAction") + ) + ), + ), + null, + true, + array( + 'message.notification.title' => array( + 'notificationTitle must be a string', + 'notificationTitle must have a length greater than 1', + ), + 'message.notification.body' => array( + 'notificationBody must be a string', + 'notificationBody must have a length greater than 1', + ), + ), + 'JSON', + array( + 'message' => array( + 'notification' => 1, + ), + ), + ), + //XML validation without errors array( array( @@ -742,44 +773,4 @@ public function routeParamValidationProvider() ), ); } - - /* - public function testUseTemplate() - { - $this->setupGet(); - $hostnameValidator = v::regex('/^[a-zA-Z]([-.a-zA-Z0-9]{0,61}[a-zA-Z0-9]){0,1}$/')->setTemplate('Hostname {{name}} is not valid'); - $entryValidator = v::regex('/^[a-zA-Z]$/')->setTemplate('Entry {{name}} should contain only letters'); - $expectedValidators = array( - 'username' => $hostnameValidator, - 'age' => $entryValidator, - ); - $mw = new Validation($expectedValidators, null, ['useTemplate' => true]); - - $errors = null; - $hasErrors = null; - $validators = []; - $next = function ($req, $res) use (&$errors, &$hasErrors, &$validators) { - $errors = $req->getAttribute('errors'); - $hasErrors = $req->getAttribute('has_errors'); - $validators = $req->getAttribute('validators'); - - return $res; - }; - - $response = $mw($this->request, $this->response, $next); - - $expectedErrors = array( - 'username' => array( - 'Hostname "davidepastore" is not valid', - ), - 'age' => array( - 'Entry "89" should contain only letters', - ), - ); - - $this->assertTrue($hasErrors); - $this->assertEquals($expectedErrors, $errors); - $this->assertEquals($newValidators, $validators); - } - */ }