Skip to content

Commit

Permalink
Merge pull request #33 from DavidePastore/fix-32
Browse files Browse the repository at this point in the history
Support Nested Object Validation
  • Loading branch information
DavidePastore authored Aug 14, 2018
2 parents 8e4b105 + 0114f84 commit c28d6e2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
14 changes: 13 additions & 1 deletion src/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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.
*
Expand Down
71 changes: 31 additions & 40 deletions tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
*/
}

0 comments on commit c28d6e2

Please sign in to comment.