respect-validation/docs/validators/Circuit.md
Alexandre Gomes Gaigalas 2a7f345e32 Streamline validators.md index
Makes it so the index looks more like a cheatsheet, condensing
information instead of making long lists that require lots of
scrolling to explore.

Additionally, the happy path for each validator was also
added, providing a quick reference use for comparison.

The direct markdown links were replaced by titled markdown
references, offering mouse-over tooltips over links that
display the validator one-line description.

To ensure a proper source of truth for these new index
goodies, the AssertionMessageLinter was modified to
verify that the first assertion in each doc is a
single-line validator that passes (a happy path), further
making our documentation conventions more solid.
2026-01-28 12:47:08 +00:00

2.1 KiB

Circuit

  • Circuit(Validator $validator1, Validator $validator2)
  • Circuit(Validator $validator1, Validator $validator2, Validator ...$validators)

Validates the input against a series of validators until the first fails.

v::circuit(v::intVal(), v::floatVal())->assert(15);
// Validation passes successfully

This validator can be handy for getting the least error messages possible from a chain.

This validator can be helpful in combinations with Lazy. An excellent example is when you want to validate a country code and a subdivision code.

$validator = v::circuit(
    v::key('countryCode', v::countryCode()),
    v::lazy(static fn($input) => v::key('subdivisionCode', v::subdivisionCode($input['countryCode']))),
);

$validator->assert([]);
// → `.countryCode` must be present

$validator->assert(['countryCode' => 'US']);
// → `.subdivisionCode` must be present

$validator->assert(['countryCode' => 'US', 'subdivisionCode' => 'ZZ']);
// → `.subdivisionCode` must be a subdivision code of United States

$validator->assert(['countryCode' => 'US', 'subdivisionCode' => 'CA']);
// Validation passes successfully

You need a valid country code to create a SubdivisionCode, so it makes sense only to validate the subdivision code only if the country code is valid. In this case, you could also have used When, but you would then have to write v::key('countryCode', v::countryCode()) twice in your chain.

Templates

This validator does not have any templates, because it will always return the result of the first validator that fails. When all the validators pass, it will return the result of the last validator of the circuit.

Categorization

  • Composite
  • Conditions
  • Nesting

Changelog

Version Description
3.0.0 Created

See Also