Skip to content

Commit d2198df

Browse files
committed
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.
1 parent 98150c7 commit d2198df

165 files changed

Lines changed: 1748 additions & 623 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bin/console

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use Respect\Dev\Commands\UpdateDomainToplevelCommand;
1717
use Respect\Dev\Commands\UpdatePostalCodesCommand;
1818
use Respect\Dev\Markdown\CompositeLinter;
1919
use Respect\Dev\Markdown\Differ as MarkdownDiffer;
20+
use Respect\Dev\Markdown\Linters\AssertionMessageLinter;
2021
use Respect\Dev\Markdown\Linters\ValidatorHeaderLinter;
2122
use Respect\Dev\Markdown\Linters\ValidatorIndexLinter;
2223
use Respect\Dev\Markdown\Linters\ValidatorRelatedLinter;
@@ -31,6 +32,7 @@ return (static function () {
3132
$application = new Application('Respect/Validation', '3.0');
3233
$application->addCommand(new CreateMixinCommand());
3334
$application->addCommand(new DocsLintCommand($differ, new CompositeLinter(
35+
new AssertionMessageLinter(),
3436
new ValidatorHeaderLinter(),
3537
new ValidatorIndexLinter(),
3638
new ValidatorRelatedLinter(),

docs/concrete-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Concrete API
22

33
There are many micro-frameworks that rely on magic methods. We don't. In this
4-
document we're gonna explore the Respect\Validation API without fluent interfaces
4+
document we're going to explore the Respect\Validation API without fluent interfaces
55
or magic methods. We'll use a traditional dependency injection approach.
66

77
```php

docs/custom-validators.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final class Something extends Simple
2727
}
2828
```
2929

30-
The `'{{subject}} is not something` message would be used then you call the validator
30+
The `'{{subject}} is not something` message would be used when you call the validator
3131
with the `not()`.
3232

3333
All classes in Validation are created by the `Factory` class. If you want

docs/validators/All.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
Validates all items of the input against a given validator.
66

77
```php
8-
v::all(v::intType())->isValid([1, 2, 3]); // true
9-
v::all(v::intType())->isValid([1, 2, '3']); // false
8+
v::all(v::intType())->assert([1, 2, 3]);
9+
// Validation passes successfully
10+
11+
v::all(v::intType())->assert([1, 2, '3']);
12+
// → Every item in `[1, 2, "3"]` must be an integer
1013
```
1114

1215
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.
3033

3134
```php
3235
v::all(v::floatType())->assert([1.5, 2]);
33-
// Message: Every item in `[1.5, 2]` must be float
36+
// Every item in `[1.5, 2]` must be float
3437

3538
v::not(v::all(v::intType()))->assert([1, 2, -3]);
36-
// Message: Every item in `[1, 2, -3]` must not be an integer
39+
// Every item in `[1, 2, -3]` must not be an integer
3740
```
3841

3942
## Categorization

docs/validators/AllOf.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
Will validate if all inner validators validates.
77

88
```php
9-
v::allOf(v::intVal(), v::positive())->isValid(15); // true
9+
v::allOf(v::intVal(), v::positive())->assert(15);
10+
// Validation passes successfully
1011
```
1112

1213
## Templates

docs/validators/Alnum.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,28 @@ Alphanumeric is a combination of alphabetic (a-z and A-Z) and numeric (0-9)
99
characters.
1010

1111
```php
12-
v::alnum()->isValid('foo 123'); // false
13-
v::alnum(' ')->isValid('foo 123'); // true
14-
v::alnum()->isValid('100%'); // false
15-
v::alnum('%')->isValid('100%'); // true
16-
v::alnum('%', ',')->isValid('10,5%'); // true
12+
v::alnum()->assert('foo 123');
13+
// → "foo 123" must contain only letters (a-z) and digits (0-9)
14+
15+
v::alnum(' ')->assert('foo 123');
16+
// Validation passes successfully
17+
18+
v::alnum()->assert('100%');
19+
// → "100%" must contain only letters (a-z) and digits (0-9)
20+
21+
v::alnum('%')->assert('100%');
22+
// Validation passes successfully
23+
24+
v::alnum('%', ',')->assert('10,5%');
25+
// Validation passes successfully
1726
```
1827

1928
You can restrict case using the [Lowercase](Lowercase.md) and
2029
[Uppercase](Uppercase.md) validators.
2130

2231
```php
23-
v::alnum()->uppercase()->isValid('example'); // false
32+
v::alnum()->uppercase()->assert('example');
33+
// → "example" must contain only uppercase letters
2434
```
2535

2636
Message template for this validator includes `{{additionalChars}}` as the string

docs/validators/Alpha.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,28 @@ Validates whether the input contains only alphabetic characters. This is similar
77
to [Alnum](Alnum.md), but it does not allow numbers.
88

99
```php
10-
v::alpha()->isValid('some name'); // false
11-
v::alpha(' ')->isValid('some name'); // true
12-
v::alpha()->isValid('Cedric-Fabian'); // false
13-
v::alpha('-')->isValid('Cedric-Fabian'); // true
14-
v::alpha('-', '\'')->isValid('\'s-Gravenhage'); // true
10+
v::alpha()->assert('some name');
11+
// → "some name" must contain only letters (a-z)
12+
13+
v::alpha(' ')->assert('some name');
14+
// Validation passes successfully
15+
16+
v::alpha()->assert('Cedric-Fabian');
17+
// → "Cedric-Fabian" must contain only letters (a-z)
18+
19+
v::alpha('-')->assert('Cedric-Fabian');
20+
// Validation passes successfully
21+
22+
v::alpha('-', '\'')->assert('\'s-Gravenhage');
23+
// Validation passes successfully
1524
```
1625

1726
You can restrict case using the [Lowercase](Lowercase.md) and
1827
[Uppercase](Uppercase.md) validators.
1928

2029
```php
21-
v::alpha()->uppercase()->isValid('example'); // false
30+
v::alpha()->uppercase()->assert('example');
31+
// → "example" must contain only uppercase letters
2232
```
2333

2434
## Templates

docs/validators/AlwaysInvalid.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
Validates any input as invalid.
66

77
```php
8-
v::alwaysInvalid()->isValid('whatever'); // false
8+
v::alwaysInvalid()->assert('whatever');
9+
// → "whatever" must be valid
910
```
1011

1112
## Templates

docs/validators/AlwaysValid.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
Validates any input as valid.
66

77
```php
8-
v::alwaysValid()->isValid('whatever'); // true
8+
v::alwaysValid()->assert('whatever');
9+
// Validation passes successfully
910
```
1011

1112
## Templates

docs/validators/AnyOf.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
This is a group validator that acts as an OR operator.
77

88
```php
9-
v::anyOf(v::intVal(), v::floatVal())->isValid(15.5); // true
9+
v::anyOf(v::intVal(), v::floatVal())->assert(15.5);
10+
// Validation passes successfully
1011
```
1112

1213
In the sample above, `IntVal()` doesn't validates, but `FloatVal()` validates,

0 commit comments

Comments
 (0)