respect-validation/docs/validators/After.md
Henrique Moody b701fac656
Create ShortCircuit validator and ShortCircuitable interface
This commit introduces a mechanism for validators to return early once
the validation outcome is determined, rather than evaluating all child
validators.

The ShortCircuit validator evaluates validators sequentially and stops
at the first failure, similar to how PHP's && operator works. This is
useful when later validators depend on earlier ones passing, or when
you want only the first error message.

The ShortCircuitCapable interface allows composite validators (AllOf,
AnyOf, OneOf, NoneOf, Each, All) to implement their own short-circuit
logic.

Why "ShortCircuit" instead of "FailFast":

The name "FailFast" was initially considered but proved misleading.
While AllOf stops on failure (fail fast), AnyOf stops on success
(succeed fast), and OneOf stops on the second success. The common
behavior is not about failing quickly, but about returning as soon as
the outcome is determined—which is exactly what short-circuit
evaluation means. This terminology is familiar to developers from
boolean operators (&& and ||), making the behavior immediately
understandable.

Co-authored-by: Alexandre Gomes Gaigalas <alganet@gmail.com>
Assisted-by: Claude Code (Opus 4.5)
2026-02-05 17:32:42 +01:00

91 lines
2.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!--
SPDX-License-Identifier: MIT
SPDX-FileCopyrightText: (c) Respect Project Contributors
SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
-->
# After
- `After(callable $callable, Validator $validator)`
Validates the input after applying a [callable][] to it.
```php
v::after(str_split(...), v::arrayType()->lengthEquals(5))->assert('world');
// Validation passes successfully
```
Consider the following variable:
```php
$url = 'http://www.google.com/search?q=respect.github.com';
```
To validate every part of this URL we could use the native `parse_url`
function to break its parts:
```php
$parts = parse_url($url);
```
This function returns an array containing `scheme`, `host`, `path` and `query`.
We can validate them this way:
```php
v::arrayVal()
->key('scheme', v::startsWith('http'))
->key('host', v::domain())
->key('path', v::stringType())
->key('query', v::notBlank());
```
Using `v::call()` you can do this in a single chain:
```php
v::after(
'parse_url',
v::arrayVal()
->key('scheme', v::startsWith('http'))
->key('host', v::domain())
->key('path', v::stringType())
->key('query', v::notBlank())
)->assert($url);
// Validation passes successfully
```
`After` does not handle possible errors (type mismatches). If you need to
ensure that your callback is of a certain type, use [ShortCircuit](ShortCircuit.md) or
handle it using a closure:
```php
v::after('strtolower', v::equals('ABC'))->assert(123);
// 𝙭 strtolower(): Argument #1 ($string) must be of type string, int given
v::shortCircuit(v::stringType(), v::after('strtolower', v::equals('abc')))->assert(123);
// → 123 must be a string
v::shortCircuit(v::stringType(), v::after('strtolower', v::equals('abc')))->assert('ABC');
// Validation passes successfully
```
## Categorization
- Callables
- Nesting
- Transformations
## Changelog
| Version | Description |
| ------: | :------------------------------------------------------- |
| 3.0.0 | No longer sets error handlers and got renamed to `After` |
| 2.0.0 | Sets error handlers |
| 0.3.9 | Created as `Call` |
## See Also
- [Each](Each.md)
- [Factory](Factory.md)
- [Satisfies](Satisfies.md)
- [Sorted](Sorted.md)