mirror of
https://github.com/Respect/Validation.git
synced 2026-03-15 14:55:44 +01:00
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)
113 lines
5.9 KiB
PHP
113 lines
5.9 KiB
PHP
<?php
|
|
|
|
/*
|
|
* 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>
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
test('Default', catchAll(
|
|
fn() => v::shortCircuit(v::alwaysValid(), v::trueVal())->assert(false),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('`false` must evaluate to `true`')
|
|
->and($fullMessage)->toBe('- `false` must evaluate to `true`')
|
|
->and($messages)->toBe(['trueVal' => '`false` must evaluate to `true`']),
|
|
));
|
|
|
|
test('With recursive', catchAll(
|
|
fn() => v::shortCircuit(v::each(v::intType()))->assert([1, 2, '3', 4]),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('`.2` must be an integer')
|
|
->and($fullMessage)->toBe('- `.2` must be an integer')
|
|
->and($messages)->toBe([2 => '`.2` must be an integer']),
|
|
));
|
|
|
|
test('Inverted', catchAll(
|
|
fn() => v::not(v::shortCircuit(v::alwaysValid(), v::trueVal()))->assert(true),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('`true` must not evaluate to `true`')
|
|
->and($fullMessage)->toBe('- `true` must not evaluate to `true`')
|
|
->and($messages)->toBe(['notTrueVal' => '`true` must not evaluate to `true`']),
|
|
));
|
|
|
|
test('Default with inverted failing rule', catchAll(
|
|
fn() => v::shortCircuit(v::alwaysValid(), v::not(v::trueVal()))->assert(true),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('`true` must not evaluate to `true`')
|
|
->and($fullMessage)->toBe('- `true` must not evaluate to `true`')
|
|
->and($messages)->toBe(['notTrueVal' => '`true` must not evaluate to `true`']),
|
|
));
|
|
|
|
test('With wrapped name, default', catchAll(
|
|
fn() => v::named('Wrapper', v::shortCircuit(v::alwaysValid(), v::named('Wrapped', v::trueVal())))->assert(false),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('Wrapped must evaluate to `true`')
|
|
->and($fullMessage)->toBe('- Wrapped must evaluate to `true`')
|
|
->and($messages)->toBe(['trueVal' => 'Wrapped must evaluate to `true`']),
|
|
));
|
|
|
|
test('With wrapper name, default', catchAll(
|
|
fn() => v::named('Wrapper', v::shortCircuit(v::alwaysValid(), v::trueVal()))->assert(false),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('Wrapper must evaluate to `true`')
|
|
->and($fullMessage)->toBe('- Wrapper must evaluate to `true`')
|
|
->and($messages)->toBe(['trueVal' => 'Wrapper must evaluate to `true`']),
|
|
));
|
|
|
|
test('With the name set in the wrapped rule of an inverted failing rule', catchAll(
|
|
fn() => v::named('Wrapper', v::shortCircuit(v::alwaysValid(), v::named('Not', v::not(v::named('Wrapped', v::trueVal())))))->assert(true),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('Wrapped must not evaluate to `true`')
|
|
->and($fullMessage)->toBe('- Wrapped must not evaluate to `true`')
|
|
->and($messages)->toBe(['notTrueVal' => 'Wrapped must not evaluate to `true`']),
|
|
));
|
|
|
|
test('With the name set in an inverted failing rule', catchAll(
|
|
fn() => v::named('Wrapper', v::shortCircuit(v::alwaysValid(), v::named('Not', v::not(v::trueVal()))))->assert(true),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('Not must not evaluate to `true`')
|
|
->and($fullMessage)->toBe('- Not must not evaluate to `true`')
|
|
->and($messages)->toBe(['notTrueVal' => 'Not must not evaluate to `true`']),
|
|
));
|
|
|
|
test('With the name set in the "shortCircuit" that has an inverted failing rule', catchAll(
|
|
fn() => v::named('Wrapper', v::shortCircuit(v::alwaysValid(), v::not(v::trueVal())))->assert(true),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('Wrapper must not evaluate to `true`')
|
|
->and($fullMessage)->toBe('- Wrapper must not evaluate to `true`')
|
|
->and($messages)->toBe(['notTrueVal' => 'Wrapper must not evaluate to `true`']),
|
|
));
|
|
|
|
test('With template', catchAll(
|
|
fn() => v::templated('ShortCircuit cool cats cunningly continuous cookies', v::shortCircuit(v::alwaysValid(), v::trueVal()))
|
|
->assert(false),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('ShortCircuit cool cats cunningly continuous cookies')
|
|
->and($fullMessage)->toBe('- ShortCircuit cool cats cunningly continuous cookies')
|
|
->and($messages)->toBe(['trueVal' => 'ShortCircuit cool cats cunningly continuous cookies']),
|
|
));
|
|
|
|
test('With multiple templates', catchAll(
|
|
fn() => v::shortCircuit(v::alwaysValid(), v::trueVal())
|
|
->assert(false, ['trueVal' => 'Clever clowns craft shortCircuit clever clocks']),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('Clever clowns craft shortCircuit clever clocks')
|
|
->and($fullMessage)->toBe('- Clever clowns craft shortCircuit clever clocks')
|
|
->and($messages)->toBe(['trueVal' => 'Clever clowns craft shortCircuit clever clocks']),
|
|
));
|
|
|
|
test('Real example', catchAll(
|
|
fn() => v::shortCircuit(
|
|
v::key('countyCode', v::countryCode()),
|
|
v::factory(
|
|
fn($input) => v::key('subdivisionCode', v::subdivisionCode($input['countyCode'])),
|
|
),
|
|
)->assert(['countyCode' => 'BR', 'subdivisionCode' => 'CA']),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('`.subdivisionCode` must be a subdivision code of Brazil')
|
|
->and($fullMessage)->toBe('- `.subdivisionCode` must be a subdivision code of Brazil')
|
|
->and($messages)->toBe(['subdivisionCode' => '`.subdivisionCode` must be a subdivision code of Brazil']),
|
|
));
|