Skip to content

Commit e8b90a3

Browse files
committed
Create All rule and all prefix
This rule behaves similarly to ``Each, but the difference is that it presents the messages in a simpler manner. This allows users to have a simpler message when validating all items in an array or iterable value. I’m also introducing the `all` prefix, making it easier for users to apply the `All` rule in combination with other rules.
1 parent 562d98d commit e8b90a3

26 files changed

+877
-1
lines changed

bin/create-mixin

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ require __DIR__ . '/../vendor/autoload.php';
88
use Nette\PhpGenerator\InterfaceType;
99
use Nette\PhpGenerator\PhpNamespace;
1010
use Nette\PhpGenerator\Printer;
11+
use Respect\Validation\Mixins\AllBuilder;
12+
use Respect\Validation\Mixins\AllChain;
1113
use Respect\Validation\Mixins\Chain;
1214
use Respect\Validation\Mixins\KeyBuilder;
1315
use Respect\Validation\Mixins\KeyChain;
@@ -168,6 +170,7 @@ function overwriteFile(string $content, string $basename): void
168170
];
169171

170172
$mixins = [
173+
['All', 'all', [], ['All', ...$structureRelatedRules]],
171174
['Key', 'key', [], $structureRelatedRules],
172175
['Length', 'length', $numberRelatedRules, []],
173176
['Max', 'max', $numberRelatedRules, []],
@@ -207,6 +210,7 @@ function overwriteFile(string $content, string $basename): void
207210

208211
if ($name === '') {
209212
$chainedInterface->addExtend(Rule::class);
213+
$chainedInterface->addExtend(AllChain::class);
210214
$chainedInterface->addExtend(KeyChain::class);
211215
$chainedInterface->addExtend(LengthChain::class);
212216
$chainedInterface->addExtend(MaxChain::class);
@@ -217,6 +221,7 @@ function overwriteFile(string $content, string $basename): void
217221
$chainedInterface->addExtend(UndefOrChain::class);
218222
$chainedInterface->addComment('@mixin \\' . Validator::class);
219223

224+
$staticInterface->addExtend(AllBuilder::class);
220225
$staticInterface->addExtend(KeyBuilder::class);
221226
$staticInterface->addExtend(LengthBuilder::class);
222227
$staticInterface->addExtend(MaxBuilder::class);

docs/09-list-of-rules-by-category.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
## Comparisons
4545

46+
- [All](rules/All.md)
4647
- [Between](rules/Between.md)
4748
- [BetweenExclusive](rules/BetweenExclusive.md)
4849
- [Equals](rules/Equals.md)
@@ -270,6 +271,7 @@
270271

271272
## Transformations
272273

274+
- [All](rules/All.md)
273275
- [Call](rules/Call.md)
274276
- [Each](rules/Each.md)
275277
- [Length](rules/Length.md)
@@ -302,6 +304,7 @@
302304

303305
## Alphabetically
304306

307+
- [All](rules/All.md)
305308
- [AllOf](rules/AllOf.md)
306309
- [Alnum](rules/Alnum.md)
307310
- [Alpha](rules/Alpha.md)

docs/rules/All.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# All
2+
3+
- `All(Rule $rule)`
4+
5+
Validates all items of the input against a given rule.
6+
7+
```php
8+
v::all(v::intType())->isValid([1, 2, 3]); // true
9+
v::all(v::intType())->isValid([1, 2, '3']); // false
10+
```
11+
12+
This rule is similar to [Each](Each.md), but as opposed to the former, it displays a single message when asserting an input.
13+
14+
## Note
15+
16+
This rule uses [Length](Length.md) with [GreaterThan][GreaterThan.md] internally. If an input has no items, the validation will fail.
17+
18+
## Templates
19+
20+
### `All::TEMPLATE_STANDARD`
21+
22+
| Mode | Template |
23+
| ---------- | ------------ |
24+
| `default` | Every item in |
25+
| `inverted` | Every item in |
26+
27+
This template serve as message prefixes.:
28+
29+
```php
30+
v::all(v::floatType())->assert([1.5, 2]);
31+
// Message: Every item in `[1.5, 2]` must be float
32+
33+
v::not(v::all(v::intType()))->assert([1, 2, -3]);
34+
// Message: Every item in `[1, 2, -3]` must not be an integer
35+
```
36+
37+
## Categorization
38+
39+
- Comparisons
40+
- Transformations
41+
42+
## Changelog
43+
44+
| Version | Description |
45+
| ------: | ----------- |
46+
| 3.0.0 | Created |
47+
48+
---
49+
50+
See also:
51+
52+
- [Each](Each.md)
53+
- [Length](Length.md)
54+
- [Max](Max.md)
55+
- [Min](Min.md)

docs/rules/Each.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ v::call('array_keys', v::each(v::stringType()))->isValid($releaseDates); // true
2222

2323
## Note
2424

25-
This rule uses [Length](Length.md) with [GreaterThan][GreaterThan.md] internally. If an input has no items, the validation will fail.
25+
This rule uses [Length](Length.md) with [GreaterThan][GreaterThan.md] internally. If an input has no items, the validation will fail.
2626

2727
## Templates
2828

@@ -57,6 +57,7 @@ This rule uses [Length](Length.md) with [GreaterThan][GreaterThan.md] internally
5757

5858
See also:
5959

60+
- [All](All.md)
6061
- [ArrayVal](ArrayVal.md)
6162
- [Call](Call.md)
6263
- [Falsy](Falsy.md)
@@ -65,5 +66,6 @@ See also:
6566
- [Key](Key.md)
6667
- [KeyExists](KeyExists.md)
6768
- [KeyOptional](KeyOptional.md)
69+
- [Length](Length.md)
6870
- [Min](Min.md)
6971
- [Unique](Unique.md)

docs/rules/Length.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ Used when it's impossible to get the length of the input.
7272

7373
See also:
7474

75+
- [All](All.md)
7576
- [Between](Between.md)
7677
- [BetweenExclusive](BetweenExclusive.md)
78+
- [Each](Each.md)
7779
- [GreaterThan](GreaterThan.md)
7880
- [GreaterThanOrEqual](GreaterThanOrEqual.md)
7981
- [LessThan](LessThan.md)

docs/rules/Max.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ This rule uses [Length](Length.md) with [GreaterThan][GreaterThan.md] internally
5252

5353
See also:
5454

55+
- [All](All.md)
5556
- [Between](Between.md)
5657
- [BetweenExclusive](BetweenExclusive.md)
5758
- [DateTimeDiff](DateTimeDiff.md)

docs/rules/Min.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ This rule uses [Length](Length.md) with [GreaterThan][GreaterThan.md] internally
5252

5353
See also:
5454

55+
- [All](All.md)
5556
- [Between](Between.md)
5657
- [BetweenExclusive](BetweenExclusive.md)
5758
- [DateTimeDiff](DateTimeDiff.md)

0 commit comments

Comments
 (0)