[TASK] Improve input validation for nested elements
This commit is contained in:
parent
fbd07a1545
commit
7ed00c1bbb
@ -234,6 +234,29 @@ class ActionController extends BaseController
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function arrayRemoveEmptyStrings($array)
|
||||||
|
{
|
||||||
|
foreach ($array as $key => &$value) {
|
||||||
|
if (is_array($value)) {
|
||||||
|
$value = $this->arrayRemoveEmptyStrings($value);
|
||||||
|
} else {
|
||||||
|
if (is_string($value) && !strlen($value)) {
|
||||||
|
unset($array[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unset($value);
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function arrayToObject($array) {
|
||||||
|
if (is_array($array)) {
|
||||||
|
return (object) array_map([__CLASS__, __METHOD__], $array);
|
||||||
|
} else {
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* validate objects
|
* validate objects
|
||||||
*
|
*
|
||||||
@ -244,14 +267,10 @@ class ActionController extends BaseController
|
|||||||
protected function validateInput($input, $schema)
|
protected function validateInput($input, $schema)
|
||||||
{
|
{
|
||||||
$validator = new Validator();
|
$validator = new Validator();
|
||||||
$input = array_filter($input, function($element) {
|
$input = $this->arrayRemoveEmptyStrings($input);
|
||||||
if (is_string($element) && !strlen($element)) {
|
$input = $this->arrayToObject($input);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return $element;
|
|
||||||
});
|
|
||||||
$validationResult = $validator->dataValidation(
|
$validationResult = $validator->dataValidation(
|
||||||
(object)$input,
|
$input,
|
||||||
json_encode($schema),
|
json_encode($schema),
|
||||||
-1
|
-1
|
||||||
);
|
);
|
||||||
@ -259,9 +278,11 @@ class ActionController extends BaseController
|
|||||||
$this->responseStatus = [400 => 'validationError'];
|
$this->responseStatus = [400 => 'validationError'];
|
||||||
foreach ($validationResult->getErrors() as $error){
|
foreach ($validationResult->getErrors() as $error){
|
||||||
$errorLabel = null;
|
$errorLabel = null;
|
||||||
$field = $error->dataPointer()[0];
|
$field = implode('.', $error->dataPointer());
|
||||||
if ($error->keyword() == 'required') {
|
if ($error->keyword() == 'required') {
|
||||||
$field = ($error->keywordArgs()['missing']);
|
$tmp = $error->dataPointer();
|
||||||
|
array_push($tmp, $error->keywordArgs()['missing']);
|
||||||
|
$field = implode('.', $tmp);
|
||||||
}
|
}
|
||||||
if ($error->keyword() == 'additionalProperties') {
|
if ($error->keyword() == 'additionalProperties') {
|
||||||
continue;
|
continue;
|
||||||
@ -287,9 +308,8 @@ class ActionController extends BaseController
|
|||||||
}
|
}
|
||||||
$this->errorLabels[$field] = $errorLabel;
|
$this->errorLabels[$field] = $errorLabel;
|
||||||
} else {
|
} else {
|
||||||
foreach ($error->keywordArgs() as $arg) {
|
|
||||||
$errorLabel = $this->getTranslation(
|
$errorLabel = $this->getTranslation(
|
||||||
'error.' . $arg . '.required'
|
'error.' . $field . '.required'
|
||||||
);
|
);
|
||||||
if ($errorLabel == null) {
|
if ($errorLabel == null) {
|
||||||
$errorLabel = 'error.'
|
$errorLabel = 'error.'
|
||||||
@ -297,8 +317,7 @@ class ActionController extends BaseController
|
|||||||
. '.'
|
. '.'
|
||||||
. $error->keyword();
|
. $error->keyword();
|
||||||
}
|
}
|
||||||
$this->errorLabels[$arg] = $errorLabel;
|
$this->errorLabels[$field] = $errorLabel;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user