No description
Find a file
Robin Krahnen a0488daa41
All checks were successful
ci/woodpecker/tag/code-style Pipeline was successful
ci/woodpecker/tag/functional-tests/1 Pipeline was successful
ci/woodpecker/tag/functional-tests/2 Pipeline was successful
ci/woodpecker/tag/functional-tests/3 Pipeline was successful
ci/woodpecker/tag/functional-tests/4 Pipeline was successful
update structure of composer.json and update dependencies
2024-10-11 14:30:45 +02:00
.woodpecker Allow recent flow versions 2023-02-18 23:55:38 +01:00
Classes add feature comment 2024-09-04 15:59:15 +02:00
Configuration/Testing reorder entries in Validation.yaml 2022-03-03 14:25:17 +01:00
Migrations/Code add "declare(strict_types=1);" 2022-05-02 09:56:09 +02:00
Resources/Private/Schema use double quotes in yaml files 2021-02-16 11:19:51 +01:00
Tests/Functional improve tests 2024-09-04 15:58:08 +02:00
CHANGELOG.md optimized CHANGELOG.md 2021-03-15 19:57:44 +01:00
composer.json update structure of composer.json and update dependencies 2024-10-11 14:30:45 +02:00
License.txt add empty line at end of License.txt 2021-09-22 15:00:26 +02:00
README.md optimized README.md 2021-02-16 12:47:06 +01:00

DigiComp.SettingValidator

This package allows configuring validators with a new configuration type.

Introduction

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.

/**
 * @Flow\Validate(type="DigiComp.SettingValidator:Settings", options={"name"="MyNamedValidator"})
 * @var MyObject
 */
protected MyObject $myObject;

The SettingsValidator will search for an entry inside the Validation.yaml with that name.

MyNamedValidator:
  ...

Resolving by type

To resolve the validation configuration by type just do not set the option name.

/**
 * @Flow\Validate(type="DigiComp.SettingValidator:Settings")
 * @var MyObject
 */
protected MyObject $myObject;

The SettingsValidator will search for an entry inside the Validation.yaml with the FQCN of MyObject.

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.

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.

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.

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:

/**
 * @Flow\Validate(type="My.Package:SomeValidator", options={"myOption"="myOptionValue"})
 * @Flow\Validate(type="My.Package:SomeOtherValidator")
 * @var MyObject
 */
protected MyObject $myObject;

New PHP code:

/**
 * @Flow\Validate(type="DigiComp.SettingValidator:Settings", options={"name"="MyNamedValidator"})
 * @var MyObject
 */
protected MyObject $myObject;

New validation configuration:

MyNamedValidator:
  self:
    'My.Package:SomeValidator':
      myOption: "myOptionValue"
    'My.Package:SomeOtherValidator': {}

Using on actions

Old PHP code:

/**
 * @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:

/**
 * @Flow\Validate(argumentName="myObject", type="DigiComp.SettingValidator:Settings", options={"name"="MyNamedValidator"})
 * @param MyObject $myObject
 */
public function myAction(MyObject $myObject)
{
    ...
}

New validation configuration:

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.

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.

/**
 * @Flow\Validate(argumentName="myObject", type="DigiComp.SettingValidator:Settings", options={"name"="MyNamedValidator"})
 * @param MyObject $myObject
 */
public function myAction(MyObject $myObject)
{
    ...
}
MyNamedValidator: {}