respect-validation/tests/feature/Validators/OneOfTest.php
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

104 lines
4.6 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: fail, fail', catchAll(
fn() => v::oneOf(v::intType(), v::negative())->assert('string'),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('"string" must be an integer')
->and($fullMessage)->toBe(<<<'FULL_MESSAGE'
- "string" must pass one of the rules
- "string" must be an integer
- "string" must be a negative number
FULL_MESSAGE)
->and($messages)->toBe([
'__root__' => '"string" must pass one of the rules',
'intType' => '"string" must be an integer',
'negative' => '"string" must be a negative number',
]),
));
test('Default: fail, pass, pass', catchAll(
fn() => v::oneOf(v::intType(), v::stringType(), v::alpha())->assert('string'),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('"string" must be an integer')
->and($fullMessage)->toBe(<<<'FULL_MESSAGE'
- "string" must pass only one of the rules
- "string" must be an integer
- "string" must be a string
- "string" must consist only of letters (a-z)
FULL_MESSAGE)
->and($messages)->toBe([
'__root__' => '"string" must pass only one of the rules',
'intType' => '"string" must be an integer',
'stringType' => '"string" must be a string',
'alpha' => '"string" must consist only of letters (a-z)',
]),
));
test('Default: pass, fail, pass', catchAll(
fn() => v::oneOf(v::stringType(), v::intType(), v::alpha())->assert('string'),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('"string" must be an integer')
->and($fullMessage)->toBe(<<<'FULL_MESSAGE'
- "string" must pass only one of the rules
- "string" must be an integer
- "string" must be a string
- "string" must consist only of letters (a-z)
FULL_MESSAGE)
->and($messages)->toBe([
'__root__' => '"string" must pass only one of the rules',
'intType' => '"string" must be an integer',
'stringType' => '"string" must be a string',
'alpha' => '"string" must consist only of letters (a-z)',
]),
));
test('Default: pass, pass, fail', catchAll(
fn() => v::oneOf(v::stringType(), v::alpha(), v::intType())->assert('string'),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('"string" must be an integer')
->and($fullMessage)->toBe(<<<'FULL_MESSAGE'
- "string" must pass only one of the rules
- "string" must be an integer
- "string" must be a string
- "string" must consist only of letters (a-z)
FULL_MESSAGE)
->and($messages)->toBe([
'__root__' => '"string" must pass only one of the rules',
'intType' => '"string" must be an integer',
'stringType' => '"string" must be a string',
'alpha' => '"string" must consist only of letters (a-z)',
]),
));
test('Inverted: fail, pass', catchAll(
fn() => v::not(v::oneOf(v::intType(), v::positive()))->assert(-1),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('-1 must not be an integer')
->and($fullMessage)->toBe('- -1 must not be an integer')
->and($messages)->toBe(['intType' => '-1 must not be an integer']),
));
test('Inverted: fail, fail, pass', catchAll(
fn() => v::not(v::oneOf(v::stringType(), v::alpha(), v::negative()))->assert(-1),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('-1 must not be a negative number')
->and($fullMessage)->toBe('- -1 must not be a negative number')
->and($messages)->toBe(['negative' => '-1 must not be a negative number']),
));
test('short-circuit: inverted when one validator passes', catchAll(
fn() => v::not(v::shortCircuit(v::oneOf(v::stringType(), v::intType())))->assert(5),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('5 must not be an integer')
->and($fullMessage)->toBe('- 5 must not be an integer')
->and($messages)->toBe(['intType' => '5 must not be an integer']),
));