Skip to content

Commit

Permalink
fix(validator): fix usage of custom validators in schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
bloodhunterd committed Mar 16, 2024
1 parent 8d6dacd commit bec49c9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
32 changes: 32 additions & 0 deletions src/Component/Validator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,38 @@ Use the array type to define an array property which contains elements by schema
}
```

##### Custom validator

Use a custom validator class for a property. The key *parameters* is optional.

~~~json
{
"@type": "object",
"@properties": {
"foo": {
"@type": "object",
"@required": true,
"@properties": {
"foo": {
"@type": "object",
"@required": true,
"@validator": {
"@name": "use",
"@constraints": {
"class": "My\Application\Custom\Validator",
"parameters": {
"foo": "param 1",
"bar": "param 2"
}
}
}
}
}
}
}
}
~~~

#### Schema property validators
The schema definition can use the validators of this component to validate a schema property value.
```json
Expand Down
5 changes: 4 additions & 1 deletion src/Component/Validator/ValidatorChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Vection\Component\Validator;

use ReflectionException;
use Vection\Component\Validator\Validator\Color;
use Vection\Component\Validator\Validator\Required;
use Vection\Component\Validator\Validator\Locale;
Expand Down Expand Up @@ -162,10 +163,12 @@ public function __invoke(string ...$keys): ValidatorChain
* This magic method is used to accept several validation
* function which will be process later in the Validator.
*
* @param string $name
* @param string $name
* @param mixed[] $constraints
*
* @return ValidatorChain
*
* @throws ReflectionException
*/
public function __call(string $name, array $constraints = []): ValidatorChain
{
Expand Down
20 changes: 15 additions & 5 deletions src/Component/Validator/ValidatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,28 @@ class ValidatorFactory
*/
public function create(string $name, array $constraints = []): ValidatorInterface

Check failure on line 33 in src/Component/Validator/ValidatorFactory.php

View workflow job for this annotation

GitHub Actions / Static analysis on PHP 8.1

Method Vection\Component\Validator\ValidatorFactory::create() has parameter $constraints with no value type specified in iterable type array.
{
if (class_exists($name)) {
$validatorClassName = $name;
if ($name === 'use') {
$validatorClassName = $constraints['class'];

if (!in_array(ValidatorInterface::class, class_implements($name), true)) {
throw new RuntimeException('Validator does not implement Validator interface');
if (!class_exists($validatorClassName)) {
throw new RuntimeException(sprintf('Validator %s does not exist.', $validatorClassName));
}

if (!in_array(ValidatorInterface::class, class_implements($validatorClassName), true)) {
throw new RuntimeException(
sprintf('Validator %s does not implement required validator interface.', $validatorClassName)
);
}

if (array_key_exists('parameters', $constraints)) {
$constraints = $constraints['parameters'];
}
}
else {
$validatorClassName = __NAMESPACE__ .'\\Validator\\'. ucfirst($name);

if (!class_exists($validatorClassName)) {
throw new RuntimeException('Validator does not exists - '.$validatorClassName);
throw new RuntimeException(sprintf('Validator %s does not exist.', $validatorClassName));
}
}

Expand Down

0 comments on commit bec49c9

Please sign in to comment.