docs/prefixes.md
Prefixes are convenience methods that make complex validators easier to use. They are dynamically generated to simplify the creation of common validation patterns.
Prefixes are automatically created for the following validators:
These prefixes allow you to write more concise validators. For example, v::allEmoji() is automatically transformed into v::all(v::emoji()).
The prefix system works by detecting method names that start with specific prefixes and automatically wrapping the remaining part of the method name with the appropriate validator. This transformation happens internally, so you can use these prefixes seamlessly.
allAny method starting with all will be transformed into v::all(v::remainingPart()).
v::allEmoji() → v::all(v::emoji())v::allIntType() → v::all(v::intType())v::allPositive() → v::all(v::positive())v::allEmoji()->assert(['😀', '😁', '😂'])
// Validation passes successfully
v::allEmoji()->assert(['😀', 'abc', '😂']);
// → Every item in `["😀", "abc", "😂"]` must be an emoji
lengthAny method starting with length will be transformed into v::length(v::remainingPart()).
v::lengthBetween(5, 10) → v::length(v::between(5, 10))v::lengthGreaterThan(3) → v::length(v::greaterThan(3))v::lengthBetween(5, 10)->assert('hello')
// Validation passes successfully
v::lengthBetween(5, 10)->assert('hi');
// → The length of "hi" must be between 5 and 10
maxAny method starting with max will be transformed into v::max(v::remainingPart()).
v::maxLessThan(100) → v::max(v::lessThan(100))v::maxPositive() → v::max(v::positive())v::maxLessThan(100)->assert([99, 50, 1])
// Validation passes successfully
v::maxLessThan(100)->assert([100, 50, 1]);
// → The maximum of `[100, 50, 1]` must be less than 100
minAny method starting with min will be transformed into v::min(v::remainingPart()).
v::minGreaterThan(0) → v::min(v::greaterThan(0))v::minPositive() → v::min(v::positive())v::minGreaterThan(0)->assert([1, 2, 3])
// Validation passes successfully
v::minGreaterThan(0)->assert([0, 1, 2]);
// → The minimum of `[0, 1, 2]` must be greater than 0
notAny method starting with not will be transformed into v::not(v::remainingPart()).
v::notEmpty() → v::not(v::empty())v::notNull() → v::not(v::null())v::notEmpty()->assert('hello')
// Validation passes successfully
v::notEmpty()->assert('');
// → The value must not be empty
nullOrAny method starting with nullOr will be transformed into v::nullOr(v::remainingPart()).
v::nullOrEmail() → v::nullOr(v::email())v::nullOrPositive() → v::nullOr(v::positive())v::nullOrEmail()->assert(null)
v::nullOrEmail()->assert('[email protected]')
// Validation passes successfully
v::nullOrEmail()->assert('invalid-email');
// → The value must be null or a valid email
undefOrAny method starting with undefOr will be transformed into v::undefOr(v::remainingPart()).
v::undefOrPositive() → v::undefOr(v::positive())v::undefOrEmail() → v::undefOr(v::email())v::undefOrPositive()->assert(undefined)
v::undefOrPositive()->assert(5)
// Validation passes successfully
v::undefOrPositive()->assert(-5);
// → The value must be undefined or a positive number
keyAny method starting with key will be transformed into v::key($key, v::remainingPart()). The first argument is used as the key.
v::keyEmail('email') → v::key('email', v::email())v::keyPositive('age') → v::key('age', v::positive())v::keyEmail('email')->assert(['email' => '[email protected]'])
// Validation passes successfully
v::keyEmail('email')->assert(['email' => 'invalid-email']);
// → The key `email` must be a valid email
propertyAny method starting with property will be transformed into v::property($property, v::remainingPart()). The first argument is used as the property.
v::propertyPositive('age') → v::property('age', v::positive())v::propertyEmail('email') → v::property('email', v::email())v::propertyPositive('age')->assert((object)['age' => 25])
// Validation passes successfully
v::propertyPositive('age')->assert((object)['age' => -5]);
// → The property `age` must be a positive number