From 9a055a2f7744d8cd17fdf9269b7a1edb2338813d Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Fri, 2 Jun 2017 23:14:05 +0200 Subject: [PATCH 1/3] TASK: Completely changing the layout of Validation.yaml --- .../Validator/SettingsValidator.php | 26 ++++++++++++++++++- Configuration/Testing/Validation.yaml | 26 ++++++++----------- .../Private/Schema/Validation.schema.yaml | 12 ++++----- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/Classes/Validation/Validator/SettingsValidator.php b/Classes/Validation/Validator/SettingsValidator.php index 112bdc4..c94efff 100644 --- a/Classes/Validation/Validator/SettingsValidator.php +++ b/Classes/Validation/Validator/SettingsValidator.php @@ -75,7 +75,31 @@ class SettingsValidator extends AbstractValidator ); } - $config = &$this->validations[$name]; + $config = []; + if (isset($this->validations[$name]['self'])) { + foreach ($this->validations[$name]['self'] as $validator => &$validation) { + if (is_null($validation)) { + continue; + } + $newValidation['options'] = $validation; + $newValidation['validator'] = $validator; + $config[] = $newValidation; + } + } + if (isset($this->validations[$name]['properties'])) { + foreach ($this->validations[$name]['properties'] as $propertyName => &$validation) { + foreach ($validation as $validator => &$options) { + if (is_null($options)) { + continue; + } + $newValidation['property'] = $propertyName; + $newValidation['validator'] = $validator; + $newValidation['options'] = $options; + $config[] = $newValidation; + } + } + } + foreach ($config as $validatorConfig) { $validator = $this->validatorResolver->createValidator( $validatorConfig['validator'], diff --git a/Configuration/Testing/Validation.yaml b/Configuration/Testing/Validation.yaml index f9575d2..bc17503 100644 --- a/Configuration/Testing/Validation.yaml +++ b/Configuration/Testing/Validation.yaml @@ -1,18 +1,14 @@ DigiComp\SettingValidator\Tests\Functional\Fixtures\TestObject: - - - property: shouldBeTrue - validator: BooleanValue - options: - expectedValue: true - - - property: shouldBeFalse - validator: BooleanValue - options: - expectedValue: false - + properties: + shouldBeTrue: + BooleanValue: + expectedValue: true + shouldBeFalse: + BooleanValue: + expectedValue: false + Grumble: ~ TrueValidator: - - - validator: BooleanValue - options: - expectedValue: true \ No newline at end of file + self: + BooleanValue: + expectedValue: true diff --git a/Resources/Private/Schema/Validation.schema.yaml b/Resources/Private/Schema/Validation.schema.yaml index b917695..3f96d9d 100644 --- a/Resources/Private/Schema/Validation.schema.yaml +++ b/Resources/Private/Schema/Validation.schema.yaml @@ -1,11 +1,11 @@ type: dictionary additionalProperties: - type: array - items: + type: dictionary + additionalProperties: false + properties: type: dictionary additionalProperties: false - # This validation sadly only hits the first level of validation, and options are not coverable with current validator properties: - validator: {type: string, required: true} - options: {type: dictionary, required: true} - property: {type: string, required: false} + type: dictionary + self: + type: dictionary From 4534099d3a196cbb9083d4cf4317057b4928dfa2 Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Sat, 3 Jun 2017 12:56:14 +0200 Subject: [PATCH 2/3] TASK: Added code migration for Validation.yaml structure --- Classes/Package.php | 17 +++++- .../Validator/SettingsValidator.php | 10 ++++ Migrations/Code/Version20170603120900.php | 55 +++++++++++++++++++ composer.json | 5 +- 4 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 Migrations/Code/Version20170603120900.php diff --git a/Classes/Package.php b/Classes/Package.php index c821e77..06276a7 100644 --- a/Classes/Package.php +++ b/Classes/Package.php @@ -1,16 +1,27 @@ registerConfigurationType( - 'Validation', + static::CONFIGURATION_TYPE_VALIDATION, ConfigurationManager::CONFIGURATION_PROCESSING_TYPE_DEFAULT, true ); diff --git a/Classes/Validation/Validator/SettingsValidator.php b/Classes/Validation/Validator/SettingsValidator.php index c94efff..4a87bd9 100644 --- a/Classes/Validation/Validator/SettingsValidator.php +++ b/Classes/Validation/Validator/SettingsValidator.php @@ -1,6 +1,16 @@ processConfiguration(Package::CONFIGURATION_TYPE_VALIDATION, + function (&$configuration) { + foreach ($configuration as $validatorName => &$validators) { + $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 be migrated.'); + continue; + } + if (isset($validator['property'])) { + $newConfiguration['properties'][$validator['property']][$validator['validator']] + = $validator['options']; + } else { + $newConfiguration['self'][$validator['validator']] = $validator['options']; + } + } + $validators = $newConfiguration; + } + }, + true + ); + } +} diff --git a/composer.json b/composer.json index a3cd64d..65020c7 100644 --- a/composer.json +++ b/composer.json @@ -65,7 +65,8 @@ "TYPO3.FluidAdaptor-20161130112935", "Neos.Media-20161219094126", "Neos.Flow-20170125103800", - "Neos.Flow-20170127183102" + "Neos.Flow-20170127183102", + "DigiComp.SettingValidator-20170603120900" ] } -} +} \ No newline at end of file From d83c009b28108adff94696cc252162d962b594f4 Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Sat, 3 Jun 2017 14:11:13 +0200 Subject: [PATCH 3/3] TASK: taming complexity --- .../Validator/SettingsValidator.php | 58 ++++++++++++------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/Classes/Validation/Validator/SettingsValidator.php b/Classes/Validation/Validator/SettingsValidator.php index 4a87bd9..c10655f 100644 --- a/Classes/Validation/Validator/SettingsValidator.php +++ b/Classes/Validation/Validator/SettingsValidator.php @@ -85,6 +85,41 @@ class SettingsValidator extends AbstractValidator ); } + $config = $this->getConfigForName($name); + + foreach ($config as $validatorConfig) { + $validator = $this->validatorResolver->createValidator( + $validatorConfig['validator'], + $validatorConfig['options'] + ); + + if (! $validator) { + throw new InvalidValidationConfigurationException( + sprintf( + 'Validator could not be resolved: "%s" Check your Validation.yaml', + $validatorConfig['validator'] + ), + 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)); + } + } + } + + /** + * @param $name + * + * @return array + */ + protected function getConfigForName($name): array + { $config = []; if (isset($this->validations[$name]['self'])) { foreach ($this->validations[$name]['self'] as $validator => &$validation) { @@ -109,27 +144,6 @@ class SettingsValidator extends AbstractValidator } } } - - 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)); - } - } + return $config; } }