Skip to content

Commit

Permalink
Update the validation engine of composite-based rules
Browse files Browse the repository at this point in the history
This change will also make the composite-based rules require at least
two rules in their constructor because those rules do not make sense
with only one rule.

Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Feb 22, 2024
1 parent 41245f6 commit c04034c
Show file tree
Hide file tree
Showing 26 changed files with 232 additions and 558 deletions.
3 changes: 2 additions & 1 deletion docs/rules/AllOf.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AllOf

- `AllOf(Validatable ...$rule)`
- `AllOf(Validatable $rule1, Validatable $rule2, Validatable ...$rule)`

Will validate if all inner validators validates.

Expand All @@ -17,6 +17,7 @@ v::allOf(v::intVal(), v::positive())->validate(15); // true

Version | Description
--------|-------------
3.0.0 | Require at least two rules to be passed
0.3.9 | Created

***
Expand Down
3 changes: 2 additions & 1 deletion docs/rules/AnyOf.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AnyOf

- `AnyOf(Validatable ...$rule)`
- `AnyOf(Validatable $rule1, Validatable $rule2, Validatable ...$rule)`

This is a group validator that acts as an OR operator.

Expand All @@ -22,6 +22,7 @@ so `AnyOf()` returns true.

Version | Description
--------|-------------
3.0.0 | Require at least two rules to be passed
2.0.0 | Created

***
Expand Down
3 changes: 2 additions & 1 deletion docs/rules/KeySet.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# KeySet

- `KeySet(Key ...$rule)`
- `KeySet(Key $rule, Key ...$rules)`

Validates a keys in a defined structure.

Expand Down Expand Up @@ -57,6 +57,7 @@ The keys' order is not considered in the validation.

Version | Description
--------|-------------
3.0.0 | Require at one rule to be passed
2.3.0 | KeySet is NonNegatable, fixed message with extra keys
1.0.0 | Created

Expand Down
3 changes: 2 additions & 1 deletion docs/rules/NoneOf.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NoneOf

- `NoneOf(Validatable ...$rule)`
- `NoneOf(Validatable $rule1, Validatable $rule2, Validatable ...$rule)`

Validates if NONE of the given validators validate:

Expand All @@ -22,6 +22,7 @@ In the sample above, 'foo' isn't a integer nor a float, so noneOf returns true.

Version | Description
--------|-------------
3.0.0 | Require at least two rules to be passed
0.3.9 | Created

***
Expand Down
4 changes: 2 additions & 2 deletions docs/rules/OneOf.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# OneOf

- `OneOf(Validatable ...$rule)`
- `OneOf(Validatable $rule1, Validatable $rule2, Validatable ...$rule)`

Will validate if exactly one inner validator passes.

Expand All @@ -23,7 +23,7 @@ character, one or the other, but not neither nor both.

Version | Description
--------|-------------
2.0.0 | Changed to pass if only one inner validator passes
3.0.0 | Require at least two rules to be passed
0.3.9 | Created

***
Expand Down
15 changes: 6 additions & 9 deletions library/ChainedValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
namespace Respect\Validation;

use finfo;
use Respect\Validation\Rules\Key;

interface ChainedValidator extends Validatable
{
public function allOf(Validatable ...$rule): ChainedValidator;
public function allOf(Validatable $rule1, Validatable $rule2, Validatable ...$rule): ChainedValidator;

public function alnum(string ...$additionalChars): ChainedValidator;

Expand All @@ -24,7 +23,7 @@ public function alwaysInvalid(): ChainedValidator;

public function alwaysValid(): ChainedValidator;

public function anyOf(Validatable ...$rule): ChainedValidator;
public function anyOf(Validatable $rule1, Validatable $rule2, Validatable ...$rule): ChainedValidator;

public function arrayType(): ChainedValidator;

Expand Down Expand Up @@ -66,9 +65,7 @@ public function consonant(string ...$additionalChars): ChainedValidator;

public function contains(mixed $containsValue, bool $identical = false): ChainedValidator;

/**
* @param mixed[] $needles
*/
/** @param non-empty-array<mixed> $needles */
public function containsAny(array $needles, bool $strictCompareArray = false): ChainedValidator;

public function countable(): ChainedValidator;
Expand Down Expand Up @@ -176,7 +173,7 @@ public function keyNested(
bool $mandatory = true
): ChainedValidator;

public function keySet(Key ...$rule): ChainedValidator;
public function keySet(Validatable $rule, Validatable ...$rules): ChainedValidator;

public function lazyConsecutive(callable $ruleCreator, callable ...$ruleCreators): ChainedValidator;

Expand Down Expand Up @@ -219,7 +216,7 @@ public function nip(): ChainedValidator;

public function no(bool $useLocale = false): ChainedValidator;

public function noneOf(Validatable ...$rule): ChainedValidator;
public function noneOf(Validatable $rule1, Validatable $rule2, Validatable ...$rule): ChainedValidator;

public function not(Validatable $rule): ChainedValidator;

Expand All @@ -245,7 +242,7 @@ public function objectType(): ChainedValidator;

public function odd(): ChainedValidator;

public function oneOf(Validatable ...$rule): ChainedValidator;
public function oneOf(Validatable $rule1, Validatable $rule2, Validatable ...$rule): ChainedValidator;

public function optional(Validatable $rule): ChainedValidator;

Expand Down
121 changes: 0 additions & 121 deletions library/Rules/AbstractComposite.php

This file was deleted.

41 changes: 1 addition & 40 deletions library/Rules/AllOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace Respect\Validation\Rules;

use Respect\Validation\Attributes\ExceptionClass;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rule;
Expand All @@ -20,7 +18,6 @@
use function array_reduce;
use function count;

#[ExceptionClass(NestedValidationException::class)]
#[Template(
'These rules must pass for {{name}}',
'These rules must not pass for {{name}}',
Expand All @@ -31,7 +28,7 @@
'None of these rules must pass for {{name}}',
self::TEMPLATE_NONE,
)]
final class AllOf extends AbstractComposite
final class AllOf extends Composite
{
public const TEMPLATE_NONE = '__none__';
public const TEMPLATE_SOME = '__some__';
Expand All @@ -48,40 +45,4 @@ public function evaluate(mixed $input): Result

return (new Result($valid, $input, $this, $template))->withChildren(...$children);
}

public function assert(mixed $input): void
{
try {
parent::assert($input);
} catch (NestedValidationException $exception) {
if (count($exception->getChildren()) === count($this->getRules()) && !$exception->hasCustomTemplate()) {
$exception->updateTemplate(self::TEMPLATE_NONE);
}

throw $exception;
}
}

public function check(mixed $input): void
{
foreach ($this->getRules() as $rule) {
$rule->check($input);
}
}

public function validate(mixed $input): bool
{
foreach ($this->getRules() as $rule) {
if (!$rule->validate($input)) {
return false;
}
}

return true;
}

protected function getStandardTemplate(mixed $input): string
{
return self::TEMPLATE_SOME;
}
}
Loading

0 comments on commit c04034c

Please sign in to comment.