Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 3433372

Browse files
committed
docs: added Collection rule
1 parent f94f645 commit 3433372

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

docs/03-rules.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@
4141

4242
## Iterable Rules
4343

44+
- [Collection](03-rules_collection.md)
4445
- [EachValue](03-rules_each-value.md)
4546
- [EachKey](03-rules_each-key.md)

docs/03-rules_collection.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Collection
2+
3+
Validates each key of an `array`, or object implementing `\Traversable`, with a set of validation constraints.
4+
5+
```php
6+
/** @var array<mixed, Validator> $fields */
7+
Collection(
8+
array $fields,
9+
bool $allowExtraFields = false,
10+
?string $message = null,
11+
?string $extraFieldsMessage = null,
12+
?string $missingFieldsMessage = null
13+
);
14+
```
15+
16+
## Basic Usage
17+
18+
```php
19+
Validator::collection(fields: [
20+
'name' => Validator::notBlank(),
21+
'age' => Validator::type('int')->greaterThanOrEqual(18)
22+
])->validate([
23+
'name' => 'Name',
24+
'age' => 25
25+
]); // true
26+
27+
Validator::collection(fields: [
28+
'name' => Validator::notBlank(),
29+
'age' => Validator::type('int')->greaterThanOrEqual(18)
30+
])->validate([
31+
'name' => '',
32+
'age' => 25
33+
]); // false ("name" is blank)
34+
35+
// by default, unknown keys are not allowed and it will fail
36+
Validator::collection(fields: [
37+
'name' => Validator::notBlank(),
38+
'age' => Validator::type('int')->greaterThanOrEqual(18)
39+
])->validate([
40+
'name' => 'Name',
41+
'age' => 25,
42+
'email' => 'mail@example.com'
43+
]); // false ("email" field is not allowed)
44+
45+
// to allow extra fields, set option to true
46+
Validator::collection(
47+
fields: [
48+
'name' => Validator::notBlank(),
49+
'age' => Validator::type('int')->greaterThanOrEqual(18)
50+
],
51+
allowExtraFields: true
52+
)->validate([
53+
'name' => 'Name',
54+
'age' => 25,
55+
'email' => 'mail@example.com'
56+
]); // true
57+
```
58+
59+
> [!NOTE]
60+
> An `UnexpectedValueException` will be thrown when a value in the `fields` associative array is not an instance of `Validator`.
61+
62+
> [!NOTE]
63+
> An `UnexpectedValueException` will be thrown when the input value is not an `array` or an object implementing `\Traversable`.
64+
65+
## Options
66+
67+
### `fields`
68+
69+
type: `array<mixed, Validator>` `required`
70+
71+
Associative array with a set of validation constraints for each key.
72+
73+
### `allowExtraFields`
74+
75+
type: `bool` default: `false`
76+
77+
By default, it is not allowed to have fields (array keys) that are not defined in the `fields` option.
78+
If set to `true`, it will be allowed (but not validated).
79+
80+
### `message`
81+
82+
type: `?string` default: `{{ message }}`
83+
84+
Message that will be shown when one of the fields is invalid.
85+
86+
The following parameters are available:
87+
88+
| Parameter | Description |
89+
|-----------------|---------------------------------------|
90+
| `{{ name }}` | Name of the invalid value |
91+
| `{{ field }}` | Name of the invalid field (array key) |
92+
| `{{ message }}` | The rule message of the invalid field |
93+
94+
### `extraFieldsMessage`
95+
96+
type: `?string` default: `The {{ field }} field is not allowed.`
97+
98+
Message that will be shown when the input value has a field that is not defined in the `fields` option
99+
and `allowExtraFields` is set to `false`.
100+
101+
The following parameters are available:
102+
103+
| Parameter | Description |
104+
|-----------------|---------------------------------------|
105+
| `{{ name }}` | Name of the invalid value |
106+
| `{{ field }}` | Name of the invalid field (array key) |
107+
108+
### `missingFieldsMessage`
109+
110+
type: `?string` default: `The {{ field }} field is missing.`
111+
112+
Message that will be shown when the input value *does not* have a field that is defined in the `fields` option.
113+
114+
The following parameters are available:
115+
116+
| Parameter | Description |
117+
|-----------------|---------------------------------------|
118+
| `{{ name }}` | Name of the invalid value |
119+
| `{{ field }}` | Name of the invalid field (array key) |
120+
121+
## Changelog
122+
123+
- `1.0.0` Created

0 commit comments

Comments
 (0)