respect-validation/docs/validators.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

27 KiB

Validators

In this page you will find a list of validators by their category.

Arrays: ArrayType - ArrayVal - Contains - ContainsAny - ContainsCount - Each - EndsWith - In - Key - KeyExists - KeyOptional - KeySet - Sorted - StartsWith - Subset - Unique

Banking: CreditCard - Iban

Booleans: AlwaysInvalid - AlwaysValid - BoolType - BoolVal - FalseVal - TrueVal

Callables: After - CallableType - Factory - Satisfies

Comparisons: All - Between - BetweenExclusive - Equals - Equivalent - GreaterThan - GreaterThanOrEqual - Identical - In - Length - LessThan - LessThanOrEqual - Max - Min

Composite: AllOf - AnyOf - NoneOf - OneOf - ShortCircuit

Conditions: Not - ShortCircuit - When

Core: Named - Not - Templated

Date and Time: Date - DateTime - DateTimeDiff - LeapDate - LeapYear - Time

Display: Format - Masked - Named - Templated

File system: Directory - Executable - Exists - Extension - File - Image - Mimetype - Readable - Size - SymbolicLink - Writable

ISO codes: CountryCode - CurrencyCode - LanguageCode - SubdivisionCode

Identifications: Bsn - Cnh - Cnpj - Cpf - Hetu - Imei - Isbn - Luhn - MacAddress - NfeAccessKey - Nif - Nip - Pesel - Pis - PolishIdCard - PortugueseNif

Internet: Domain - Email - Ip - PublicDomainSuffix - Tld - Url

Localization: CountryCode - CurrencyCode - LanguageCode - PostalCode - SubdivisionCode

Math: Factor - Finite - Infinite - Multiple - Negative - Positive

Miscellaneous: Blank - Falsy - Masked - Named - Templated - Undef

Nesting: After - AllOf - AnyOf - Each - Factory - Key - KeySet - NoneOf - Not - NullOr - OneOf - Property - PropertyOptional - ShortCircuit - UndefOr - When

Numbers: Base - Decimal - Digit - Even - Factor - Finite - FloatType - FloatVal - Infinite - IntType - IntVal - Multiple - Negative - Number - NumericVal - Odd - Positive - Roman

Objects: Attributes - Instance - ObjectType - Property - PropertyExists - PropertyOptional

Strings: Alnum - Alpha - Base64 - Charset - Consonant - Contains - ContainsAny - ContainsCount - Control - Digit - Emoji - EndsWith - Format - Graph - HexRgbColor - In - Json - Lowercase - Phone - PostalCode - Printable - Punct - Regex - Slug - Sorted - Space - Spaced - StartsWith - StringType - StringVal - Uppercase - Uuid - Version - Vowel - Xdigit

Structures: Attributes - Key - KeyExists - KeyOptional - KeySet - Property - PropertyExists - PropertyOptional

Transformations: After - All - Each - Length - Max - Min - Size

Types: ArrayType - ArrayVal - BoolType - BoolVal - CallableType - Countable - FloatType - FloatVal - IntType - IntVal - IterableType - IterableVal - NullType - NumericVal - ObjectType - ResourceType - ScalarVal - StringType - StringVal

