Compare commits
160 commits
version/1.
...
master
Author | SHA1 | Date | |
---|---|---|---|
a0488daa41 | |||
230677d289 | |||
ad973f63ed | |||
1f5b491d5a | |||
3b64e5b87b | |||
cf710783d9 | |||
da4f409a8d | |||
b86110a3b0 | |||
04d1810bb2 | |||
a3209c6976 | |||
f452efae74 | |||
a213057567 | |||
1e7a319604 | |||
|
a94e3a3291 | ||
|
09f6aa089b | ||
|
0e5a09e45a | ||
|
0ad084f734 | ||
7c0c667dd3 | |||
01bb260444 | |||
eff9a13d22 | |||
1d09aeeced | |||
25d5a3c21f | |||
6beb9f1997 | |||
2162238cc6 | |||
bd503834d7 | |||
86cf0c546b | |||
|
1fd6480cd2 | ||
|
c09b13c124 | ||
|
fb9f31ccee | ||
|
3087390299 | ||
|
d4e095dd54 | ||
|
9707c47a1c | ||
|
9c4138b4d1 | ||
|
51115825ac | ||
|
26a71d266d | ||
|
c4c3d94fd9 | ||
|
ba7e6ce33f | ||
|
a1882a2b11 | ||
|
d03d940f6a | ||
|
3fab89e54b | ||
|
bd7fca349c | ||
|
75101c2147 | ||
|
f857055059 | ||
|
591886bfdf | ||
|
20cf7bff05 | ||
|
9610c2a27e | ||
|
b01aa11242 | ||
|
e5aed7a587 | ||
|
c5e511fecc | ||
|
eada129ce9 | ||
|
3426886a56 | ||
|
849c87ce6f | ||
|
ba6f572cec | ||
|
82a68be97d | ||
|
42cefc7ced | ||
|
1775021854 | ||
|
5437fb29bd | ||
|
5ab1085590 | ||
|
937e74671c | ||
|
651f5815a4 | ||
|
e311c202bc | ||
|
efdb8560a1 | ||
|
8c9790b662 | ||
|
0de085c5cf | ||
|
ca1ec5707f | ||
|
c6e7d549c1 | ||
|
bb8ec70593 | ||
|
8f6fbf9058 | ||
|
8eea5e9b15 | ||
|
f33647559a | ||
|
a81c1bfe35 | ||
|
000c301d25 | ||
|
c6e23fdf35 | ||
|
272f78f96e | ||
|
e4abe5a96c | ||
|
72904639a4 | ||
|
362b37b6d6 | ||
|
8d76072227 | ||
|
e141dbddee | ||
|
f20c3ba090 | ||
|
8cc705bd48 | ||
|
743ec61c44 | ||
|
08e6ad00d4 | ||
|
8b0e5640b9 | ||
|
d30ed3310a | ||
|
00f7e385d9 | ||
|
72789243f0 | ||
|
c528658f82 | ||
|
20e891ac91 | ||
|
befaf99d9f | ||
|
e8c94b15a1 | ||
|
87544c3976 | ||
|
a73f3545ea | ||
|
11425866a1 | ||
|
2bbebd9419 | ||
|
5f77512296 | ||
|
df77fcf75a | ||
|
8b4bf2b09b | ||
|
d16ca767c1 | ||
|
40aaaa930b | ||
|
6fa2343816 | ||
|
c721f78f08 | ||
|
5e5c6bfb9d | ||
|
14a6ebbd00 | ||
|
4dd8d9026b | ||
|
3cac00f12a | ||
409efffe45 | |||
|
1d27166706 | ||
9c09eacaea | |||
0f9c5f4697 | |||
4a6d344ab6 | |||
|
4228053b3f | ||
|
daeace6a2e | ||
|
6e3158e9d6 | ||
|
41df85d9b1 | ||
|
187c9e1c25 | ||
|
404105eaa8 | ||
|
8b9da3197e | ||
2ca4e6e6ae | |||
5eca8b80fb | |||
|
2443564272 | ||
|
ab79cc0e14 | ||
|
6922dba2a9 | ||
|
aaeb2d5c7f | ||
f3dcab421c | |||
ff343cd8a2 | |||
26e996e2f7 | |||
|
3ba6b33f0a | ||
|
b0fe0be425 | ||
|
a31595513f | ||
|
2b41d5f3db | ||
|
5c927e9dda | ||
|
3da68c2e49 | ||
eccc2b1bdd | |||
f0e31dcc11 | |||
|
e5b5f08153 | ||
d83c009b28 | |||
4534099d3a | |||
9a055a2f77 | |||
40fc82e515 | |||
93fa97e817 | |||
|
8dfb72c230 | ||
5c2a5babdd | |||
|
30adb0f7cc | ||
|
610a89bfaf | ||
|
55352da4d6 | ||
|
d24fefeda4 | ||
|
9e980e40e1 | ||
|
6855634a80 | ||
|
5120239c12 | ||
|
22e060163f | ||
|
365ed39776 | ||
|
2fc3edd6c0 | ||
|
e36874e26d | ||
|
85174f7489 | ||
|
0e7d19b3cd | ||
|
2b9b1febe9 | ||
|
5c44ad5579 | ||
|
baa35e0926 | ||
|
9531b7af46 |
20 changed files with 1086 additions and 192 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +0,0 @@
|
||||||
.svn
|
|
8
.woodpecker/code-style.yml
Normal file
8
.woodpecker/code-style.yml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
pipeline:
|
||||||
|
code-style:
|
||||||
|
image: composer
|
||||||
|
commands:
|
||||||
|
- composer global config repositories.repo-name vcs https://git.digital-competence.de/Packages/php-codesniffer
|
||||||
|
- composer global config --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
|
||||||
|
- composer global require digicomp/php-codesniffer:@dev
|
||||||
|
- composer global exec -- phpcs --runtime-set ignore_warnings_on_exit 1 --standard=DigiComp Classes/ Migrations/ Tests/ Resources/Private/
|
32
.woodpecker/functional-tests.yml
Normal file
32
.woodpecker/functional-tests.yml
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
workspace:
|
||||||
|
base: /woodpecker
|
||||||
|
path: package
|
||||||
|
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- FLOW_VERSION: 6.3
|
||||||
|
PHP_VERSION: 7.4
|
||||||
|
- FLOW_VERSION: 7.3
|
||||||
|
PHP_VERSION: 7.4
|
||||||
|
- FLOW_VERSION: 7.3
|
||||||
|
PHP_VERSION: 8.2
|
||||||
|
- FLOW_VERSION: 8.2
|
||||||
|
PHP_VERSION: 8.2
|
||||||
|
|
||||||
|
pipeline:
|
||||||
|
functional-tests:
|
||||||
|
image: thecodingmachine/php:${PHP_VERSION}-v4-cli
|
||||||
|
environment:
|
||||||
|
# Enable the PDO_SQLITE extension
|
||||||
|
- "PHP_EXTENSION_PDO_SQLITE=1"
|
||||||
|
- "FLOW_VERSION=${FLOW_VERSION}"
|
||||||
|
- "NEOS_BUILD_DIR=/woodpecker/Build-${FLOW_VERSION}"
|
||||||
|
commands:
|
||||||
|
- "sudo mkdir $NEOS_BUILD_DIR"
|
||||||
|
- "sudo chown -R docker:docker $NEOS_BUILD_DIR"
|
||||||
|
- "cd $NEOS_BUILD_DIR"
|
||||||
|
- "composer create-project --no-install neos/flow-base-distribution:^$FLOW_VERSION ."
|
||||||
|
- "composer config repositories.repo-name path /woodpecker/package"
|
||||||
|
- "composer remove --dev --no-update neos/behat || composer remove --no-update neos/behat"
|
||||||
|
- "composer require digicomp/settingvalidator:@dev"
|
||||||
|
- "bin/phpunit --configuration Build/BuildEssentials/PhpUnit/FunctionalTests.xml Packages/Application/DigiComp.SettingValidator/Tests/Functional"
|
14
CHANGELOG.md
Normal file
14
CHANGELOG.md
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
# Changelog
|
||||||
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## [Unreleased]
|
||||||
|
|
||||||
|
## [3.0.1] - 2020-09-09
|
||||||
|
### Changed
|
||||||
|
- dependency to Flow ^6.3
|
||||||
|
|
||||||
|
## [3.0.0] - 2020-08-31
|
||||||
|
Start of the changelog.
|
|
@ -1,33 +0,0 @@
|
||||||
<?php
|
|
||||||
namespace DigiComp\SettingValidator;
|
|
||||||
|
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
|
||||||
use TYPO3\Flow\Annotations as Flow;
|
|
||||||
use TYPO3\Flow\Configuration\ConfigurationManager;
|
|
||||||
use TYPO3\Flow\Core\Bootstrap;
|
|
||||||
use \TYPO3\Flow\Package\Package as BasePackage;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Flow\Scope("prototype")
|
|
||||||
*/
|
|
||||||
class Package extends BasePackage
|
|
||||||
{
|
|
||||||
|
|
||||||
public function boot(Bootstrap $bootstrap)
|
|
||||||
{
|
|
||||||
parent::boot($bootstrap);
|
|
||||||
|
|
||||||
$dispatcher = $bootstrap->getSignalSlotDispatcher();
|
|
||||||
$dispatcher->connect(
|
|
||||||
'TYPO3\Flow\Configuration\ConfigurationManager',
|
|
||||||
'configurationManagerReady',
|
|
||||||
function (ConfigurationManager $configurationManager) {
|
|
||||||
$configurationManager->registerConfigurationType(
|
|
||||||
'Validation',
|
|
||||||
ConfigurationManager::CONFIGURATION_PROCESSING_TYPE_DEFAULT,
|
|
||||||
true
|
|
||||||
);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,96 +0,0 @@
|
||||||
<?php
|
|
||||||
namespace DigiComp\SettingValidator\Validation\Validator;
|
|
||||||
|
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
|
||||||
use TYPO3\Flow\Annotations as Flow;
|
|
||||||
use TYPO3\Flow\Configuration\ConfigurationManager;
|
|
||||||
use TYPO3\Flow\Reflection\ObjectAccess;
|
|
||||||
use TYPO3\Flow\Reflection\ReflectionService;
|
|
||||||
use TYPO3\Flow\Validation\Exception\InvalidValidationConfigurationException;
|
|
||||||
use TYPO3\Flow\Validation\Exception\InvalidValidationOptionsException;
|
|
||||||
use TYPO3\Flow\Validation\Validator\AbstractValidator;
|
|
||||||
use TYPO3\Flow\Validation\ValidatorResolver;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validator resolving other Validators defined in Validation.yaml
|
|
||||||
*
|
|
||||||
* @Flow\Scope("prototype")
|
|
||||||
*/
|
|
||||||
class SettingsValidator extends AbstractValidator
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var ValidatorResolver
|
|
||||||
* @Flow\Inject
|
|
||||||
*/
|
|
||||||
protected $validatorResolver;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $validations;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var \TYPO3\Flow\Configuration\ConfigurationManager
|
|
||||||
*/
|
|
||||||
protected $configurationManager;
|
|
||||||
|
|
||||||
public function injectConfigurationManager(ConfigurationManager $configurationManager)
|
|
||||||
{
|
|
||||||
$this->configurationManager = $configurationManager;
|
|
||||||
$this->validations = $this->configurationManager->getConfiguration('Validation');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var ReflectionService
|
|
||||||
* @Flow\Inject
|
|
||||||
*/
|
|
||||||
protected $reflectionService;
|
|
||||||
|
|
||||||
protected $supportedOptions = array(
|
|
||||||
'name' => array('', 'Set the name of the setting-array to use', 'string', false)
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if $value is valid. If it is not valid, needs to add an error
|
|
||||||
* to Result.
|
|
||||||
*
|
|
||||||
* @param mixed $value
|
|
||||||
*
|
|
||||||
* @throws InvalidValidationOptionsException
|
|
||||||
* @throws InvalidValidationConfigurationException
|
|
||||||
*/
|
|
||||||
protected function isValid($value)
|
|
||||||
{
|
|
||||||
$name = $this->options['name'] ? $this->options['name'] : $this->reflectionService->getClassNameByObject(
|
|
||||||
$value
|
|
||||||
);
|
|
||||||
if (!isset($this->validations[$name])) {
|
|
||||||
throw new InvalidValidationOptionsException(
|
|
||||||
'The name ' . $name . ' has not been defined in Validation.yaml!',
|
|
||||||
1397821438
|
|
||||||
);
|
|
||||||
}
|
|
||||||
$config = &$this->validations[$name];
|
|
||||||
foreach ($config as $validatorConfig) {
|
|
||||||
$validator = $this->validatorResolver->createValidator(
|
|
||||||
$validatorConfig['validator'],
|
|
||||||
$validatorConfig['options']
|
|
||||||
);
|
|
||||||
if (!$validator) {
|
|
||||||
throw new InvalidValidationConfigurationException(
|
|
||||||
'Validator could not be resolved: ' .
|
|
||||||
$validatorConfig['validator'] . '. Check your Validation.yaml',
|
|
||||||
1402326139
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (isset($validatorConfig['property'])) {
|
|
||||||
$this->result->forProperty($validatorConfig['property'])->merge(
|
|
||||||
$validator->validate(ObjectAccess::getPropertyPath($value, $validatorConfig['property']))
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
$this->result->merge($validator->validate($value));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
43
Classes/Package.php
Normal file
43
Classes/Package.php
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DigiComp\SettingValidator;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the DigiComp.SettingValidator package.
|
||||||
|
*
|
||||||
|
* (c) digital competence
|
||||||
|
*
|
||||||
|
* This package is Open Source Software. For the full copyright and license
|
||||||
|
* information, please view the LICENSE file which was distributed with this
|
||||||
|
* source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use Neos\Flow\Configuration\ConfigurationManager;
|
||||||
|
use Neos\Flow\Core\Bootstrap;
|
||||||
|
use Neos\Flow\Package\Package as NeosFlowPackagePackage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Package base class of the DigiComp.SettingValidator package.
|
||||||
|
*/
|
||||||
|
class Package extends NeosFlowPackagePackage
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function boot(Bootstrap $bootstrap): void
|
||||||
|
{
|
||||||
|
parent::boot($bootstrap);
|
||||||
|
|
||||||
|
$dispatcher = $bootstrap->getSignalSlotDispatcher();
|
||||||
|
|
||||||
|
$dispatcher->connect(
|
||||||
|
ConfigurationManager::class,
|
||||||
|
'configurationManagerReady',
|
||||||
|
function (ConfigurationManager $configurationManager): void {
|
||||||
|
$configurationManager->registerConfigurationType('Validation');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
152
Classes/Validation/Validator/ConditionalValidator.php
Normal file
152
Classes/Validation/Validator/ConditionalValidator.php
Normal file
|
@ -0,0 +1,152 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DigiComp\SettingValidator\Validation\Validator;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the DigiComp.SettingValidator package.
|
||||||
|
*
|
||||||
|
* (c) digital competence
|
||||||
|
*
|
||||||
|
* This package is Open Source Software. For the full copyright and license
|
||||||
|
* information, please view the LICENSE file which was distributed with this
|
||||||
|
* source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use Neos\Eel\EelEvaluatorInterface;
|
||||||
|
use Neos\Eel\Exception as NeosEelException;
|
||||||
|
use Neos\Eel\Utility;
|
||||||
|
use Neos\Flow\Annotations as Flow;
|
||||||
|
use Neos\Flow\ObjectManagement\DependencyInjection\DependencyProxy;
|
||||||
|
use Neos\Flow\Validation\Exception\InvalidValidationConfigurationException;
|
||||||
|
use Neos\Flow\Validation\Exception\NoSuchValidatorException;
|
||||||
|
use Neos\Flow\Validation\Validator\AbstractValidator;
|
||||||
|
use Neos\Flow\Validation\ValidatorResolver;
|
||||||
|
|
||||||
|
class ConditionalValidator extends AbstractValidator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @Flow\Inject
|
||||||
|
* @var EelEvaluatorInterface
|
||||||
|
*/
|
||||||
|
protected $eelEvaluator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Flow\Inject
|
||||||
|
* @var ValidatorResolver
|
||||||
|
*/
|
||||||
|
protected $validatorResolver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
protected $supportedOptions = [
|
||||||
|
'conditions' => [[], 'List of entries with "condition" (eel expression) and "validators" (list of validators).', 'array', true],
|
||||||
|
'fallbackValidators' => [[], 'List of validators that is used if no condition matched.', 'array'],
|
||||||
|
'validationGroups' => [['Default'], 'Same as "Validation Groups" of Flow Framework', 'array'],
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
* @throws InvalidValidationConfigurationException
|
||||||
|
* @throws NeosEelException
|
||||||
|
* @throws NoSuchValidatorException
|
||||||
|
*/
|
||||||
|
protected function isValid($value): void
|
||||||
|
{
|
||||||
|
$validatorConfigs = [];
|
||||||
|
|
||||||
|
$hasMatch = false;
|
||||||
|
|
||||||
|
foreach ($this->options['conditions'] as $condition) {
|
||||||
|
if ($this->eelEvaluator instanceof DependencyProxy) {
|
||||||
|
$this->eelEvaluator->_activateDependency();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Utility::evaluateEelExpression($condition['condition'], $this->eelEvaluator, ['this' => $value])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$hasMatch = true;
|
||||||
|
|
||||||
|
foreach ($condition['validators'] as $validator => $options) {
|
||||||
|
if ($options === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$validatorConfigs[] = [
|
||||||
|
'validator' => $validator,
|
||||||
|
'options' => $options,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$hasMatch) {
|
||||||
|
foreach ($this->options['fallbackValidators'] as $validator => $options) {
|
||||||
|
if ($options === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$validatorConfigs[] = [
|
||||||
|
'validator' => $validator,
|
||||||
|
'options' => $options,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($validatorConfigs as $validatorConfig) {
|
||||||
|
if (!$this->doesValidationGroupsMatch($validatorConfig)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->handleValidationGroups($validatorConfig);
|
||||||
|
|
||||||
|
$validator = $this->validatorResolver->createValidator(
|
||||||
|
$validatorConfig['validator'],
|
||||||
|
$validatorConfig['options']
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($validator === null) {
|
||||||
|
throw new InvalidValidationConfigurationException(
|
||||||
|
\sprintf(
|
||||||
|
'Validator "%s" could not be resolved. Check your Validation.yaml',
|
||||||
|
$validatorConfig['validator']
|
||||||
|
),
|
||||||
|
1402326139
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->getResult()->merge($validator->validate($value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether at least one configured group does match, if any is configured.
|
||||||
|
*
|
||||||
|
* @param array $validatorConfig
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function doesValidationGroupsMatch(array $validatorConfig): bool
|
||||||
|
{
|
||||||
|
return !isset($validatorConfig['options']['validationGroups'])
|
||||||
|
|| \array_intersect(
|
||||||
|
$validatorConfig['options']['validationGroups'],
|
||||||
|
$this->options['validationGroups']
|
||||||
|
) !== [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add validation groups for recursion if necessary.
|
||||||
|
*
|
||||||
|
* @param array $validatorConfig
|
||||||
|
*/
|
||||||
|
protected function handleValidationGroups(array &$validatorConfig): void
|
||||||
|
{
|
||||||
|
if (\in_array($validatorConfig['validator'], ['DigiComp.SettingValidator:Settings', 'DigiComp.SettingValidator:Conditional', 'DigiComp.SettingValidator:Properties', 'Neos.Flow:Collection'])) {
|
||||||
|
$validatorConfig['options']['validationGroups'] = $this->options['validationGroups'];
|
||||||
|
} elseif (isset($validatorConfig['options']['validationGroups'])) {
|
||||||
|
unset($validatorConfig['options']['validationGroups']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
119
Classes/Validation/Validator/PropertiesValidator.php
Normal file
119
Classes/Validation/Validator/PropertiesValidator.php
Normal file
|
@ -0,0 +1,119 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DigiComp\SettingValidator\Validation\Validator;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the DigiComp.SettingValidator package.
|
||||||
|
*
|
||||||
|
* (c) digital competence
|
||||||
|
*
|
||||||
|
* This package is Open Source Software. For the full copyright and license
|
||||||
|
* information, please view the LICENSE file which was distributed with this
|
||||||
|
* source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use Neos\Flow\Annotations as Flow;
|
||||||
|
use Neos\Flow\Validation\Exception\InvalidValidationConfigurationException;
|
||||||
|
use Neos\Flow\Validation\Exception\NoSuchValidatorException;
|
||||||
|
use Neos\Flow\Validation\Validator\AbstractValidator;
|
||||||
|
use Neos\Flow\Validation\ValidatorResolver;
|
||||||
|
use Neos\Utility\ObjectAccess;
|
||||||
|
|
||||||
|
class PropertiesValidator extends AbstractValidator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @Flow\Inject
|
||||||
|
* @var ValidatorResolver
|
||||||
|
*/
|
||||||
|
protected $validatorResolver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
protected $supportedOptions = [
|
||||||
|
'validatorsForProperties' => [[], 'List of validators for properties. ', 'array', true],
|
||||||
|
'validationGroups' => [['Default'], 'Same as "Validation Groups" of Flow Framework', 'array'],
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
* @throws InvalidValidationConfigurationException
|
||||||
|
* @throws NoSuchValidatorException
|
||||||
|
*/
|
||||||
|
protected function isValid($value): void
|
||||||
|
{
|
||||||
|
$validatorConfigs = [];
|
||||||
|
|
||||||
|
foreach ($this->options['validatorsForProperties'] as $property => $validators) {
|
||||||
|
foreach ($validators as $validator => $options) {
|
||||||
|
if ($options === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$validatorConfigs[] = [
|
||||||
|
'validator' => $validator,
|
||||||
|
'options' => $options,
|
||||||
|
'property' => $property,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($validatorConfigs as $validatorConfig) {
|
||||||
|
if (!$this->doesValidationGroupsMatch($validatorConfig)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->handleValidationGroups($validatorConfig);
|
||||||
|
|
||||||
|
$validator = $this->validatorResolver->createValidator(
|
||||||
|
$validatorConfig['validator'],
|
||||||
|
$validatorConfig['options']
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($validator === null) {
|
||||||
|
throw new InvalidValidationConfigurationException(
|
||||||
|
\sprintf(
|
||||||
|
'Validator "%s" could not be resolved. Check your Validation.yaml',
|
||||||
|
$validatorConfig['validator']
|
||||||
|
),
|
||||||
|
1402326139
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->getResult()->forProperty($validatorConfig['property'])->merge(
|
||||||
|
$validator->validate(ObjectAccess::getPropertyPath($value, $validatorConfig['property']))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether at least one configured group does match, if any is configured.
|
||||||
|
*
|
||||||
|
* @param array $validatorConfig
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function doesValidationGroupsMatch(array $validatorConfig): bool
|
||||||
|
{
|
||||||
|
return !isset($validatorConfig['options']['validationGroups'])
|
||||||
|
|| \array_intersect(
|
||||||
|
$validatorConfig['options']['validationGroups'],
|
||||||
|
$this->options['validationGroups']
|
||||||
|
) !== [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add validation groups for recursion if necessary.
|
||||||
|
*
|
||||||
|
* @param array $validatorConfig
|
||||||
|
*/
|
||||||
|
protected function handleValidationGroups(array &$validatorConfig): void
|
||||||
|
{
|
||||||
|
if (\in_array($validatorConfig['validator'], ['DigiComp.SettingValidator:Settings', 'DigiComp.SettingValidator:Conditional', 'DigiComp.SettingValidator:Properties', 'Neos.Flow:Collection'])) {
|
||||||
|
$validatorConfig['options']['validationGroups'] = $this->options['validationGroups'];
|
||||||
|
} elseif (isset($validatorConfig['options']['validationGroups'])) {
|
||||||
|
unset($validatorConfig['options']['validationGroups']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
160
Classes/Validation/Validator/SettingsValidator.php
Normal file
160
Classes/Validation/Validator/SettingsValidator.php
Normal file
|
@ -0,0 +1,160 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DigiComp\SettingValidator\Validation\Validator;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the DigiComp.SettingValidator package.
|
||||||
|
*
|
||||||
|
* (c) digital competence
|
||||||
|
*
|
||||||
|
* This package is Open Source Software. For the full copyright and license
|
||||||
|
* information, please view the LICENSE file which was distributed with this
|
||||||
|
* source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use Neos\Flow\Annotations as Flow;
|
||||||
|
use Neos\Flow\Validation\Exception\InvalidValidationConfigurationException;
|
||||||
|
use Neos\Flow\Validation\Exception\InvalidValidationOptionsException;
|
||||||
|
use Neos\Flow\Validation\Exception\NoSuchValidatorException;
|
||||||
|
use Neos\Flow\Validation\Validator\AbstractValidator;
|
||||||
|
use Neos\Flow\Validation\ValidatorResolver;
|
||||||
|
use Neos\Utility\TypeHandling;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validator resolving other Validators defined in Validation.yaml
|
||||||
|
*/
|
||||||
|
class SettingsValidator extends AbstractValidator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @Flow\Inject
|
||||||
|
* @var ValidatorResolver
|
||||||
|
*/
|
||||||
|
protected $validatorResolver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Flow\InjectConfiguration(type="Validation")
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected array $validations;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
protected $supportedOptions = [
|
||||||
|
'name' => ['', 'Name of the setting array to use', 'string'],
|
||||||
|
'validationGroups' => [['Default'], 'Same as "Validation Groups" of Flow Framework', 'array'],
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
* @throws InvalidValidationOptionsException
|
||||||
|
* @throws InvalidValidationConfigurationException
|
||||||
|
* @throws NoSuchValidatorException
|
||||||
|
*/
|
||||||
|
protected function isValid($value): void
|
||||||
|
{
|
||||||
|
$validations = $this->validations;
|
||||||
|
|
||||||
|
// TODO: feature idea - we could extend the library to automatically be part of the base conjunction validator
|
||||||
|
$name = $this->options['name'] !== '' ? $this->options['name'] : TypeHandling::getTypeForValue($value);
|
||||||
|
if (!isset($validations[$name])) {
|
||||||
|
throw new InvalidValidationOptionsException(
|
||||||
|
'The name "' . $name . '" has not been defined in Validation.yaml!',
|
||||||
|
1397821438
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// @deprecated - converts old "self" to new structure
|
||||||
|
if (isset($validations[$name]['self'])) {
|
||||||
|
foreach ($validations[$name]['self'] as $validator => $options) {
|
||||||
|
if (isset($validations[$name][$validator])) {
|
||||||
|
throw new \RuntimeException('The validator "' . $validator . '" is already defined on parent level.', 1725000364);
|
||||||
|
}
|
||||||
|
$validations[$name][$validator] = $options;
|
||||||
|
}
|
||||||
|
|
||||||
|
unset($validations[$name]['self']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// @deprecated - converts old "properties" to new structure
|
||||||
|
if (isset($validations[$name]['properties'])) {
|
||||||
|
if (isset($validations[$name]['DigiComp.SettingValidator:Properties'])) {
|
||||||
|
throw new \RuntimeException('The validator "DigiComp.SettingValidator:Properties" is already defined on parent level.', 1725000396);
|
||||||
|
}
|
||||||
|
$validations[$name]['DigiComp.SettingValidator:Properties'] = [
|
||||||
|
'validatorsForProperties' => $validations[$name]['properties'],
|
||||||
|
];
|
||||||
|
|
||||||
|
unset($validations[$name]['properties']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$validatorConfigs = [];
|
||||||
|
|
||||||
|
foreach ($validations[$name] as $validator => $options) {
|
||||||
|
if ($options === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$validatorConfigs[] = [
|
||||||
|
'validator' => $validator,
|
||||||
|
'options' => $options,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($validatorConfigs as $validatorConfig) {
|
||||||
|
if (!$this->doesValidationGroupsMatch($validatorConfig)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->handleValidationGroups($validatorConfig);
|
||||||
|
|
||||||
|
$validator = $this->validatorResolver->createValidator(
|
||||||
|
$validatorConfig['validator'],
|
||||||
|
$validatorConfig['options']
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($validator === null) {
|
||||||
|
throw new InvalidValidationConfigurationException(
|
||||||
|
\sprintf(
|
||||||
|
'Validator "%s" could not be resolved. Check your Validation.yaml',
|
||||||
|
$validatorConfig['validator']
|
||||||
|
),
|
||||||
|
1402326139
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->getResult()->merge($validator->validate($value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether at least one configured group does match, if any is configured.
|
||||||
|
*
|
||||||
|
* @param array $validatorConfig
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function doesValidationGroupsMatch(array $validatorConfig): bool
|
||||||
|
{
|
||||||
|
return !isset($validatorConfig['options']['validationGroups'])
|
||||||
|
|| \array_intersect(
|
||||||
|
$validatorConfig['options']['validationGroups'],
|
||||||
|
$this->options['validationGroups']
|
||||||
|
) !== [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add validation groups for recursion if necessary.
|
||||||
|
*
|
||||||
|
* @param array $validatorConfig
|
||||||
|
*/
|
||||||
|
protected function handleValidationGroups(array &$validatorConfig): void
|
||||||
|
{
|
||||||
|
if (\in_array($validatorConfig['validator'], ['DigiComp.SettingValidator:Settings', 'DigiComp.SettingValidator:Conditional', 'DigiComp.SettingValidator:Properties', 'Neos.Flow:Collection'])) {
|
||||||
|
$validatorConfig['options']['validationGroups'] = $this->options['validationGroups'];
|
||||||
|
} elseif (isset($validatorConfig['options']['validationGroups'])) {
|
||||||
|
unset($validatorConfig['options']['validationGroups']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
38
Configuration/Testing/Validation.yaml
Normal file
38
Configuration/Testing/Validation.yaml
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
DigiComp\SettingValidator\Tests\Functional\Fixtures\TestObject:
|
||||||
|
properties:
|
||||||
|
shouldBeTrue:
|
||||||
|
BooleanValue:
|
||||||
|
expectedValue: true
|
||||||
|
shouldBeFalse:
|
||||||
|
BooleanValue:
|
||||||
|
expectedValue: false
|
||||||
|
Grumble: ~
|
||||||
|
|
||||||
|
DigiComp\SettingValidator\Tests\Functional\Fixtures\TestValidationGroupsCustomObject:
|
||||||
|
self:
|
||||||
|
DigiComp.SettingValidator:Settings:
|
||||||
|
name: "GroupValidatorCustom"
|
||||||
|
|
||||||
|
DigiComp\SettingValidator\Tests\Functional\Fixtures\TestValidationGroupsDefaultObject:
|
||||||
|
self:
|
||||||
|
DigiComp.SettingValidator:Settings:
|
||||||
|
name: "GroupValidatorDefault"
|
||||||
|
|
||||||
|
GroupValidatorDefault:
|
||||||
|
properties:
|
||||||
|
shouldBeTrue:
|
||||||
|
BooleanValue:
|
||||||
|
expectedValue: true
|
||||||
|
|
||||||
|
GroupValidatorCustom:
|
||||||
|
properties:
|
||||||
|
shouldBeFalse:
|
||||||
|
BooleanValue:
|
||||||
|
expectedValue: false
|
||||||
|
validationGroups:
|
||||||
|
- "Custom"
|
||||||
|
|
||||||
|
TrueValidator:
|
||||||
|
self:
|
||||||
|
BooleanValue:
|
||||||
|
expectedValue: true
|
56
Migrations/Code/Version20170603120900.php
Normal file
56
Migrations/Code/Version20170603120900.php
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Neos\Flow\Core\Migrations;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restructures all Validation.yaml to new format
|
||||||
|
*/
|
||||||
|
class Version20170603120900 extends AbstractMigration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getIdentifier(): string
|
||||||
|
{
|
||||||
|
return 'DigiComp.SettingValidator-20170603120900';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
$this->processConfiguration(
|
||||||
|
'Validation',
|
||||||
|
function (array &$configuration): void {
|
||||||
|
foreach ($configuration as $validatorName => &$validators) {
|
||||||
|
// guard that protects configuration, which has already the new format:
|
||||||
|
if (isset($validators['properties']) || isset($validators['self'])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$newConfiguration = ['properties' => [], 'self' => []];
|
||||||
|
|
||||||
|
foreach ($validators as $key => $validator) {
|
||||||
|
if (!isset($validator['validator']) || !isset($validator['options'])) {
|
||||||
|
$this->showWarning(
|
||||||
|
'The Validation.yaml files contained no validator or options for validation: '
|
||||||
|
. '"' . $validatorName . '.' . $key . '". It was not migrated.'
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (isset($validator['property'])) {
|
||||||
|
$newConfiguration['properties'][$validator['property']][$validator['validator']] =
|
||||||
|
$validator['options'];
|
||||||
|
} else {
|
||||||
|
$newConfiguration['self'][$validator['validator']] = $validator['options'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$validators = $newConfiguration;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
233
README.md
233
README.md
|
@ -1,46 +1,211 @@
|
||||||
DigiComp.SettingValidator
|
# DigiComp.SettingValidator
|
||||||
-------------------------
|
|
||||||
|
|
||||||
|
This package allows configuring validators with a new configuration type.
|
||||||
|
|
||||||
This Package allows to configure Validators for your Action-Arguments or domain model properties to be set by a new
|
## Introduction
|
||||||
Yaml-File in your Configuration directory.
|
|
||||||
|
|
||||||
Lets imagine you had this action-method:
|
This package provides the `SettingsValidator` which uses the configuration type `Validation` to resolve the validators
|
||||||
|
that should be applied to the value. It distinguishes between validators that are applied to the value itself and its
|
||||||
|
properties.
|
||||||
|
|
||||||
|
## Resolving the validation configuration
|
||||||
|
|
||||||
|
The `SettingsValidator` has an option `name`. If it is set, the name is used to resolve the validation configuration,
|
||||||
|
otherwise the type of the value is used, which is mainly useful for objects where the FQCN is used.
|
||||||
|
|
||||||
|
### Resolving by option `name`
|
||||||
|
|
||||||
|
To resolve the validation configuration by name just use the option `name`.
|
||||||
|
|
||||||
|
```php
|
||||||
/**
|
/**
|
||||||
* @param Order $order
|
* @Flow\Validate(type="DigiComp.SettingValidator:Settings", options={"name"="MyNamedValidator"})
|
||||||
* @Flow\Validate(type="DigiComp.SettingValidator:Settings")
|
* @var MyObject
|
||||||
*/
|
*/
|
||||||
public function createOrder($order) {...}
|
protected MyObject $myObject;
|
||||||
|
```
|
||||||
|
|
||||||
Then your Validation.yaml could look like this:
|
The `SettingsValidator` will search for an entry inside the `Validation.yaml` with that name.
|
||||||
|
|
||||||
SuperVendor\SuperPackage\Domain\Model\Order:
|
```yaml
|
||||||
-
|
MyNamedValidator:
|
||||||
property: price
|
...
|
||||||
validator: NumberRange
|
```
|
||||||
options:
|
|
||||||
maximum: 20
|
|
||||||
minimum: 10
|
|
||||||
-
|
|
||||||
validator: SuperVendor.SuperPackage:SomeOtherValidator #validates the complete object
|
|
||||||
options: []
|
|
||||||
-
|
|
||||||
property: customer
|
|
||||||
validator: DigiComp.SettingValidator:Settings
|
|
||||||
options:
|
|
||||||
name: OrderCustomer
|
|
||||||
|
|
||||||
OrderCustomer:
|
### Resolving by type
|
||||||
-
|
|
||||||
property: firstName
|
|
||||||
validator: StringLength
|
|
||||||
options:
|
|
||||||
minimum: 3
|
|
||||||
maximum: 20
|
|
||||||
|
|
||||||
|
To resolve the validation configuration by type just do not set the option `name`.
|
||||||
|
|
||||||
As you see: Nesting is possible ;) That way you can easily construct flexible structures.
|
```php
|
||||||
|
/**
|
||||||
|
* @Flow\Validate(type="DigiComp.SettingValidator:Settings")
|
||||||
|
* @var MyObject
|
||||||
|
*/
|
||||||
|
protected MyObject $myObject;
|
||||||
|
```
|
||||||
|
|
||||||
The SettingsValidator has an optional option: "name" - If you don't give one, it assumes your validation value is an
|
The `SettingsValidator` will search for an entry inside the `Validation.yaml` with the FQCN of `MyObject`.
|
||||||
object and searches in Validation.yaml for the FQCN.
|
|
||||||
|
```yaml
|
||||||
|
My\Package\Domain\Model\MyObject:
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
## The validation configuration
|
||||||
|
|
||||||
|
### Difference between `self` and `properties`
|
||||||
|
|
||||||
|
`self` contains a map of validators that are applied to the value itself. `properties` contains a map with property
|
||||||
|
names of the value you would like to validate and each entry contains a map of validators that are applied to that
|
||||||
|
property.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
MyNamedValidator:
|
||||||
|
self:
|
||||||
|
...
|
||||||
|
properties:
|
||||||
|
myProperty1:
|
||||||
|
...
|
||||||
|
myProperty2:
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
### Configuring a validator
|
||||||
|
|
||||||
|
To configure a validator you use the type of the validator as key and the options as entries of that key. If the
|
||||||
|
validator has no options or all the default values are used, set an empty map as options.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
MyNamedValidator:
|
||||||
|
self:
|
||||||
|
'My.Package:SomeValidator':
|
||||||
|
myOption: "myOptionValue"
|
||||||
|
properties:
|
||||||
|
myProperty1:
|
||||||
|
'My.Package:SomeOtherValidator': {}
|
||||||
|
myProperty2:
|
||||||
|
'My.Package:SomeOtherValidator': {}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Disable a validator
|
||||||
|
|
||||||
|
To disable a validator you need to set the options to `null`.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
MyNamedValidator:
|
||||||
|
self:
|
||||||
|
'My.Package:SomeValidator': ~
|
||||||
|
```
|
||||||
|
|
||||||
|
## Using the `SettingsValidator`
|
||||||
|
|
||||||
|
The `SettingsValidator` can be used to reduce the number of `@Flow\Validate` annotations and gives you the possibility
|
||||||
|
of overwriting existing validation configurations in other packages.
|
||||||
|
|
||||||
|
### Using on properties
|
||||||
|
|
||||||
|
Old PHP code:
|
||||||
|
|
||||||
|
```php
|
||||||
|
/**
|
||||||
|
* @Flow\Validate(type="My.Package:SomeValidator", options={"myOption"="myOptionValue"})
|
||||||
|
* @Flow\Validate(type="My.Package:SomeOtherValidator")
|
||||||
|
* @var MyObject
|
||||||
|
*/
|
||||||
|
protected MyObject $myObject;
|
||||||
|
```
|
||||||
|
|
||||||
|
New PHP code:
|
||||||
|
|
||||||
|
```php
|
||||||
|
/**
|
||||||
|
* @Flow\Validate(type="DigiComp.SettingValidator:Settings", options={"name"="MyNamedValidator"})
|
||||||
|
* @var MyObject
|
||||||
|
*/
|
||||||
|
protected MyObject $myObject;
|
||||||
|
```
|
||||||
|
|
||||||
|
New validation configuration:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
MyNamedValidator:
|
||||||
|
self:
|
||||||
|
'My.Package:SomeValidator':
|
||||||
|
myOption: "myOptionValue"
|
||||||
|
'My.Package:SomeOtherValidator': {}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Using on actions
|
||||||
|
|
||||||
|
Old PHP code:
|
||||||
|
|
||||||
|
```php
|
||||||
|
/**
|
||||||
|
* @Flow\Validate(argumentName="myObject", type="My.Package:SomeValidator", options={"myOption"="myOptionValue"})
|
||||||
|
* @Flow\Validate(argumentName="myObject", type="My.Package:SomeOtherValidator")
|
||||||
|
* @param MyObject $myObject
|
||||||
|
*/
|
||||||
|
public function myAction(MyObject $myObject)
|
||||||
|
{
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
New PHP code:
|
||||||
|
|
||||||
|
```php
|
||||||
|
/**
|
||||||
|
* @Flow\Validate(argumentName="myObject", type="DigiComp.SettingValidator:Settings", options={"name"="MyNamedValidator"})
|
||||||
|
* @param MyObject $myObject
|
||||||
|
*/
|
||||||
|
public function myAction(MyObject $myObject)
|
||||||
|
{
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
New validation configuration:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
MyNamedValidator:
|
||||||
|
self:
|
||||||
|
'My.Package:SomeValidator':
|
||||||
|
myOption: "myOptionValue"
|
||||||
|
'My.Package:SomeOtherValidator': {}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Using inside validator configurations
|
||||||
|
|
||||||
|
You can use the `SettingsValidator` inside the validator configuration to easily construct flexible structures.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
MyNamedValidator:
|
||||||
|
properties:
|
||||||
|
myProperty1:
|
||||||
|
'DigiComp.SettingValidator:Settings':
|
||||||
|
name: "MyOtherNamedValidator"
|
||||||
|
|
||||||
|
MyOtherNamedValidator:
|
||||||
|
self:
|
||||||
|
'My.Package:SomeOtherValidator': {}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Providing an empty validator
|
||||||
|
|
||||||
|
It can be useful to provide an empty validator in code that is used by many projects. By doing so you can make sure that
|
||||||
|
a different validation is possible in any project.
|
||||||
|
|
||||||
|
```php
|
||||||
|
/**
|
||||||
|
* @Flow\Validate(argumentName="myObject", type="DigiComp.SettingValidator:Settings", options={"name"="MyNamedValidator"})
|
||||||
|
* @param MyObject $myObject
|
||||||
|
*/
|
||||||
|
public function myAction(MyObject $myObject)
|
||||||
|
{
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
MyNamedValidator: {}
|
||||||
|
```
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
type: dictionary
|
type: "dictionary"
|
||||||
additionalProperties:
|
additionalProperties:
|
||||||
type: array
|
type: "dictionary"
|
||||||
items:
|
|
||||||
type: dictionary
|
|
||||||
additionalProperties: false
|
additionalProperties: false
|
||||||
# This validation sadly only hits the first level of validation, and options are not coverable with current validator
|
|
||||||
properties:
|
properties:
|
||||||
validator: {type: string, required: TRUE}
|
type: "dictionary"
|
||||||
options: {type: dictionary, required: TRUE}
|
additionalProperties: false
|
||||||
property: {type: string, required: FALSE}
|
properties:
|
||||||
|
type: "dictionary"
|
||||||
|
self:
|
||||||
|
type: "dictionary"
|
||||||
|
|
50
Tests/Functional/Fixtures/TestObject.php
Normal file
50
Tests/Functional/Fixtures/TestObject.php
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DigiComp\SettingValidator\Tests\Functional\Fixtures;
|
||||||
|
|
||||||
|
use Neos\Flow\Annotations as Flow;
|
||||||
|
|
||||||
|
class TestObject
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected bool $shouldBeTrue = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected bool $shouldBeFalse = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Flow\Validate(type="DigiComp.SettingValidator:Settings", options={"name"="TrueValidator"})
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected bool $shouldBeTrueAndValidatedByAnnotation = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isShouldBeTrue(): bool
|
||||||
|
{
|
||||||
|
return $this->shouldBeTrue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isShouldBeFalse(): bool
|
||||||
|
{
|
||||||
|
return $this->shouldBeFalse;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isShouldBeTrueAndValidatedByAnnotation(): bool
|
||||||
|
{
|
||||||
|
return $this->shouldBeTrueAndValidatedByAnnotation;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DigiComp\SettingValidator\Tests\Functional\Fixtures;
|
||||||
|
|
||||||
|
class TestValidationGroupsCustomObject
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected bool $shouldBeTrue = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected bool $shouldBeFalse = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isShouldBeTrue(): bool
|
||||||
|
{
|
||||||
|
return $this->shouldBeTrue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isShouldBeFalse(): bool
|
||||||
|
{
|
||||||
|
return $this->shouldBeFalse;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DigiComp\SettingValidator\Tests\Functional\Fixtures;
|
||||||
|
|
||||||
|
class TestValidationGroupsDefaultObject
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected bool $shouldBeTrue = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected bool $shouldBeFalse = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isShouldBeTrue(): bool
|
||||||
|
{
|
||||||
|
return $this->shouldBeTrue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isShouldBeFalse(): bool
|
||||||
|
{
|
||||||
|
return $this->shouldBeFalse;
|
||||||
|
}
|
||||||
|
}
|
79
Tests/Functional/SettingsValidatorTest.php
Normal file
79
Tests/Functional/SettingsValidatorTest.php
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DigiComp\SettingValidator\Tests\Functional;
|
||||||
|
|
||||||
|
use DigiComp\SettingValidator\Tests\Functional\Fixtures\TestObject;
|
||||||
|
use DigiComp\SettingValidator\Tests\Functional\Fixtures\TestValidationGroupsCustomObject;
|
||||||
|
use DigiComp\SettingValidator\Tests\Functional\Fixtures\TestValidationGroupsDefaultObject;
|
||||||
|
use DigiComp\SettingValidator\Validation\Validator\SettingsValidator;
|
||||||
|
use Neos\Flow\Tests\FunctionalTestCase;
|
||||||
|
use Neos\Flow\Validation\Exception\InvalidValidationConfigurationException;
|
||||||
|
use Neos\Flow\Validation\Exception\InvalidValidationOptionsException;
|
||||||
|
use Neos\Flow\Validation\Exception\NoSuchValidatorException;
|
||||||
|
use Neos\Flow\Validation\ValidatorResolver;
|
||||||
|
|
||||||
|
class SettingsValidatorTest extends FunctionalTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @throws InvalidValidationOptionsException
|
||||||
|
*/
|
||||||
|
public function ifNoNameIsGivenClassNameIsUsed(): void
|
||||||
|
{
|
||||||
|
$result = $this->objectManager->get(SettingsValidator::class)->validate(new TestObject());
|
||||||
|
|
||||||
|
self::assertTrue($result->hasErrors());
|
||||||
|
self::assertCount(1, $result->getFlattenedErrors());
|
||||||
|
self::assertCount(1, $result->forProperty('shouldBeFalse')->getErrors());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @throws InvalidValidationConfigurationException
|
||||||
|
* @throws InvalidValidationOptionsException
|
||||||
|
* @throws NoSuchValidatorException
|
||||||
|
*/
|
||||||
|
public function conjunctionValidationWorksAsExpected(): void
|
||||||
|
{
|
||||||
|
$result = $this->objectManager
|
||||||
|
->get(ValidatorResolver::class)
|
||||||
|
->getBaseValidatorConjunction(TestObject::class)
|
||||||
|
->validate(new TestObject());
|
||||||
|
|
||||||
|
self::assertTrue($result->hasErrors());
|
||||||
|
self::assertCount(1, $result->getFlattenedErrors());
|
||||||
|
self::assertCount(1, $result->forProperty('shouldBeTrueAndValidatedByAnnotation')->getErrors());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @throws InvalidValidationOptionsException
|
||||||
|
*/
|
||||||
|
public function defaultValidationGroupWorks(): void
|
||||||
|
{
|
||||||
|
$result = $this->objectManager
|
||||||
|
->get(SettingsValidator::class, ['validationGroups' => ['Default']])
|
||||||
|
->validate(new TestValidationGroupsDefaultObject());
|
||||||
|
|
||||||
|
self::assertTrue($result->hasErrors(), 'No errors for validation group "Default"');
|
||||||
|
self::assertCount(1, $result->getFlattenedErrors(), 'Got a non expected number of errors for group "Default"');
|
||||||
|
self::assertCount(1, $result->forProperty('shouldBeTrue')->getErrors(), 'Got no error for property');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @throws InvalidValidationOptionsException
|
||||||
|
*/
|
||||||
|
public function customValidationGroupWorks(): void
|
||||||
|
{
|
||||||
|
$result = $this->objectManager
|
||||||
|
->get(SettingsValidator::class, ['validationGroups' => ['Custom']])
|
||||||
|
->validate(new TestValidationGroupsCustomObject());
|
||||||
|
|
||||||
|
self::assertTrue($result->hasErrors(), 'No errors for validation group "Custom"');
|
||||||
|
self::assertCount(1, $result->getFlattenedErrors(), 'Got a non expected number of errors for group "Custom"');
|
||||||
|
self::assertCount(1, $result->forProperty('shouldBeFalse')->getErrors(), 'Got no error for property');
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,34 +1,55 @@
|
||||||
{
|
{
|
||||||
"name": "digicomp/settingvalidator",
|
"name": "digicomp/settingvalidator",
|
||||||
"type": "typo3-flow-package",
|
"description": "Just a Neos\\Flow Validator resolving other Validators with Configuration/Validation.yaml",
|
||||||
"description": "Just a TYPO3\\Flow Validator resolving other Validators with Configuration/Validation.yaml",
|
"type": "neos-package",
|
||||||
|
"keywords": [
|
||||||
|
"Neos",
|
||||||
|
"Flow",
|
||||||
|
"validation"
|
||||||
|
],
|
||||||
|
"homepage": "https://github.com/digital-competence/DigiComp.SettingValidator",
|
||||||
|
"license": "MIT",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "Ferdinand Kuhl",
|
"name": "Ferdinand Kuhl",
|
||||||
"email": "f.kuhl@digital-competence.de",
|
"email": "f.kuhl@digital-competence.de",
|
||||||
"homepage": "http://www.digital-competence.de",
|
"homepage": "https://www.digital-competence.de",
|
||||||
"role": "Developer"
|
"role": "Developer"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"license": "MIT",
|
|
||||||
"homepage": "https://github.com/fcool/DigiComp.SettingValidator",
|
|
||||||
"keywords": [
|
|
||||||
"Flow",
|
|
||||||
"validation"
|
|
||||||
],
|
|
||||||
"require": {
|
"require": {
|
||||||
"typo3/flow": "~2.0|~3.0",
|
"php": ">=7.4.0",
|
||||||
"php": ">=5.3.0"
|
"neos/flow": "^6.3.5 || ^7.0 || ^8.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"mikey179/vfsstream": "^1.6.1",
|
||||||
|
"neos/buildessentials": "^7.0.0",
|
||||||
|
"phpunit/phpunit": "~8.5",
|
||||||
|
"vimeo/psalm": "~4.22.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-0": {
|
"psr-4": {
|
||||||
"DigiComp\\SettingValidator": "Classes"
|
"DigiComp\\SettingValidator\\": "Classes/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"autoload-dev": {
|
||||||
|
"psr-4": {
|
||||||
|
"DigiComp\\SettingValidator\\Tests\\": "Tests/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"sort-packages": true,
|
||||||
|
"platform-check": true
|
||||||
|
},
|
||||||
"extra": {
|
"extra": {
|
||||||
"branch-alias": {
|
"branch-alias": {
|
||||||
|
"dev-develop": "3.2.x-dev",
|
||||||
|
"dev-version/2.x-dev": "2.1.x-dev",
|
||||||
"dev-version/1.x-dev": "1.0.x-dev"
|
"dev-version/1.x-dev": "1.0.x-dev"
|
||||||
},
|
},
|
||||||
|
"neos": {
|
||||||
|
"package-key": "DigiComp.SettingValidator"
|
||||||
|
},
|
||||||
"applied-flow-migrations": [
|
"applied-flow-migrations": [
|
||||||
"Inwebs.Basket-201409170938",
|
"Inwebs.Basket-201409170938",
|
||||||
"TYPO3.FLOW3-201201261636",
|
"TYPO3.FLOW3-201201261636",
|
||||||
|
@ -44,7 +65,26 @@
|
||||||
"TYPO3.Fluid-20141113120800",
|
"TYPO3.Fluid-20141113120800",
|
||||||
"TYPO3.Flow-20141113121400",
|
"TYPO3.Flow-20141113121400",
|
||||||
"TYPO3.Fluid-20141121091700",
|
"TYPO3.Fluid-20141121091700",
|
||||||
"TYPO3.Fluid-20150214130800"
|
"TYPO3.Fluid-20150214130800",
|
||||||
|
"TYPO3.Flow-20151113161300",
|
||||||
|
"TYPO3.Flow-20161115140400",
|
||||||
|
"TYPO3.Flow-20161115140430",
|
||||||
|
"Neos.Flow-20161124204700",
|
||||||
|
"Neos.Flow-20161124204701",
|
||||||
|
"Neos.Flow-20161124224015",
|
||||||
|
"Neos.Eel-20161124230101",
|
||||||
|
"Neos.Imagine-20161124231742",
|
||||||
|
"Neos.Media-20161124233100",
|
||||||
|
"Neos.Flow-20161125124112",
|
||||||
|
"Neos.SwiftMailer-20161130105617",
|
||||||
|
"TYPO3.FluidAdaptor-20161130112935",
|
||||||
|
"Neos.Media-20161219094126",
|
||||||
|
"Neos.Flow-20170125103800",
|
||||||
|
"Neos.Flow-20170127183102",
|
||||||
|
"DigiComp.SettingValidator-20170603120900",
|
||||||
|
"Neos.Flow-20180415105700",
|
||||||
|
"Neos.Flow-20190425144900",
|
||||||
|
"Neos.Flow-20190515215000"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue