[TASK] Sepeate validation from ActionController

This commit is contained in:
Philipp Dieter 2021-03-15 16:54:03 +01:00
parent a3cb4325bf
commit 329fa6d313
2 changed files with 199 additions and 93 deletions

View File

@ -12,9 +12,7 @@ namespace Cjel\TemplatesAide\Controller;
* *
***/ ***/
use \Opis\JsonSchema\{ use Cjel\TemplatesAide\Traits\ValidationTrait;
Validator, ValidationResult, ValidationError, Schema
};
use TYPO3\CMS\Core\Cache\CacheManager; use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Log\LogManager; use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
@ -30,6 +28,10 @@ use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
class ActionController extends BaseController class ActionController extends BaseController
{ {
use ValidationTrait {
validateAgainstSchema as traitValidateAgainstSchema;
}
/* /*
* page type * page type
*/ */
@ -96,11 +98,6 @@ class ActionController extends BaseController
*/ */
protected $redirect = null; protected $redirect = null;
/**
* is valid
*/
protected $isValid = true;
/** /**
* errors * errors
*/ */
@ -390,106 +387,93 @@ 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 * legacy function to prevent beaking old code
* *
* @param $input * @deprecated
* @param schema
* @return void
*/ */
protected function validateInput($input, $schema) protected function validateInput($input, $schema)
{ {
$validator = new Validator(); return $this->validateAgainstSchema($input, $schema, true);
$input = $this->arrayRemoveEmptyStrings($input); }
//@todo make optional when usiing rest api
//array_walk_recursive( /**
// $input, * validate input and translate error messages
// function (&$value) { */
// if (filter_var($value, FILTER_VALIDATE_INT)) { protected function validateAgainstSchema(
// $value = (int)$value; $input, $schema, $translate = false
// } ) {
// } $validationResult = $this->traitValidateAgainstSchema(
//);
$input = $this->arrayToObject($input);
$validationResult = $validator->dataValidation(
$input, $input,
json_encode($schema), $schema
-1
); );
if (!$validationResult->isValid()) { if (!$validationResult->isValid()) {
$this->isValid = false; //foreach ($validationResult->getErrors() as $error){
$this->responseStatus = [400 => 'validationError']; // $field = implode('.', $error->dataPointer());
foreach ($validationResult->getErrors() as $error){ // if ($error->keyword() == 'required') {
$errorLabel = null; // $tmp = $error->dataPointer();
$field = implode('.', $error->dataPointer()); // array_push($tmp, $error->keywordArgs()['missing']);
if ($error->keyword() == 'required') { // $field = implode('.', $tmp);
$tmp = $error->dataPointer(); // }
array_push($tmp, $error->keywordArgs()['missing']); // if ($error->keyword() == 'additionalProperties') {
$field = implode('.', $tmp); // continue;
} // }
if ($error->keyword() == 'additionalProperties') { // $this->errors[$field] = [
continue; // 'keyword' => $error->keyword(),
} // 'details' => $error->keywordArgs()
$this->errors[$field] = [ // ];
'keyword' => $error->keyword(), //}
'details' => $error->keywordArgs() if ($translate) {
]; $this->translateErrorMessages($validationResult);
if ($error->keyword() != 'required') {
$errorLabel = $this->getTranslation(
'error.' . $field . '.' . $error->keyword()
);
//if ($errorLabel == null) {
// $errorLabel = $this->getTranslation(
// 'error.' . $field . '.required'
// );
//}
if ($errorLabel == null) {
$errorLabel = 'error.'
. $field
. '.'
. $error->keyword();
}
$this->errorLabels[$field] = $errorLabel;
} else {
$errorLabel = $this->getTranslation(
'error.' . $field . '.required'
);
if ($errorLabel == null) {
$errorLabel = 'error.'
. $field
. '.'
. $error->keyword();
}
$this->errorLabels[$field] = $errorLabel;
}
} }
} }
return $validationResult->isValid(); return $validationResult->isValid();
} }
/**
* translate error messages to user readable strings
*/
protected function translateErrorMessages($validationResult)
{
foreach ($validationResult->getErrors() as $error){
$errorLabel = null;
$field = implode('.', $error->dataPointer());
if ($error->keyword() == 'required') {
$tmp = $error->dataPointer();
array_push($tmp, $error->keywordArgs()['missing']);
$field = implode('.', $tmp);
}
if ($error->keyword() == 'additionalProperties') {
continue;
}
if ($error->keyword() != 'required') {
$errorLabel = $this->getTranslation(
'error.' . $field . '.' . $error->keyword()
);
if ($errorLabel == null) {
$errorLabel = 'error.'
. $field
. '.'
. $error->keyword();
}
$this->errorLabels[$field] = $errorLabel;
} else {
$errorLabel = $this->getTranslation(
'error.' . $field . '.required'
);
if ($errorLabel == null) {
$errorLabel = 'error.'
. $field
. '.'
. $error->keyword();
}
$this->errorLabels[$field] = $errorLabel;
}
}
}
/** /**
* returns plugin namespace to build js post request * returns plugin namespace to build js post request
* *

View File

@ -0,0 +1,122 @@
<?php
namespace Cjel\TemplatesAide\Traits;
/***
*
* This file is part of the "Templates Aide" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2021 Philipp Dieter <philippdieter@attic-media.net>
*
***/
use \Opis\JsonSchema\{
Validator, ValidationResult, ValidationError, Schema
};
/**
* ValidationTrait
*/
trait ValidationTrait
{
/**
* is valid
*/
protected $isValid = true;
/**
* errors
*/
protected $errors = [];
/**
* errors labels
*/
protected $errorLabels = [];
/**
* validate objects
*
* @param $input
* @param schema
* @return void
*/
protected function validateAgainstSchema($input, $schema)
{
$validator = new Validator();
$input = $this->arrayRemoveEmptyStrings($input);
//@TODO make optional when usiing rest api
//array_walk_recursive(
// $input,
// function (&$value) {
// if (filter_var($value, FILTER_VALIDATE_INT)) {
// $value = (int)$value;
// }
// }
//);
$input = $this->arrayToObject($input);
$validationResult = $validator->dataValidation(
$input,
json_encode($schema),
-1
);
if (!$validationResult->isValid()) {
$this->isValid = false;
$this->responseStatus = [400 => 'validationError'];
foreach ($validationResult->getErrors() as $error){
$field = implode('.', $error->dataPointer());
if ($error->keyword() == 'required') {
$tmp = $error->dataPointer();
array_push($tmp, $error->keywordArgs()['missing']);
$field = implode('.', $tmp);
}
if ($error->keyword() == 'additionalProperties') {
foreach ($error->subErrors() as $subError) {
$this->errors[$subError->dataPointer()[0]] = [
'keyword' => 'superfluos',
];
}
} else {
$this->errors[$field] = [
'keyword' => $error->keyword(),
'details' => $error->keywordArgs()
];
}
}
}
return $validationResult;
}
/**
* remove empty strings
*/
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;
}
/**
* function arrayTObject
*/
public static function arrayToObject($array) {
if (is_array($array)) {
return (object) array_map([__CLASS__, __METHOD__], $array);
} else {
return $array;
}
}
}