Alphabetically

  • After - v::after(str_split(...), v::arrayType()->lengthEquals(5))->assert('world');
  • All - v::all(v::dateTime())->assert($releaseDates);
  • AllOf - v::allOf(v::intVal(), v::positive())->assert(15);
  • Alnum - v::alnum(' ')->assert('foo 123');
  • Alpha - v::alpha(' ')->assert('some name');
  • AlwaysInvalid - v::not(v::alwaysInvalid())->assert('whatever');
  • AlwaysValid - v::alwaysValid()->assert('whatever');
  • AnyOf - v::anyOf(v::intVal(), v::floatVal())->assert(15.5);
  • ArrayType - v::arrayType()->assert([]);
  • ArrayVal - v::arrayVal()->assert([]);
  • Attributes - v::attributes()->assert(new Person('John Doe', '2020-06-23', 'john.doe@gmail.com'));
  • Base - v::base(2)->assert('011010001');
  • Base64 - v::base64()->assert('cmVzcGVjdCE=');
  • Between - v::intVal()->between(10, 20)->assert(10);
  • BetweenExclusive - v::betweenExclusive('a', 'e')->assert('c');
  • Blank - v::blank()->assert(' ');
  • BoolType - v::boolType()->assert(true);
  • BoolVal - v::boolVal()->assert('on');
  • Bsn - v::bsn()->assert('612890053');
  • CallableType - v::callableType()->assert(function () {});
  • Charset - v::charset('ASCII')->assert('sugar');
  • Cnh - v::cnh()->assert('02650306461');
  • Cnpj - v::cnpj()->assert('00394460005887');
  • Consonant - v::consonant()->assert('xkcd');
  • Contains - v::contains('ipsum')->assert('lorem ipsum');
  • ContainsAny - v::containsAny(['lorem', 'dolor'])->assert('lorem ipsum');
  • ContainsCount - v::containsCount('ipsum', 2)->assert('ipsum lorem ipsum');
  • Control - v::control()->assert("\n\r\t");
  • Countable - v::countable()->assert([]);
  • CountryCode - v::countryCode()->assert('BR');
  • Cpf - v::cpf()->assert('95574461102');
  • CreditCard - v::creditCard()->assert('5376 7473 9720 8720');
  • CurrencyCode - v::currencyCode()->assert('GBP');
  • Date - v::date()->assert('2017-12-31');
  • DateTime - v::dateTime()->assert('2009-01-01');
  • DateTimeDiff - v::dateTimeDiff('years', v::greaterThan(18), 'd/m/Y')->assert('09/12/1990');
  • Decimal - v::decimal(2)->assert('27990.50');
  • Digit - v::digit(' ')->assert('020 612 1851');
  • Directory - v::directory()->assert(__DIR__);
  • Domain - v::domain()->assert('google.com');
  • Each - v::each(v::dateTime())->assert($releaseDates);
  • Email - v::email()->assert('alganet@gmail.com');
  • Emoji - v::emoji()->assert('🍕');
  • EndsWith - v::endsWith('ipsum')->assert('lorem ipsum');
  • Equals - v::equals('alganet')->assert('alganet');
  • Equivalent - v::equivalent(1)->assert(true);
  • Even - v::intVal()->even()->assert(2);
  • Executable - v::executable()->assert('/path/to/executable');
  • Exists - v::exists()->assert(__FILE__);
  • Extension - v::extension('png')->assert('image.png');
  • Factor - v::factor(0)->assert(5);
  • Factory - v::factory(static fn($input) => v::boolVal())->assert(true);
  • FalseVal - v::falseVal()->assert(false);
  • Falsy - v::falsy()->assert('');
  • File - v::file()->assert(__FILE__);
  • Finite - v::finite()->assert('10');
  • FloatType - v::floatType()->assert(1.5);
  • FloatVal - v::floatVal()->assert(1.5);
  • Format - v::format(f::pattern('00-00'))->assert('42-33');
  • Graph - v::graph()->assert('LKM@#$%4;');
  • GreaterThan - v::greaterThan(10)->assert(11);
  • GreaterThanOrEqual - v::intVal()->greaterThanOrEqual(10)->assert(10);
  • Hetu - v::hetu()->assert('010106A9012');
  • HexRgbColor - v::hexRgbColor()->assert('#FFFAAA');
  • Iban - v::iban()->assert('SE35 5000 0000 0549 1000 0003');
  • Identical - v::identical(42)->assert(42);
  • Image - v::image()->assert('/path/to/image.gif');
  • Imei - v::imei()->assert('35-209900-176148-1');
  • In - v::in('lorem ipsum')->assert('ipsum');
  • Infinite - v::infinite()->assert(INF);
  • Instance - v::instance('DateTime')->assert(new DateTime);
  • IntType - v::intType()->assert(42);
  • IntVal - v::intVal()->assert('10');
  • Ip - v::ip()->assert('127.0.0.1');
  • Isbn - v::isbn()->assert('ISBN-13: 978-0-596-52068-7');
  • IterableType - v::iterableType()->assert([]);
  • IterableVal - v::iterableVal()->assert([]);
  • Json - v::json()->assert('{"foo":"bar"}');
  • Key - v::key('name', v::stringType())->assert(['name' => 'The Respect Panda']);
  • KeyExists - v::keyExists('name')->assert(['name' => 'The Respect Panda']);
  • KeyOptional - v::keyOptional('name', v::stringType())->assert([]);
  • KeySet - v::keySet(v::key('foo', v::intVal()))->assert(['foo' => 42]);
  • LanguageCode - v::languageCode()->assert('pt');
  • LeapDate - v::leapDate('Y-m-d')->assert('1988-02-29');
  • LeapYear - v::leapYear()->assert('1988');
  • Length - v::length(v::between(1, 5))->assert('abc');
  • LessThan - v::lessThan(10)->assert(9);
  • LessThanOrEqual - v::lessThanOrEqual(10)->assert(9);
  • Lowercase - v::stringType()->lowercase()->assert('xkcd');
  • Luhn - v::luhn()->assert('2222400041240011');
  • MacAddress - v::macAddress()->assert('00:11:22:33:44:55');
  • Masked - v::masked('1-@', v::email())->assert('foo@example.com');
  • Max - v::max(v::equals(30))->assert([10, 20, 30]);
  • Mimetype - v::mimetype('image/png')->assert('/path/to/image.png');
  • Min - v::min(v::equals(10))->assert([10, 20, 30]);
  • Multiple - v::intVal()->multiple(3)->assert(9);
  • Named - v::named('Your email', v::email())->assert('foo@example.com');
  • Negative - v::numericVal()->negative()->assert(-15);
  • NfeAccessKey - v::nfeAccessKey()->assert('52060433009911002506550120000007800267301615');
  • Nif - v::nif()->assert('49294492H');
  • Nip - v::nip()->assert('1645865777');
  • NoneOf - v::noneOf(v::intVal(), v::floatVal())->assert('foo');
  • Not - v::not(v::ip())->assert('foo');
  • NullOr - v::nullOr(v::email())->assert(null);
  • NullType - v::nullType()->assert(null);
  • Number - v::number()->assert(42);
  • NumericVal - v::numericVal()->assert(-12);
  • ObjectType - v::objectType()->assert(new stdClass);
  • Odd - v::odd()->assert(3);
  • OneOf - v::oneOf(v::digit(), v::alpha())->assert('AB');
  • Pesel - v::pesel()->assert('21120209256');
  • Phone - v::phone()->assert('+1 650 253 00 00');
  • Pis - v::pis()->assert('120.0340.678-8');
  • PolishIdCard - v::polishIdCard()->assert('AYW036733');
  • PortugueseNif - v::portugueseNif()->assert('124885446');
  • Positive - v::positive()->assert(1);
  • PostalCode - v::postalCode('BR')->assert('02179000');
  • Printable - v::printable()->assert('LMKA0$% _123');
  • Property - v::property('name', v::equals('The Respect Panda'))->assert($object);
  • PropertyExists - v::propertyExists('name')->assert($object);
  • PropertyOptional - v::propertyOptional('name', v::notBlank())->assert($object);
  • PublicDomainSuffix - v::publicDomainSuffix()->assert('co.uk');
  • Punct - v::punct()->assert('&,.;[]');
  • Readable - v::readable()->assert('/path/to/file.txt');
  • Regex - v::regex('/[a-z]/')->assert('a');
  • ResourceType - v::resourceType()->assert(fopen('/path/to/file.txt', 'r'));
  • Roman - v::roman()->assert('IV');
  • Satisfies - v::satisfies(fn (int $input): bool => $input % 5 === 0,)->assert(10);
  • ScalarVal - v::scalarVal()->assert(135.0);
  • ShortCircuit - v::shortCircuit(v::intVal(), v::positive())->assert(15);
  • Size - v::size('KB', v::greaterThan(1))->assert('/path/to/file');
  • Slug - v::slug()->assert('my-wordpress-title');
  • Sorted - v::sorted('ASC')->assert([1, 2, 3]);
  • Space - v::space()->assert(' ');
  • Spaced - v::spaced()->assert('foo bar');
  • StartsWith - v::startsWith('lorem')->assert('lorem ipsum');
  • StringType - v::stringType()->assert('hi');
  • StringVal - v::stringVal()->assert('6');
  • SubdivisionCode - v::subdivisionCode('BR')->assert('SP');
  • Subset - v::subset([1, 2, 3])->assert([1, 2]);
  • SymbolicLink - v::symbolicLink()->assert('/path/to/symbolic-link');
  • Templated - v::templated('You must provide a valid email', v::email())->assert('foo@bar.com');
  • Time - v::time()->assert('00:00:00');
  • Tld - v::tld()->assert('com');
  • TrueVal - v::trueVal()->assert(true);
  • Undef - v::undef()->assert('');
  • UndefOr - v::undefOr(v::alpha())->assert('');
  • Unique - v::unique()->assert([]);
  • Uppercase - v::uppercase()->assert('W3C');
  • Url - v::url()->assert('http://example.com');
  • Uuid - v::uuid()->assert('eb3115e5-bd16-4939-ab12-2b95745a30f3');
  • Version - v::version()->assert('1.0.0');
  • Vowel - v::vowel()->assert('aei');
  • When - v::when(v::intVal(), v::positive(), v::notBlank())->assert(1);
  • Writable - v::writable()->assert('/path/to/file');
  • Xdigit - v::xdigit()->assert('abc123');