Replace isValid() calls with assert()

There's more value on showing how `assert()` displays the validation
messages than simply showing if `isValid()` returns `true` or `false`.

However, that increases the chances of having outdated documentation, so
I created a doc linter that updates the Markdown files with the
correct message.
This commit is contained in:
Henrique Moody 2026-01-06 14:12:06 +01:00
commit d2198dfd01
No known key found for this signature in database
GPG key ID: 221E9281655813A6
165 changed files with 1748 additions and 623 deletions

View file

@ -17,6 +17,7 @@ use Respect\Dev\Commands\UpdateDomainToplevelCommand;
use Respect\Dev\Commands\UpdatePostalCodesCommand;
use Respect\Dev\Markdown\CompositeLinter;
use Respect\Dev\Markdown\Differ as MarkdownDiffer;
use Respect\Dev\Markdown\Linters\AssertionMessageLinter;
use Respect\Dev\Markdown\Linters\ValidatorHeaderLinter;
use Respect\Dev\Markdown\Linters\ValidatorIndexLinter;
use Respect\Dev\Markdown\Linters\ValidatorRelatedLinter;
@ -31,6 +32,7 @@ return (static function () {
$application = new Application('Respect/Validation', '3.0');
$application->addCommand(new CreateMixinCommand());
$application->addCommand(new DocsLintCommand($differ, new CompositeLinter(
new AssertionMessageLinter(),
new ValidatorHeaderLinter(),
new ValidatorIndexLinter(),
new ValidatorRelatedLinter(),

View file

@ -1,7 +1,7 @@
# Concrete API
There are many micro-frameworks that rely on magic methods. We don't. In this
document we're gonna explore the Respect\Validation API without fluent interfaces
document we're going to explore the Respect\Validation API without fluent interfaces
or magic methods. We'll use a traditional dependency injection approach.
```php

View file

@ -27,7 +27,7 @@ final class Something extends Simple
}
```
The `'{{subject}} is not something` message would be used then you call the validator
The `'{{subject}} is not something` message would be used when you call the validator
with the `not()`.
All classes in Validation are created by the `Factory` class. If you want

View file

@ -5,8 +5,11 @@
Validates all items of the input against a given validator.
```php
v::all(v::intType())->isValid([1, 2, 3]); // true
v::all(v::intType())->isValid([1, 2, '3']); // false
v::all(v::intType())->assert([1, 2, 3]);
// Validation passes successfully
v::all(v::intType())->assert([1, 2, '3']);
// → Every item in `[1, 2, "3"]` must be an integer
```
This validator is similar to [Each](Each.md), but as opposed to the former, it displays a single message when asserting an input.
@ -30,10 +33,10 @@ The template serves as a prefix to the template of the inner validator.
```php
v::all(v::floatType())->assert([1.5, 2]);
// Message: Every item in `[1.5, 2]` must be float
// Every item in `[1.5, 2]` must be float
v::not(v::all(v::intType()))->assert([1, 2, -3]);
// Message: Every item in `[1, 2, -3]` must not be an integer
// Every item in `[1, 2, -3]` must not be an integer
```
## Categorization

View file

@ -6,7 +6,8 @@
Will validate if all inner validators validates.
```php
v::allOf(v::intVal(), v::positive())->isValid(15); // true
v::allOf(v::intVal(), v::positive())->assert(15);
// Validation passes successfully
```
## Templates

View file

@ -9,18 +9,28 @@ Alphanumeric is a combination of alphabetic (a-z and A-Z) and numeric (0-9)
characters.
```php
v::alnum()->isValid('foo 123'); // false
v::alnum(' ')->isValid('foo 123'); // true
v::alnum()->isValid('100%'); // false
v::alnum('%')->isValid('100%'); // true
v::alnum('%', ',')->isValid('10,5%'); // true
v::alnum()->assert('foo 123');
// → "foo 123" must contain only letters (a-z) and digits (0-9)
v::alnum(' ')->assert('foo 123');
// Validation passes successfully
v::alnum()->assert('100%');
// → "100%" must contain only letters (a-z) and digits (0-9)
v::alnum('%')->assert('100%');
// Validation passes successfully
v::alnum('%', ',')->assert('10,5%');
// Validation passes successfully
```
You can restrict case using the [Lowercase](Lowercase.md) and
[Uppercase](Uppercase.md) validators.
```php
v::alnum()->uppercase()->isValid('example'); // false
v::alnum()->uppercase()->assert('example');
// → "example" must contain only uppercase letters
```
Message template for this validator includes `{{additionalChars}}` as the string

View file

@ -7,18 +7,28 @@ Validates whether the input contains only alphabetic characters. This is similar
to [Alnum](Alnum.md), but it does not allow numbers.
```php
v::alpha()->isValid('some name'); // false
v::alpha(' ')->isValid('some name'); // true
v::alpha()->isValid('Cedric-Fabian'); // false
v::alpha('-')->isValid('Cedric-Fabian'); // true
v::alpha('-', '\'')->isValid('\'s-Gravenhage'); // true
v::alpha()->assert('some name');
// → "some name" must contain only letters (a-z)
v::alpha(' ')->assert('some name');
// Validation passes successfully
v::alpha()->assert('Cedric-Fabian');
// → "Cedric-Fabian" must contain only letters (a-z)
v::alpha('-')->assert('Cedric-Fabian');
// Validation passes successfully
v::alpha('-', '\'')->assert('\'s-Gravenhage');
// Validation passes successfully
```
You can restrict case using the [Lowercase](Lowercase.md) and
[Uppercase](Uppercase.md) validators.
```php
v::alpha()->uppercase()->isValid('example'); // false
v::alpha()->uppercase()->assert('example');
// → "example" must contain only uppercase letters
```
## Templates

View file

@ -5,7 +5,8 @@
Validates any input as invalid.
```php
v::alwaysInvalid()->isValid('whatever'); // false
v::alwaysInvalid()->assert('whatever');
// → "whatever" must be valid
```
## Templates

View file

@ -5,7 +5,8 @@
Validates any input as valid.
```php
v::alwaysValid()->isValid('whatever'); // true
v::alwaysValid()->assert('whatever');
// Validation passes successfully
```
## Templates

View file

@ -6,7 +6,8 @@
This is a group validator that acts as an OR operator.
```php
v::anyOf(v::intVal(), v::floatVal())->isValid(15.5); // true
v::anyOf(v::intVal(), v::floatVal())->assert(15.5);
// Validation passes successfully
```
In the sample above, `IntVal()` doesn't validates, but `FloatVal()` validates,

View file

@ -5,9 +5,14 @@
Validates whether the type of an input is array.
```php
v::arrayType()->isValid([]); // true
v::arrayType()->isValid([1, 2, 3]); // true
v::arrayType()->isValid(new ArrayObject()); // false
v::arrayType()->assert([]);
// Validation passes successfully
v::arrayType()->assert([1, 2, 3]);
// Validation passes successfully
v::arrayType()->assert(new ArrayObject());
// → `ArrayObject { getArrayCopy() => [] }` must be an array
```
## Templates
@ -55,5 +60,4 @@ See also:
- [ResourceType](ResourceType.md)
- [StringType](StringType.md)
- [Subset](Subset.md)
- [Type](Type.md)
- [Unique](Unique.md)

View file

@ -6,9 +6,14 @@ Validates if the input is an array or if the input can be used as an array
(instance of `ArrayAccess` or `SimpleXMLElement`).
```php
v::arrayVal()->isValid([]); // true
v::arrayVal()->isValid(new ArrayObject); // true
v::arrayVal()->isValid(new SimpleXMLElement('<xml></xml>')); // true
v::arrayVal()->assert([]);
// Validation passes successfully
v::arrayVal()->assert(new ArrayObject);
// Validation passes successfully
v::arrayVal()->assert(new SimpleXMLElement('<xml></xml>'));
// Validation passes successfully
```
## Templates
@ -55,5 +60,4 @@ See also:
- [ScalarVal](ScalarVal.md)
- [Sorted](Sorted.md)
- [Subset](Subset.md)
- [Type](Type.md)
- [Unique](Unique.md)

View file

@ -19,7 +19,6 @@ final class Person
#[Validator\Not(new Validator\Undef())]
public string $name,
#[Validator\Date('Y-m-d')]
#[Validator\DateTimeDiff('years', new Validator\LessThanOrEqual(25))]
public string $birthdate,
#[Validator\Email]
public ?string $email = null,
@ -34,38 +33,34 @@ Here is how you can validate the attributes of the object:
```php
v::attributes()->assert(new Person('John Doe', '2020-06-23', 'john.doe@gmail.com'));
// No exception
// Validation passes successfully
v::attributes()->assert(new Person('John Doe', '2020-06-23', 'john.doe@gmail.com', '+12024561111'));
// No exception
// Validation passes successfully
v::attributes()->assert(new Person('', '2020-06-23', 'john.doe@gmail.com', '+12024561111'));
// Message: `.name` must not be empty
// `.name` must be defined
v::attributes()->assert(new Person('John Doe', 'not a date', 'john.doe@gmail.com', '+12024561111'));
// Message: `.birthdate` must be a valid date in the format "2005-12-30"
// `.birthdate` must be a valid date in the format "2005-12-30"
v::attributes()->assert(new Person('John Doe', '2020-06-23', 'not an email', '+12024561111'));
// Message: `.email` must be a valid email address or must be null
// `.email` must be a valid email address or must be null
v::attributes()->assert(new Person('John Doe', '2020-06-23', 'john.doe@gmail.com', 'not a phone number'));
// Message: `.phone` must be a valid telephone number or must be null
// `.phone` must be a valid telephone number or must be null
v::attributes()->assert(new Person('John Doe', '2020-06-23'));
// Full message:
// - `Person { +$name="John Doe" +$birthdate="2020-06-23" +$email=null +$phone=null +$address=null }` must pass at least one of the rules
// - `.email` must be defined
// - `.phone` must be defined
// → - `Person { +$name="John Doe" +$birthdate="2020-06-23" +$email=null +$phone=null }` must pass at least one of the rules
// → - `.email` must be defined
// → - `.phone` must be defined
v::attributes()->assert(new Person('', 'not a date', 'not an email', 'not a phone number'));
// Full message:
// - `Person { +$name="" +$birthdate="not a date" +$email="not an email" +$phone="not a phone number" +$address=null }` must pass the rules
// - `.name` must not be empty
// - `.birthdate` must pass all the rules
// - `.birthdate` must be a valid date in the format "2005-12-30"
// - For comparison with now, `.birthdate` must be a valid datetime
// - `.email` must be a valid email address or must be null
// - `.phone` must be a valid telephone number or must be null
// → - `Person { +$name="" +$birthdate="not a date" +$email="not an email" +$phone="not a phone number" }` must pass the rules
// → - `.name` must be defined
// → - `.birthdate` must be a valid date in the format "2005-12-30"
// → - `.email` must be a valid email address or must be null
// → - `.phone` must be a valid telephone number or must be null
```
## Caveats

View file

@ -6,11 +6,20 @@
Validate numbers in any base, even with non regular bases.
```php
v::base(2)->isValid('011010001'); // true
v::base(3)->isValid('0120122001'); // true
v::base(8)->isValid('01234567520'); // true
v::base(16)->isValid('012a34f5675c20d'); // true
v::base(2)->isValid('0120122001'); // false
v::base(2)->assert('011010001');
// Validation passes successfully
v::base(3)->assert('0120122001');
// Validation passes successfully
v::base(8)->assert('01234567520');
// Validation passes successfully
v::base(16)->assert('012a34f5675c20d');
// Validation passes successfully
v::base(2)->assert('0120122001');
// → "0120122001" must be a number in base 2
```
## Templates

View file

@ -5,8 +5,11 @@
Validate if a string is Base64-encoded.
```php
v::base64()->isValid('cmVzcGVjdCE='); // true
v::base64()->isValid('respect!'); // false
v::base64()->assert('cmVzcGVjdCE=');
// Validation passes successfully
v::base64()->assert('respect!');
// → "respect!" must be a base64 encoded string
```
## Templates

View file

@ -5,9 +5,14 @@
Validates whether the input is between two other values.
```php
v::intVal()->between(10, 20)->isValid(10); // true
v::intVal()->between(10, 20)->isValid(15); // true
v::intVal()->between(10, 20)->isValid(20); // true
v::intVal()->between(10, 20)->assert(10);
// Validation passes successfully
v::intVal()->between(10, 20)->assert(15);
// Validation passes successfully
v::intVal()->between(10, 20)->assert(20);
// Validation passes successfully
```
Validation makes comparison easier, check out our supported

View file

@ -5,12 +5,20 @@
Validates whether the input is between two other values, exclusively.
```php
v::betweenExclusive(10, 20)->isValid(10); // true
v::betweenExclusive('a', 'e')->isValid('c'); // true
v::betweenExclusive(new DateTime('yesterday'), new DateTime('tomorrow'))->isValid(new DateTime('today')); // true
v::betweenExclusive(10, 20)->assert(10);
// → 10 must be greater than 10 and less than 20
v::betweenExclusive(0, 100)->isValid(100); // false
v::betweenExclusive('a', 'z')->isValid('a'); // false
v::betweenExclusive('a', 'e')->assert('c');
// Validation passes successfully
v::betweenExclusive(new DateTime('yesterday'), new DateTime('tomorrow'))->assert(new DateTime('today'));
// Validation passes successfully
v::betweenExclusive(0, 100)->assert(100);
// → 100 must be greater than 0 and less than 100
v::betweenExclusive('a', 'z')->assert('a');
// → "a" must be greater than "a" and less than "z"
```
Validation makes comparison easier, check out our supported [comparable values](../comparable-values.md).

View file

@ -7,22 +7,20 @@ Validates if the given input is a blank value (`null`, zeros, empty strings or e
We recommend you to check [Comparing empty values](../comparing-empty-values.md) for more details.
```php
v::blank()->isValid(null); // true
v::blank()->isValid(''); // true
v::blank()->isValid([]); // true
v::blank()->isValid(' '); // true
v::blank()->isValid(0); // true
v::blank()->isValid('0'); // true
v::blank()->isValid(0); // true
v::blank()->isValid('0.0'); // true
v::blank()->isValid(false); // true
v::blank()->isValid(['']); // true
v::blank()->isValid([' ']); // true
v::blank()->isValid([0]); // true
v::blank()->isValid(['0']); // true
v::blank()->isValid([false]); // true
v::blank()->isValid([[''], [0]]); // true
v::blank()->isValid(new stdClass()); // true
v::blank()->assert(' ');
// Validation passes successfully
v::blank()->assert(0);
// Validation passes successfully
v::blank()->assert(false);
// Validation passes successfully
v::blank()->assert(['', ' ', '0.0', []]);
// Validation passes successfully
v::blank()->assert(new stdClass());
// Validation passes successfully
```
It's similar to [Falsy](Falsy.md), but way stricter.

View file

@ -5,8 +5,11 @@
Validates whether the type of the input is [boolean](http://php.net/types.boolean).
```php
v::boolType()->isValid(true); // true
v::boolType()->isValid(false); // true
v::boolType()->assert(true);
// Validation passes successfully
v::boolType()->assert(false);
// Validation passes successfully
```
## Templates
@ -53,4 +56,3 @@ See also:
- [StringType](StringType.md)
- [StringVal](StringVal.md)
- [TrueVal](TrueVal.md)
- [Type](Type.md)

View file

@ -5,12 +5,23 @@
Validates if the input results in a boolean value:
```php
v::boolVal()->isValid('on'); // true
v::boolVal()->isValid('off'); // true
v::boolVal()->isValid('yes'); // true
v::boolVal()->isValid('no'); // true
v::boolVal()->isValid(1); // true
v::boolVal()->isValid(0); // true
v::boolVal()->assert('on');
// Validation passes successfully
v::boolVal()->assert('off');
// Validation passes successfully
v::boolVal()->assert('yes');
// Validation passes successfully
v::boolVal()->assert('no');
// Validation passes successfully
v::boolVal()->assert(1);
// Validation passes successfully
v::boolVal()->assert(0);
// Validation passes successfully
```
## Templates
@ -53,4 +64,3 @@ See also:
- [ResourceType](ResourceType.md)
- [StringType](StringType.md)
- [TrueVal](TrueVal.md)
- [Type](Type.md)

View file

@ -5,7 +5,8 @@
Validates a Dutch citizen service number ([BSN](https://nl.wikipedia.org/wiki/Burgerservicenummer)).
```php
v::bsn()->isValid('612890053'); // true
v::bsn()->assert('612890053');
// Validation passes successfully
```
## Templates

View file

@ -38,7 +38,8 @@ v::call(
->key('host', v::domain())
->key('path', v::stringType())
->key('query', v::notBlank())
)->isValid($url);
)->assert($url);
// Validation passes successfully
```
## Templates

View file

@ -5,9 +5,14 @@
Validates whether the pseudo-type of the input is [callable](http://php.net/types.callable).
```php
v::callableType()->isValid(function () {}); // true
v::callableType()->isValid('trim'); // true
v::callableType()->isValid([new DateTime(), 'format']); // true
v::callableType()->assert(function () {});
// Validation passes successfully
v::callableType()->assert('trim');
// Validation passes successfully
v::callableType()->assert([new DateTime(), 'format']);
// Validation passes successfully
```
## Templates
@ -53,4 +58,3 @@ See also:
- [ResourceType](ResourceType.md)
- [StringType](StringType.md)
- [StringVal](StringVal.md)
- [Type](Type.md)

View file

@ -7,10 +7,9 @@ Validates the input using the return of a given callable.
```php
v::callback(
function (int $input): bool {
return $input + ($input / 2) == 15;
}
)->isValid(10); // true
fn (int $input): bool => $input + ($input / 2) == 15,
)->assert(10);
// Validation passes successfully
```
## Templates

View file

@ -6,9 +6,14 @@
Validates if a string is in a specific charset.
```php
v::charset('ASCII')->isValid('açúcar'); // false
v::charset('ASCII')->isValid('sugar'); //true
v::charset('ISO-8859-1', 'EUC-JP')->isValid('日本国'); // true
v::charset('ASCII')->assert('açúcar');
// → "açúcar" must only contain characters from the `["ASCII"]` charset
v::charset('ASCII')->assert('sugar');
// Validation passes successfully
v::charset('ISO-8859-1', 'EUC-JP')->assert('日本国');
// Validation passes successfully
```
The array format is a logic OR, not AND.

View file

@ -11,10 +11,22 @@ This validator can be helpful in combinations with [Lazy](Lazy.md). An excellent
country code and a subdivision code.
```php
v::circuit(
$validator = v::circuit(
v::key('countryCode', v::countryCode()),
v::lazy(static fn($input) => v::key('subdivisionCode', v::subdivisionCode($input['countryCode']))),
)->isValid($_POST);
);
$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](SubdivisionCode.md), so it makes sense only to validate the

View file

@ -5,7 +5,8 @@
Validates a Brazilian driver's license.
```php
v::cnh()->isValid('02650306461'); // true
v::cnh()->assert('02650306461');
// Validation passes successfully
```
## Templates

View file

@ -6,7 +6,8 @@
Validates if the input contains only consonants.
```php
v::consonant()->isValid('xkcd'); // true
v::consonant()->assert('xkcd');
// Validation passes successfully
```
## Templates

View file

@ -8,13 +8,15 @@ Validates if the input contains some value.
For strings:
```php
v::contains('ipsum')->isValid('lorem ipsum'); // true
v::contains('ipsum')->assert('lorem ipsum');
// Validation passes successfully
```
For arrays:
```php
v::contains('ipsum')->isValid(['ipsum', 'lorem']); // true
v::contains('ipsum')->assert(['ipsum', 'lorem']);
// Validation passes successfully
```
A second parameter may be passed for identical comparison instead

View file

@ -8,13 +8,15 @@ Validates if the input contains at least one of defined values
For strings (comparing is case insensitive):
```php
v::containsAny(['lorem', 'dolor'])->isValid('lorem ipsum'); // true
v::containsAny(['lorem', 'dolor'])->assert('lorem ipsum');
// Validation passes successfully
```
For arrays (comparing is case sensitive to respect "contains" behavior):
```php
v::containsAny(['lorem', 'dolor'])->isValid(['ipsum', 'lorem']); // true
v::containsAny(['lorem', 'dolor'])->assert(['ipsum', 'lorem']);
// Validation passes successfully
```
A second parameter may be passed for identical comparison instead

View file

@ -7,7 +7,8 @@ Validates if all of the characters in the provided string, are control
characters.
```php
v::control()->isValid("\n\r\t"); // true
v::control()->assert("\n\r\t");
// Validation passes successfully
```
## Templates

View file

@ -6,9 +6,14 @@ Validates if the input is countable, in other words, if you're allowed to use
[count()](http://php.net/count) function on it.
```php
v::countable()->isValid([]); // true
v::countable()->isValid(new ArrayObject()); // true
v::countable()->isValid('string'); // false
v::countable()->assert([]);
// Validation passes successfully
v::countable()->assert(new ArrayObject());
// Validation passes successfully
v::countable()->assert('string');
// → "string" must be a countable value
```
## Templates

View file

@ -8,11 +8,17 @@ Validates whether the input is a country code in [ISO 3166-1][] standard.
**This validator requires [sokil/php-isocodes][] and [sokil/php-isocodes-db-only][] to be installed.**
```php
v::countryCode()->isValid('BR'); // true
v::countryCode()->assert('BR');
// Validation passes successfully
v::countryCode('alpha-2')->isValid('NL'); // true
v::countryCode('alpha-3')->isValid('USA'); // true
v::countryCode('numeric')->isValid('504'); // true
v::countryCode('alpha-2')->assert('NL');
// Validation passes successfully
v::countryCode('alpha-3')->assert('USA');
// Validation passes successfully
v::countryCode('numeric')->assert('504');
// Validation passes successfully
```
This validator supports the three sets of country codes:

View file

@ -5,20 +5,23 @@
Validates a Brazillian CPF number.
```php
v::cpf()->isValid('11598647644'); // true
v::cpf()->assert('11598647644');
// Validation passes successfully
```
It ignores any non-digit char:
```php
v::cpf()->isValid('693.319.118-40'); // true
v::cpf()->assert('693.319.118-40');
// Validation passes successfully
```
If you need to validate digits only, add `->digit()` to
the chain:
```php
v::digit()->cpf()->isValid('11598647644'); // true
v::digit()->cpf()->assert('11598647644');
// Validation passes successfully
```
## Templates

View file

@ -6,17 +6,35 @@
Validates a credit card number.
```php
v::creditCard()->isValid('5376 7473 9720 8720'); // true
v::creditCard()->isValid('5376-7473-9720-8720'); // true
v::creditCard()->isValid('5376.7473.9720.8720'); // true
v::creditCard()->assert('5376 7473 9720 8720');
// Validation passes successfully
v::creditCard('American_Express')->isValid('340316193809364'); // true
v::creditCard('Diners_Club')->isValid('30351042633884'); // true
v::creditCard('Discover')->isValid('6011000990139424'); // true
v::creditCard('JCB')->isValid('3566002020360505'); // true
v::creditCard('Mastercard')->isValid('5376747397208720'); // true
v::creditCard('Visa')->isValid('4024007153361885'); // true
v::creaditCard('RuPay')->isValid('6062973831636410') // true
v::creditCard()->assert('5376-7473-9720-8720');
// Validation passes successfully
v::creditCard()->assert('5376.7473.9720.8720');
// Validation passes successfully
v::creditCard('American Express')->assert('340316193809364');
// Validation passes successfully
v::creditCard('Diners Club')->assert('30351042633884');
// Validation passes successfully
v::creditCard('Discover')->assert('6011000990139424');
// Validation passes successfully
v::creditCard('JCB')->assert('3566002020360505');
// Validation passes successfully
v::creditCard('MasterCard')->assert('5376747397208720');
// Validation passes successfully
v::creditCard('Visa')->assert('4024007153361885');
// Validation passes successfully
v::creditCard('RuPay')->assert('6062973831636410');
// Validation passes successfully
```
The current supported brands are:
@ -33,7 +51,8 @@ It ignores any non-numeric characters, use [Digit](Digit.md),
[Spaced](Spaced.md), or [Regex](Regex.md) when appropriate.
```php
v::digit()->creditCard()->isValid('5376747397208720'); // true
v::digit()->creditCard()->assert('5376747397208720');
// Validation passes successfully
```
## Templates

View file

@ -8,7 +8,8 @@ Validates an [ISO 4217][] currency code.
**This validator requires [sokil/php-isocodes][] and [sokil/php-isocodes-db-only][] to be installed.**
```php
v::currencyCode()->isValid('GBP'); // true
v::currencyCode()->assert('GBP');
// Validation passes successfully
```
This validator supports the two [ISO 4217][] sets:

View file

@ -21,12 +21,23 @@ PHP's [date()](http://php.net/date) function, but only those are allowed:
When a `$format` is not given its default value is `Y-m-d`.
```php
v::date()->isValid('2017-12-31'); // true
v::date()->isValid('2020-02-29'); // true
v::date()->isValid('2019-02-29'); // false
v::date('m/d/y')->isValid('12/31/17'); // true
v::date('F jS, Y')->isValid('May 1st, 2017'); // true
v::date('Ydm')->isValid(20173112); // true
v::date()->assert('2017-12-31');
// Validation passes successfully
v::date()->assert('2020-02-29');
// Validation passes successfully
v::date()->assert('2019-02-29');
// → "2019-02-29" must be a valid date in the format "2005-12-30"
v::date('m/d/y')->assert('12/31/17');
// Validation passes successfully
v::date('F jS, Y')->assert('May 1st, 2017');
// Validation passes successfully
v::date('Ydm')->assert(20173112);
// Validation passes successfully
```
## Templates

View file

@ -10,26 +10,32 @@ The `$format` argument should be in accordance to [DateTime::format()][]. See mo
When a `$format` is not given its default value is `Y-m-d H:i:s`.
```php
v::dateTime()->isValid('2009-01-01'); // true
v::dateTime()->assert('2009-01-01');
// Validation passes successfully
```
Also accepts [strtotime()](http://php.net/strtotime) values:
```php
v::dateTime()->isValid('now'); // true
v::dateTime()->assert('now');
// Validation passes successfully
```
And `DateTimeInterface` instances:
```php
v::dateTime()->isValid(new DateTime()); // true
v::dateTime()->isValid(new DateTimeImmutable()); // true
v::dateTime()->assert(new DateTime());
// Validation passes successfully
v::dateTime()->assert(new DateTimeImmutable());
// Validation passes successfully
```
You can pass a format when validating strings:
```php
v::dateTime('Y-m-d')->isValid('01-01-2009'); // false
v::dateTime('Y-m-d')->assert('01-01-2009');
// → "01-01-2009" must be a valid date/time in the format "2005-12-30"
```
Format has no effect when validating DateTime instances.
@ -50,9 +56,11 @@ you desire, and you may want to use [Callback](Callback.md) to create a custom v
$input = '2014-04-12T23:20:50.052Z';
v::callback(fn($input) => is_string($input) && DateTime::createFromFormat(DateTime::RFC3339_EXTENDED, $input))
->isValid($input); // true
->assert($input);
// Validation passes successfully
v::dateTime(DateTime::RFC3339_EXTENDED)->isValid($input); // false
v::dateTime(DateTime::RFC3339_EXTENDED)->assert($input);
// → "2014-04-12T23:20:50.052Z" must be a valid date/time in the format "2005-12-30T01:02:03.000+00:00"
```
## Templates

View file

@ -10,13 +10,20 @@ The `$format` argument should follow PHP's [date()][] function. When the `$forma
[Supported Date and Time Formats][] by PHP (see [strtotime()][]).
```php
v::dateTimeDiff('years', v::equals(7))->isValid('7 years ago'); // true
v::dateTimeDiff('years', v::equals(7))->isValid('7 years ago + 1 minute'); // false
v::dateTimeDiff('years', v::equals(7))->assert('7 years ago');
// → The number of years between now and "7 years ago" must be equal to 7
v::dateTimeDiff('years', v::greaterThan(18), 'd/m/Y')->isValid('09/12/1990'); // true
v::dateTimeDiff('years', v::greaterThan(18), 'd/m/Y')->isValid('09/12/2023'); // false
v::dateTimeDiff('years', v::equals(7))->assert('7 years ago + 1 minute');
// → The number of years between now and "7 years ago + 1 minute" must be equal to 7
v::dateTimeDiff('months', v::between(1, 18))->isValid('5 months ago'); // true
v::dateTimeDiff('years', v::greaterThan(18), 'd/m/Y')->assert('09/12/1990');
// Validation passes successfully
v::dateTimeDiff('years', v::greaterThan(18), 'd/m/Y')->assert('09/12/2023');
// → The number of years between "01/01/2024" and "09/12/2023" must be greater than 18
v::dateTimeDiff('months', v::between(1, 18))->assert('5 months ago');
// Validation passes successfully
```
The supported types are:
@ -70,11 +77,11 @@ Used when the input cannot be parsed with the given format.
The template serves as a prefix to the template of the inner validator.
```php
v::dateTimeDiff('years', v::equals(2))->assert('1 year ago')
// The number of years between now and 1 year ago must be equal to 2
v::dateTimeDiff('years', v::equals(2))->assert('1 year ago');
// The number of years between now and "1 year ago" must be equal to 2
v::not(v::dateTimeDiff('years', v::lessThan(8)))->assert('7 year ago')
// The number of years between now and 7 year ago must not be less than 8
v::not(v::dateTimeDiff('years', v::lessThan(8)))->assert('7 year ago');
// The number of years between now and "7 year ago" must not be less than 8
```
## Template placeholders
@ -94,7 +101,7 @@ When using custom templates, the key must be `dateTimeDiff` + name of the valida
v::dateTimeDiff('years', v::equals(2))->assert('1 year ago', [
'dateTimeDiffEquals' => 'Please enter a date that is 2 years ago'
]);
// Please enter a date that is 2 years ago.
// Please enter a date that is 2 years ago
```
## Categorization

View file

@ -5,10 +5,14 @@
Validates whether the input matches the expected number of decimals.
```php
v::decimals(2)->isValid('27990.50'); // true
v::decimals(1)->isValid('27990.50'); // false
v::decimal(1)->isValid(1.5); // true
v::decimal(2)->assert('27990.50');
// Validation passes successfully
v::decimal(1)->assert('27990.50');
// → "27990.50" must have 1 decimals
v::decimal(1)->assert(1.5);
// Validation passes successfully
```
## Known limitations
@ -17,7 +21,8 @@ When validating float types, it is not possible to determine the amount of
ending zeros and because of that, validations like the ones below will pass.
```php
v::decimal(1)->isValid(1.50); // true
v::decimal(1)->assert(1.50);
// Validation passes successfully
```
## Templates

View file

@ -6,10 +6,17 @@
Validates whether the input contains only digits.
```php
v::digit()->isValid('020 612 1851'); // false
v::digit(' ')->isValid('020 612 1851'); // true
v::digit()->isValid('172.655.537-21'); // false
v::digit('.', '-')->isValid('172.655.537-21'); // true
v::digit()->assert('020 612 1851');
// → "020 612 1851" must contain only digits (0-9)
v::digit(' ')->assert('020 612 1851');
// Validation passes successfully
v::digit()->assert('172.655.537-21');
// → "172.655.537-21" must contain only digits (0-9)
v::digit('.', '-')->assert('172.655.537-21');
// Validation passes successfully
```
## Templates

View file

@ -5,15 +5,21 @@
Validates if the given path is a directory.
```php
v::directory()->isValid(__DIR__); // true
v::directory()->isValid(__FILE__); // false
v::directory()->assert(__DIR__);
// Validation passes successfully
v::directory()->assert(__FILE__);
// → "/path/to/file" must be a directory
```
This validator will consider SplFileInfo instances, so you can do something like:
```php
v::directory()->isValid(new SplFileInfo('library/'));
v::directory()->isValid(dir('/'));
v::directory()->assert(new SplFileInfo('library/'));
// Validation passes successfully
v::directory()->assert(dir('/'));
// Validation passes successfully
```
## Templates

View file

@ -6,14 +6,16 @@
Validates whether the input is a valid domain name or not.
```php
v::domain()->isValid('google.com');
v::domain()->assert('google.com');
// Validation passes successfully
```
You can skip _top level domain_ (TLD) checks to validate internal
domain names:
```php
v::domain(false)->isValid('dev.machine.local');
v::domain(false)->assert('dev.machine.local');
// Validation passes successfully
```
This is a composite validator, it validates several validators

View file

@ -11,13 +11,15 @@ $releaseDates = [
'relational' => '2011-02-05',
];
v::each(v::dateTime())->isValid($releaseDates); // true
v::each(v::dateTime())->assert($releaseDates);
// Validation passes successfully
```
You can also validate array keys combining this validator with [Call](Call.md):
```php
v::call('array_keys', v::each(v::stringType()))->isValid($releaseDates); // true
v::call('array_keys', v::each(v::stringType()))->assert($releaseDates);
// Validation passes successfully
```
## Note

View file

@ -5,7 +5,8 @@
Validates an email address.
```php
v::email()->isValid('alganet@gmail.com'); // true
v::email()->assert('alganet@gmail.com');
// Validation passes successfully
```
## Templates

View file

@ -5,16 +5,35 @@
Validates if the input is an emoji or a sequence of emojis.
```php
v::emoji()->isValid('🍕'); // true
v::emoji()->isValid('🎈'); // true
v::emoji()->isValid('⚡'); // true
v::emoji()->isValid('🌊🌊🌊🌊🌊🏄🌊🌊🌊🏖🌴'); // true
v::emoji()->isValid('🇧🇷'); // true (country flag)
v::emoji()->isValid('👨‍👩‍👧‍👦'); // true (ZWJ sequence)
v::emoji()->isValid('👩🏽'); // true (skin tone modifier)
v::emoji()->isValid('1⃣'); // true (keycap sequence)
v::emoji()->isValid('Hello World'); // false
v::emoji()->isValid('this is a spark ⚡'); // false (mixed content)
v::emoji()->assert('🍕');
// Validation passes successfully
v::emoji()->assert('🎈');
// Validation passes successfully
v::emoji()->assert('⚡');
// Validation passes successfully
v::emoji()->assert('🌊🌊🌊🌊🌊🏄🌊🌊🌊🏖🌴');
// Validation passes successfully
v::emoji()->assert('🇧🇷'); // (Country flags)
// Validation passes successfully
v::emoji()->assert('👨‍👩‍👧‍👦'); // (ZWJ sequence)
// Validation passes successfully
v::emoji()->assert('👩🏽'); // (Skin tone modifier)
// Validation passes successfully
v::emoji()->assert('1⃣'); // (Keycap sequence)
// Validation passes successfully
v::emoji()->assert('Hello World');
// → "Hello World" must be an emoji
v::emoji()->assert('this is a spark ⚡'); // (Mixed content)
// → "this is a spark ⚡" must be an emoji
```
This validator supports:

View file

@ -9,13 +9,15 @@ only if the value is at the end of the input.
For strings:
```php
v::endsWith('ipsum')->isValid('lorem ipsum'); // true
v::endsWith('ipsum')->assert('lorem ipsum');
// Validation passes successfully
```
For arrays:
```php
v::endsWith('ipsum')->isValid(['lorem', 'ipsum']); // true
v::endsWith('ipsum')->assert(['lorem', 'ipsum']);
// Validation passes successfully
```
A second parameter may be passed for identical comparison instead

View file

@ -5,7 +5,8 @@
Validates if the input is equal to some value.
```php
v::equals('alganet')->isValid('alganet'); // true
v::equals('alganet')->assert('alganet');
// Validation passes successfully
```
Message template for this validator includes `{{compareTo}}`.

View file

@ -5,9 +5,14 @@
Validates if the input is equivalent to some value.
```php
v::equivalent(1)->isValid(true); // true
v::equivalent('Something')->isValid('someThing'); // true
v::equivalent(new ArrayObject([1, 2, 3, 4, 5]))->isValid(new ArrayObject([1, 2, 3, 4, 5])); // true
v::equivalent(1)->assert(true);
// Validation passes successfully
v::equivalent('Something')->assert('someThing');
// Validation passes successfully
v::equivalent(new ArrayObject([1, 2, 3, 4, 5]))->assert(new ArrayObject([1, 2, 3, 4, 5]));
// Validation passes successfully
```
This validator is very similar to [Equals](Equals.md) but it does not make case-sensitive

View file

@ -5,7 +5,8 @@
Validates whether the input is an even number or not.
```php
v::intVal()->even()->isValid(2); // true
v::intVal()->even()->assert(2);
// Validation passes successfully
```
Using `int()` before `even()` is a best practice.

View file

@ -5,7 +5,11 @@
Validates if a file is an executable.
```php
v::executable()->isValid('script.sh'); // true
v::executable()->assert('/path/to/file');
// → "/path/to/file" must be an executable file
v::executable()->assert('/path/to/executable');
// Validation passes successfully
```
## Templates

View file

@ -5,14 +5,18 @@
Validates files or directories.
```php
v::exists()->isValid(__FILE__); // true
v::exists()->isValid(__DIR__); // true
v::exists()->assert(__FILE__);
// Validation passes successfully
v::exists()->assert(__DIR__);
// Validation passes successfully
```
This validator will consider SplFileInfo instances, so you can do something like:
```php
v::exists()->isValid(new SplFileInfo('file.txt'));
v::exists()->assert(new SplFileInfo('/path/to/file.txt'));
// Validation passes successfully
```
## Templates

View file

@ -5,7 +5,8 @@
Validates if the file extension matches the expected one:
```php
v::extension('png')->isValid('image.png'); // true
v::extension('png')->assert('image.png');
// Validation passes successfully
```
This validator is case-sensitive.

View file

@ -5,9 +5,14 @@
Validates if the input is a factor of the defined dividend.
```php
v::factor(0)->isValid(5); // true
v::factor(4)->isValid(2); // true
v::factor(4)->isValid(3); // false
v::factor(0)->assert(5);
// Validation passes successfully
v::factor(4)->assert(2);
// Validation passes successfully
v::factor(4)->assert(3);
// → 3 must be a factor of 4
```
## Templates

View file

@ -5,14 +5,29 @@
Validates if a value is considered as `false`.
```php
v::falseVal()->isValid(false); // true
v::falseVal()->isValid(0); // true
v::falseVal()->isValid('0'); // true
v::falseVal()->isValid('false'); // true
v::falseVal()->isValid('off'); // true
v::falseVal()->isValid('no'); // true
v::falseVal()->isValid('0.5'); // false
v::falseVal()->isValid('2'); // false
v::falseVal()->assert(false);
// Validation passes successfully
v::falseVal()->assert(0);
// Validation passes successfully
v::falseVal()->assert('0');
// Validation passes successfully
v::falseVal()->assert('false');
// Validation passes successfully
v::falseVal()->assert('off');
// Validation passes successfully
v::falseVal()->assert('no');
// Validation passes successfully
v::falseVal()->assert('0.5');
// → "0.5" must evaluate to `false`
v::falseVal()->assert('2');
// → "2" must evaluate to `false`
```
## Templates

View file

@ -7,25 +7,29 @@ Validates whether the given input is considered empty or falsy, similar to PHP's
We recommend you to check [Comparing empty values](../comparing-empty-values.md) for more details.
```php
v::falsy()->isValid(''); // true
v::falsy()->assert('');
// Validation passes successfully
```
Null values are empty:
```php
v::falsy()->isValid(null); // true
v::falsy()->assert(null);
// Validation passes successfully
```
Numbers:
```php
v::falsy()->isValid(0); // true
v::falsy()->assert(0);
// Validation passes successfully
```
Empty arrays:
```php
v::falsy()->isValid([]); // true
v::falsy()->assert([]);
// Validation passes successfully
```
## Templates

View file

@ -5,9 +5,14 @@
Validates whether the input follows the Fibonacci integer sequence.
```php
v::fibonacci()->isValid(1); // true
v::fibonacci()->isValid('34'); // true
v::fibonacci()->isValid(6); // false
v::fibonacci()->assert(1);
// Validation passes successfully
v::fibonacci()->assert('34');
// Validation passes successfully
v::fibonacci()->assert(6);
// → 6 must be a valid Fibonacci number
```
## Templates

View file

@ -5,14 +5,18 @@
Validates whether file input is as a regular filename.
```php
v::file()->isValid(__FILE__); // true
v::file()->isValid(__DIR__); // false
v::file()->assert(__FILE__);
// Validation passes successfully
v::file()->assert(__DIR__);
// → "/path/to/dir" must be a valid file
```
This validator will consider SplFileInfo instances, so you can do something like:
```php
v::file()->isValid(new SplFileInfo('file.txt'));
v::file()->assert(new SplFileInfo('/path/to/file.txt'));
// Validation passes successfully
```
## Templates

View file

@ -6,12 +6,23 @@
Validates the input with the PHP's [filter_var()](http://php.net/filter_var) function.
```php
v::filterVar(FILTER_VALIDATE_EMAIL)->isValid('bob@example.com'); // true
v::filterVar(FILTER_VALIDATE_URL)->isValid('http://example.com'); // true
v::filterVar(FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)->isValid('http://example.com'); // false
v::filterVar(FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)->isValid('http://example.com/path'); // true
v::filterVar(FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)->isValid('webserver.local'); // true
v::filterVar(FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)->isValid('@local'); // false
v::filterVar(FILTER_VALIDATE_EMAIL)->assert('bob@example.com');
// Validation passes successfully
v::filterVar(FILTER_VALIDATE_URL)->assert('http://example.com');
// Validation passes successfully
v::filterVar(FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)->assert('http://example.com');
// → "http://example.com" must be valid
v::filterVar(FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)->assert('http://example.com/path');
// Validation passes successfully
v::filterVar(FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)->assert('webserver.local');
// Validation passes successfully
v::filterVar(FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)->assert('@local');
// → "@local" must be valid
```
## Templates

View file

@ -5,8 +5,11 @@
Validates if the input is a finite number.
```php
v::finite()->isValid('10'); // true
v::finite()->isValid(10); // true
v::finite()->assert('10');
// Validation passes successfully
v::finite()->assert(10);
// Validation passes successfully
```
## Templates
@ -46,4 +49,3 @@ See also:
- [IntType](IntType.md)
- [IntVal](IntVal.md)
- [NumericVal](NumericVal.md)
- [Type](Type.md)

View file

@ -5,9 +5,14 @@
Validates whether the type of the input is [float](http://php.net/types.float).
```php
v::floatType()->isValid(1.5); // true
v::floatType()->isValid('1.5'); // false
v::floatType()->isValid(0e5); // true
v::floatType()->assert(1.5);
// Validation passes successfully
v::floatType()->assert('1.5');
// → "1.5" must be float
v::floatType()->assert(0e5);
// Validation passes successfully
```
## Templates
@ -54,4 +59,3 @@ See also:
- [ResourceType](ResourceType.md)
- [StringType](StringType.md)
- [StringVal](StringVal.md)
- [Type](Type.md)

View file

@ -5,8 +5,11 @@
Validate whether the input value is float.
```php
v::floatVal()->isValid(1.5); // true
v::floatVal()->isValid('1e5'); // true
v::floatVal()->assert(1.5);
// Validation passes successfully
v::floatVal()->assert('1e5');
// Validation passes successfully
```
## Templates
@ -44,4 +47,3 @@ See also:
- [FloatType](FloatType.md)
- [IntType](IntType.md)
- [IntVal](IntVal.md)
- [Type](Type.md)

View file

@ -7,7 +7,8 @@ Validates if all characters in the input are printable and actually creates
visible output (no white space).
```php
v::graph()->isValid('LKM@#$%4;'); // true
v::graph()->assert('LKM@#$%4;');
// Validation passes successfully
```
## Templates

View file

@ -5,8 +5,11 @@
Validates whether the input is greater than a value.
```php
v::greaterThan(10)->isValid(11); // true
v::greaterThan(10)->isValid(9); // false
v::greaterThan(10)->assert(11);
// Validation passes successfully
v::greaterThan(10)->assert(9);
// → 9 must be greater than 10
```
Validation makes comparison easier, check out our supported

View file

@ -5,9 +5,14 @@
Validates whether the input is greater than or equal to a value.
```php
v::intVal()->greaterThanOrEqual(10)->isValid(9); // false
v::intVal()->greaterThanOrEqual(10)->isValid(10); // true
v::intVal()->greaterThanOrEqual(10)->isValid(11); // true
v::intVal()->greaterThanOrEqual(10)->assert(9);
// → 9 must be greater than or equal to 10
v::intVal()->greaterThanOrEqual(10)->assert(10);
// Validation passes successfully
v::intVal()->greaterThanOrEqual(10)->assert(11);
// Validation passes successfully
```
Validation makes comparison easier, check out our supported

View file

@ -5,11 +5,17 @@
Validates a Finnish personal identity code ([HETU][]).
```php
v::hetu()->isValid('010106A9012'); // true
v::hetu()->isValid('290199-907A'); // true
v::hetu()->isValid('280291+923X'); // true
v::hetu()->assert('010106A9012');
// Validation passes successfully
v::hetu()->isValid('010106_9012'); // false
v::hetu()->assert('290199-907A');
// Validation passes successfully
v::hetu()->assert('280291+923X');
// Validation passes successfully
v::hetu()->assert('010106_9012');
// → "010106_9012" must be a valid Finnish personal identity code
```
The validation is case-sensitive.

View file

@ -5,10 +5,17 @@
Validates weather the input is a hex RGB color or not.
```php
v::hexRgbColor()->isValid('#FFFAAA'); // true
v::hexRgbColor()->isValid('#ff6600'); // true
v::hexRgbColor()->isValid('123123'); // true
v::hexRgbColor()->isValid('FCD'); // true
v::hexRgbColor()->assert('#FFFAAA');
// Validation passes successfully
v::hexRgbColor()->assert('#ff6600');
// Validation passes successfully
v::hexRgbColor()->assert('123123');
// Validation passes successfully
v::hexRgbColor()->assert('FCD');
// Validation passes successfully
```
## Templates

View file

@ -6,12 +6,20 @@ Validates whether the input is a valid [IBAN][] (International Bank Account
Number) or not.
```php
v::iban()->isValid('SE35 5000 0000 0549 1000 0003'); // true
v::iban()->isValid('ch9300762011623852957'); // true
v::iban()->assert('SE35 5000 0000 0549 1000 0003');
// Validation passes successfully
v::iban()->isValid('ZZ32 5000 5880 7742'); // false
v::iban()->isValid(123456789); // false
v::iban()->isValid(''); // false
v::iban()->assert('ch9300762011623852957');
// → "ch9300762011623852957" must be a valid IBAN
v::iban()->assert('ZZ32 5000 5880 7742');
// → "ZZ32 5000 5880 7742" must be a valid IBAN
v::iban()->assert(123456789);
// → 123456789 must be a valid IBAN
v::iban()->assert('');
// → "" must be a valid IBAN
```
## Templates

View file

@ -5,8 +5,11 @@
Validates if the input is identical to some value.
```php
v::identical(42)->isValid(42); // true
v::identical(42)->isValid('42'); // false
v::identical(42)->assert(42);
// Validation passes successfully
v::identical(42)->assert('42');
// → "42" must be identical to 42
```
Message template for this validator includes `{{compareTo}}`.

View file

@ -5,9 +5,14 @@
Validates if the file is a valid image by checking its MIME type.
```php
v::image()->isValid('image.gif'); // true
v::image()->isValid('image.jpg'); // true
v::image()->isValid('image.png'); // true
v::image()->assert('/path/to/image.gif');
// Validation passes successfully
v::image()->assert('/path/to/image.jpg');
// Validation passes successfully
v::image()->assert('/path/to/image.png');
// Validation passes successfully
```
All the validations above must return `false` if the input is not a valid file

View file

@ -5,8 +5,11 @@
Validates is the input is a valid [IMEI][].
```php
v::imei()->isValid('35-209900-176148-1'); // true
v::imei()->isValid('490154203237518'); // true
v::imei()->assert('35-209900-176148-1');
// Validation passes successfully
v::imei()->assert('490154203237518');
// Validation passes successfully
```
## Templates

View file

@ -8,13 +8,15 @@ Validates if the input is contained in a specific haystack.
For strings:
```php
v::in('lorem ipsum')->isValid('ipsum'); // true
v::in('lorem ipsum')->assert('ipsum');
// Validation passes successfully
```
For arrays:
```php
v::in(['lorem', 'ipsum'])->isValid('lorem'); // true
v::in(['lorem', 'ipsum'])->assert('lorem');
// Validation passes successfully
```
A second parameter may be passed for identical comparison instead

View file

@ -5,7 +5,8 @@
Validates if the input is an infinite number.
```php
v::infinite()->isValid(INF); // true
v::infinite()->assert(INF);
// Validation passes successfully
```
## Templates
@ -45,4 +46,3 @@ See also:
- [IntType](IntType.md)
- [IntVal](IntVal.md)
- [NumericVal](NumericVal.md)
- [Type](Type.md)

View file

@ -5,8 +5,11 @@
Validates if the input is an instance of the given class or interface.
```php
v::instance('DateTime')->isValid(new DateTime); // true
v::instance('Traversable')->isValid(new ArrayObject); // true
v::instance('DateTime')->assert(new DateTime);
// Validation passes successfully
v::instance('Traversable')->assert(new ArrayObject);
// Validation passes successfully
```
Message template for this validator includes `{{instanceName}}`.
@ -45,4 +48,3 @@ See also:
- [IterableType](IterableType.md)
- [IterableVal](IterableVal.md)
- [ObjectType](ObjectType.md)
- [Type](Type.md)

View file

@ -5,8 +5,11 @@
Validates whether the type of the input is [integer](http://php.net/types.integer).
```php
v::intType()->isValid(42); // true
v::intType()->isValid('10'); // false
v::intType()->assert(42);
// Validation passes successfully
v::intType()->assert('10');
// → "10" must be an integer
```
## Templates
@ -57,4 +60,3 @@ See also:
- [ResourceType](ResourceType.md)
- [StringType](StringType.md)
- [StringVal](StringVal.md)
- [Type](Type.md)

View file

@ -5,11 +5,20 @@
Validates if the input is an integer, allowing leading zeros and other number bases.
```php
v::intVal()->isValid('10'); // true
v::intVal()->isValid('089'); // true
v::intVal()->isValid(10); // true
v::intVal()->isValid(0b101010); // true
v::intVal()->isValid(0x2a); // true
v::intVal()->assert('10');
// Validation passes successfully
v::intVal()->assert('089');
// Validation passes successfully
v::intVal()->assert(10);
// Validation passes successfully
v::intVal()->assert(0b101010);
// Validation passes successfully
v::intVal()->assert(0x2a);
// Validation passes successfully
```
This validator will consider as valid any input that PHP can convert to an integer,
@ -17,8 +26,11 @@ but that does not contain non-integer values. That way, one can safely use the
value this validator validates, without having surprises.
```php
v::intVal()->isValid(true); // false
v::intVal()->isValid('89a'); // false
v::intVal()->assert(true);
// → `true` must be an integer value
v::intVal()->assert('89a');
// → "89a" must be an integer value
```
Even though PHP can cast the values above as integers, this validator will not
@ -65,4 +77,3 @@ See also:
- [Infinite](Infinite.md)
- [IntType](IntType.md)
- [NumericVal](NumericVal.md)
- [Type](Type.md)

View file

@ -9,28 +9,38 @@ Validates whether the input is a valid IP address.
This validator uses the native [filter_var()][] PHP function.
```php
v::ip()->isValid('127.0.0.1'); // true
v::ip('220.78.168.0/21')->isValid('220.78.173.2'); // true
v::ip('220.78.168.0/21')->isValid('220.78.176.2'); // false
v::ip()->assert('127.0.0.1');
// Validation passes successfully
v::ip('220.78.168.0/21')->assert('220.78.173.2');
// Validation passes successfully
v::ip('220.78.168.0/21')->assert('220.78.176.2');
// → "220.78.176.2" must be an IP address in the 220.78.168.0/255.255.255.255 range
```
Validating ranges:
```php
v::ip('127.0.0.1-127.0.0.5')->isValid('127.0.0.2'); // true
v::ip('127.0.0.1-127.0.0.5')->isValid('127.0.0.10'); // false
v::ip('127.0.0.1-127.0.0.5')->assert('127.0.0.2');
// Validation passes successfully
v::ip('127.0.0.1-127.0.0.5')->assert('127.0.0.10');
// → "127.0.0.10" must be an IP address in the 127.0.0.1-127.0.0.5 range
```
You can pass a parameter with [filter_var()][] flags for IP.
```php
v::ip('*', FILTER_FLAG_NO_PRIV_RANGE)->isValid('192.168.0.1'); // false
v::ip('*', FILTER_FLAG_NO_PRIV_RANGE)->assert('192.168.0.1');
// → "192.168.0.1" must be an IP address
```
If you want to validate IPv6 you can do as follow:
```php
v::ip('*', FILTER_FLAG_IPV6)->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'); // true
v::ip('*', FILTER_FLAG_IPV6)->assert('2001:0db8:85a3:08d3:1319:8a2e:0370:7334');
// Validation passes successfully
```
## Templates

View file

@ -5,10 +5,17 @@
Validates whether the input is a valid [ISBN][] or not.
```php
v::isbn()->isValid('ISBN-13: 978-0-596-52068-7'); // true
v::isbn()->isValid('978 0 596 52068 7'); // true
v::isbn()->isValid('ISBN-12: 978-0-596-52068-7'); // false
v::isbn()->isValid('978 10 596 52068 7'); // false
v::isbn()->assert('ISBN-13: 978-0-596-52068-7');
// Validation passes successfully
v::isbn()->assert('978 0 596 52068 7');
// Validation passes successfully
v::isbn()->assert('ISBN-12: 978-0-596-52068-7');
// → "ISBN-12: 978-0-596-52068-7" must be a valid ISBN
v::isbn()->assert('978 10 596 52068 7');
// → "978 10 596 52068 7" must be a valid ISBN
```
## Templates

View file

@ -5,11 +5,17 @@
Validates whether the input is iterable, meaning that it matches the built-in compile time type alias `iterable`.
```php
v::iterableType()->isValid([]); // true
v::iterableType()->isValid(new ArrayObject()); // true
v::iterableType()->assert([]);
// Validation passes successfully
v::iterableType()->isValid(new stdClass()); // false
v::iterableType()->isValid('string'); // false
v::iterableType()->assert(new ArrayObject());
// Validation passes successfully
v::iterableType()->assert(new stdClass());
// → `stdClass {}` must be iterable
v::iterableType()->assert('string');
// → "string" must be iterable
```
## Templates

View file

@ -5,10 +5,17 @@
Validates whether the input is an iterable value, in other words, if you can iterate over it with the [foreach][] language construct.
```php
v::iterableVal()->isValid([]); // true
v::iterableVal()->isValid(new ArrayObject()); // true
v::iterableVal()->isValid(new stdClass()); // true
v::iterableVal()->isValid('string'); // false
v::iterableVal()->assert([]);
// Validation passes successfully
v::iterableVal()->assert(new ArrayObject());
// Validation passes successfully
v::iterableVal()->assert(new stdClass());
// Validation passes successfully
v::iterableVal()->assert('string');
// → "string" must be an iterable value
```
## Note

View file

@ -5,7 +5,8 @@
Validates if the given input is a valid JSON.
```php
v::json()->isValid('{"foo":"bar"}'); // true
v::json()->assert('{"foo":"bar"}');
// Validation passes successfully
```
## Templates

View file

@ -5,11 +5,14 @@
Validates the value of an array against a given validator.
```php
v::key('name', v::stringType())->isValid(['name' => 'The Respect Panda']); // true
v::key('name', v::stringType())->assert(['name' => 'The Respect Panda']);
// Validation passes successfully
v::key('email', v::email())->isValid(['email' => 'therespectpanda@gmail.com']); // true
v::key('email', v::email())->assert(['email' => 'therespectpanda@gmail.com']);
// Validation passes successfully
v::key('age', v::intVal())->isValid([]); // false
v::key('age', v::intVal())->assert([]);
// → `.age` must be present
```
You can also use `Key` to validate nested arrays:
@ -18,21 +21,22 @@ You can also use `Key` to validate nested arrays:
v::key(
'payment_details',
v::key('credit_card', v::creditCard())
)->isValid([
)->assert([
'payment_details' => [
'credit_card' => '5376 7473 9720 8720',
],
]); // true
]);
// Validation passes successfully
```
The name of this validator is automatically set to the key name.
```php
v::key('email', v::email())->assert([]);
// message: email must be present
// `.email` must be present
v::key('email', v::email())->assert(['email' => 'not email']);
// message: email must be valid email
// `.email` must be a valid email address
```
## Note

View file

@ -5,14 +5,23 @@
Validates if the given key exists in an array.
```php
v::keyExists('name')->isValid(['name' => 'The Respect Panda']); // true
v::keyExists('name')->isValid(['email' => 'therespectpanda@gmail.com']); // false
v::keyExists('name')->assert(['name' => 'The Respect Panda']);
// Validation passes successfully
v::keyExists(0)->isValid(['a', 'b', 'c']); // true
v::keyExists(4)->isValid(['a', 'b', 'c']); // false
v::keyExists('name')->assert(['email' => 'therespectpanda@gmail.com']);
// → `.name` must be present
v::keyExists('username')->isValid(new ArrayObject(['username' => 'therespectpanda'])); // true
v::keyExists(5)->isValid(new ArrayObject(['a', 'b', 'c'])); // false
v::keyExists(0)->assert(['a', 'b', 'c']);
// Validation passes successfully
v::keyExists(4)->assert(['a', 'b', 'c']);
// → `.4` must be present
v::keyExists('username')->assert(new ArrayObject(['username' => 'therespectpanda']));
// Validation passes successfully
v::keyExists(5)->assert(new ArrayObject(['a', 'b', 'c']));
// → `.5` must be present
```
## Notes
@ -43,17 +52,17 @@ When no custom name is set, the path is displayed as `{{name}}`. When a custom n
```php
v::keyExists('foo')->assert([]);
// Message: `.foo` must be present
// `.foo` must be present
v::named('Custom name', v::keyExists('foo'))->assert([]);
// Message: `.foo` (<- Custom name) must be present
// `.foo` (<- Custom name) must be present
```
If you want to display only a custom name while checking if a key exists, use [Key](Key.md) with [AlwaysValid](AlwaysValid.md):
```php
v::key('foo', v::named('Custom name', v::alwaysValid())->assert([]);
// Message: Custom name must be present
v::key('foo', v::named('Custom name', v::alwaysValid()))->assert([]);
// Custom name must be present
```
## Categorization

View file

@ -5,20 +5,27 @@
Validates the value of an array against a given validator when the key exists.
```php
v::keyOptional('name', v::stringType())->isValid([]); // true
v::keyOptional('name', v::stringType())->isValid(['name' => 'The Respect Panda']); // true
v::keyOptional('name', v::stringType())->assert([]);
// Validation passes successfully
v::keyOptional('email', v::email())->isValid([]); // true
v::keyOptional('email', v::email())->isValid(['email' => 'therespectpanda@gmail.com']); // true
v::keyOptional('name', v::stringType())->assert(['name' => 'The Respect Panda']);
// Validation passes successfully
v::keyOptional('age', v::intVal())->isValid(['age' => 'Twenty-Five']); // false
v::keyOptional('email', v::email())->assert([]);
// Validation passes successfully
v::keyOptional('email', v::email())->assert(['email' => 'therespectpanda@gmail.com']);
// Validation passes successfully
v::keyOptional('age', v::intVal())->assert(['age' => 'Twenty-Five']);
// → `.age` must be an integer value
```
The name of this validator is automatically set to the key name.
```php
v::keyOptional('age', v::intVal())->assert(['age' => 'Twenty-Five']);
// message: age must be an integer number
// `.age` must be an integer value
```
## Note
@ -28,7 +35,7 @@ want to ensure the input is an array, use [ArrayType](ArrayType.md) with it.
```php
v::arrayType()->keyOptional('phone', v::phone())->assert('This is not an array');
// message: "This is not an array" must be of type array
// → "This is not an array" must be an array
```
Below are some other validators that are tightly related to `KeyOptional`:

View file

@ -9,7 +9,8 @@ Validates a keys in a defined structure.
v::keySet(
v::keyExists('foo'),
v::keyExists('bar')
)->isValid(['foo' => 'whatever', 'bar' => 'something']); // true
)->assert(['foo' => 'whatever', 'bar' => 'something']);
// Validation passes successfully
```
It will validate the keys in the array with the validators passed in the constructor.
@ -17,11 +18,13 @@ It will validate the keys in the array with the validators passed in the constru
```php
v::keySet(
v::key('foo', v::intVal())
)->isValid(['foo' => 42]); // true
)->assert(['foo' => 42]);
// Validation passes successfully
v::keySet(
v::key('foo', v::intVal())
)->isValid(['foo' => 'string']); // false
)->assert(['foo' => 'string']);
// → `.foo` must be an integer value
```
Extra keys are not allowed:
@ -29,7 +32,8 @@ Extra keys are not allowed:
```php
v::keySet(
v::key('foo', v::intVal())
)->isValid(['foo' => 42, 'bar' => 'String']); // false
)->assert(['foo' => 42, 'bar' => 'String']);
// → `.bar` must not be present
```
Missing required keys are not allowed:
@ -39,7 +43,8 @@ v::keySet(
v::key('foo', v::intVal()),
v::key('bar', v::stringType()),
v::key('baz', v::boolType())
)->isValid(['foo' => 42, 'bar' => 'String']); // false
)->assert(['foo' => 42, 'bar' => 'String']);
// → `.baz` must be present
```
Missing non-required keys are allowed:
@ -49,7 +54,8 @@ v::keySet(
v::key('foo', v::intVal()),
v::key('bar', v::stringType()),
v::keyOptional('baz', v::boolType())
)->isValid(['foo' => 42, 'bar' => 'String']); // true
)->assert(['foo' => 42, 'bar' => 'String']);
// Validation passes successfully
```
Alternatively, you can pass a chain of key-related validators to `keySet()`:
@ -60,7 +66,8 @@ v::keySet(
->key('foo', v::intVal())
->key('bar', v::stringType())
->keyOptional('baz', v::boolType())
)->isValid(['foo' => 42, 'bar' => 'String']); // true
)->assert(['foo' => 42, 'bar' => 'String']);
// Validation passes successfully
```
It is not possible to negate `keySet()` validators with `not()`.

View file

@ -8,11 +8,20 @@ Validates whether the input is language code based on [ISO 639][].
**This validator requires [sokil/php-isocodes][] and [sokil/php-isocodes-db-only][] to be installed.**
```php
v::languageCode()->isValid('pt'); // true
v::languageCode()->isValid('en'); // true
v::languageCode()->isValid('it'); // true
v::languageCode('alpha-3')->isValid('ita'); // true
v::languageCode('alpha-3')->isValid('eng'); // true
v::languageCode()->assert('pt');
// Validation passes successfully
v::languageCode()->assert('en');
// Validation passes successfully
v::languageCode()->assert('it');
// Validation passes successfully
v::languageCode('alpha-3')->assert('ita');
// Validation passes successfully
v::languageCode('alpha-3')->assert('eng');
// Validation passes successfully
```
This validator supports the two[ISO 639][] sets:

View file

@ -8,7 +8,8 @@ This validator is particularly useful when creating validators that rely on the
`confirmation` field matches the `password` field when processing data from a form.
```php
v::key('confirmation', v::equals($_POST['password'] ?? null))->isValid($_POST);
v::key('confirmation', v::equals($_POST['password'] ?? null))->assert($_POST);
// → `.confirmation` must be present
```
The issue with the code is that its hard to reuse because youre relying upon the input itself (`$_POST`). That means
@ -17,7 +18,8 @@ you can create a chain of validators and use it everywhere.
The `lazy()` validator makes this job much simpler and more elegantly:
```php
v::lazy(static fn($input) => v::key('confirmation', v::equals($input['password'] ?? null)))->isValid($_POST);
v::lazy(static fn($input) => v::key('confirmation', v::equals($input['password'] ?? null)))->assert($_POST);
// → `.confirmation` must be present
```
The code above is similar to the first example, but the biggest difference is that the creation of the validator doesn't rely

View file

@ -5,7 +5,8 @@
Validates if a date is leap.
```php
v::leapDate('Y-m-d')->isValid('1988-02-29'); // true
v::leapDate('Y-m-d')->assert('1988-02-29');
// Validation passes successfully
```
This validator accepts DateTime instances as well. The $format

View file

@ -5,7 +5,8 @@
Validates if a year is leap.
```php
v::leapYear()->isValid('1988'); // true
v::leapYear()->assert('1988');
// Validation passes successfully
```
This validator accepts DateTime instances as well.

View file

@ -5,19 +5,24 @@
Validates the length of the given input against a given validator.
```php
v::length(v::between(1, 5))->isValid('abc'); // true
v::length(v::between(1, 5))->assert('abc');
// Validation passes successfully
v::length(v::greaterThan(5))->isValid('abcdef'); // true
v::length(v::greaterThan(5))->assert('abcdef');
// Validation passes successfully
v::length(v::lessThan(5))->isValid('abc'); // true
v::length(v::lessThan(5))->assert('abc');
// Validation passes successfully
```
This validator can be used to validate the length of strings, arrays, and objects that implement the `Countable` interface.
```php
v::length(v::greaterThanOrEqual(3))->isValid([1, 2, 3]); // true
v::length(v::greaterThanOrEqual(3))->assert([1, 2, 3]);
// Validation passes successfully
v::length(v::equals(0))->isValid(new SplPriorityQueue()); // true
v::length(v::equals(0))->assert(new SplPriorityQueue());
// Validation passes successfully
```
## Templates
@ -44,10 +49,10 @@ The template serves as a prefix to the template of the inner validator.
```php
v::length(v::equals(3))->assert('tulip');
// Message: The length of "tulip" must be equal to 3
// The length of "tulip" must be equal to 3
v::not(v::length(v::equals(4)))->assert('rose');
// Message: The length of "rose" must not be equal to 4
// The length of "rose" must not be equal to 4
```
### `Length::TEMPLATE_WRONG_TYPE`

View file

@ -5,8 +5,11 @@
Validates whether the input is less than a value.
```php
v::lessThan(10)->isValid(9); // true
v::lessThan(10)->isValid(10); // false
v::lessThan(10)->assert(9);
// Validation passes successfully
v::lessThan(10)->assert(10);
// → 10 must be less than 10
```
Validation makes comparison easier, check out our supported

View file

@ -5,9 +5,14 @@
Validates whether the input is less than or equal to a value.
```php
v::lessThanOrEqual(10)->isValid(9); // true
v::lessThanOrEqual(10)->isValid(10); // true
v::lessThanOrEqual(10)->isValid(11); // false
v::lessThanOrEqual(10)->assert(9);
// Validation passes successfully
v::lessThanOrEqual(10)->assert(10);
// Validation passes successfully
v::lessThanOrEqual(10)->assert(11);
// → 11 must be less than or equal to 10
```
Validation makes comparison easier, check out our supported

View file

@ -5,7 +5,8 @@
Validates whether the characters in the input are lowercase.
```php
v::stringType()->lowercase()->isValid('xkcd'); // true
v::stringType()->lowercase()->assert('xkcd');
// Validation passes successfully
```
## Templates

View file

@ -5,8 +5,11 @@
Validate whether a given input is a [Luhn][] number.
```php
v::luhn()->isValid('2222400041240011'); // true
v::luhn()->isValid('respect!'); // false
v::luhn()->assert('2222400041240011');
// Validation passes successfully
v::luhn()->assert('respect!');
// → "respect!" must be a valid Luhn number
```
## Templates

View file

@ -5,8 +5,11 @@
Validates whether the input is a valid MAC address.
```php
v::macAddress()->isValid('00:11:22:33:44:55'); // true
v::macAddress()->isValid('af-AA-22-33-44-55'); // true
v::macAddress()->assert('00:11:22:33:44:55');
// Validation passes successfully
v::macAddress()->assert('af-AA-22-33-44-55');
// Validation passes successfully
```
## Templates

View file

@ -5,14 +5,18 @@
Validates the maximum value of the input against a given validator.
```php
v::max(v::equals(30))->isValid([10, 20, 30]); // true
v::max(v::equals(30))->assert([10, 20, 30]);
// Validation passes successfully
v::max(v::between('e', 'g'))->isValid(['b', 'd', 'f']); // true
v::max(v::between('e', 'g'))->assert(['b', 'd', 'f']);
// Validation passes successfully
v::max(v::greaterThan(new DateTime('today')))
->isValid([new DateTime('yesterday'), new DateTime('tomorrow')]); // true
->assert([new DateTime('yesterday'), new DateTime('tomorrow')]);
// Validation passes successfully
v::max(v::greaterThan(15))->isValid([4, 8, 12]); // false
v::max(v::greaterThan(15))->assert([4, 8, 12]);
// → The maximum of `[4, 8, 12]` must be greater than 15
```
## Note

View file

@ -5,8 +5,11 @@
Validates if the input is a file and if its MIME type matches the expected one.
```php
v::mimetype('image/png')->isValid('image.png'); // true
v::mimetype('image/jpeg')->isValid('image.jpg'); // true
v::mimetype('image/png')->assert('/path/to/image.png');
// Validation passes successfully
v::mimetype('image/jpeg')->assert('/path/to/image.jpg');
// Validation passes successfully
```
This validator is case-sensitive and requires [fileinfo](http://php.net/fileinfo) PHP extension.

View file

@ -5,14 +5,18 @@
Validates the minimum value of the input against a given validator.
```php
v::min(v::equals(10))->isValid([10, 20, 30]); // true
v::min(v::equals(10))->assert([10, 20, 30]);
// Validation passes successfully
v::min(v::between('a', 'c'))->isValid(['b', 'd', 'f']); // true
v::min(v::between('a', 'c'))->assert(['b', 'd', 'f']);
// Validation passes successfully
v::min(v::greaterThan(new DateTime('yesterday')))
->isValid([new DateTime('today'), new DateTime('tomorrow')]); // true
->assert([new DateTime('today'), new DateTime('tomorrow')]);
// Validation passes successfully
v::min(v::lessThan(3))->isValid([4, 8, 12]); // false
v::min(v::lessThan(3))->assert([4, 8, 12]);
// → The minimum of `[4, 8, 12]` must be less than 3
```
## Note

View file

@ -5,7 +5,8 @@
Validates if the input is a multiple of the given parameter
```php
v::intVal()->multiple(3)->isValid(9); // true
v::intVal()->multiple(3)->assert(9);
// Validation passes successfully
```
## Templates

View file

@ -6,14 +6,14 @@ Validates the input with the given validator, and uses the custom name in the er
```php
v::named('Your email', v::email())->assert('not an email');
// Message: Your email must be a valid email address
// Your email must be a valid email address
```
Here's an example of a similar code, but without using the `Named` validator:
```php
v::email()->assert('not an email');
// Message: "not an email" must be a valid email address
// "not an email" must be a valid email address
```
The `Named` validator can be also useful when you're using [Attributes](Attributes.md) and want a custom name for a specific property.

Some files were not shown because too many files have changed in this diff Show more