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

Commit 224bfb5

Browse files
committed
feat(doc): added EachKey documentation
1 parent 8222564 commit 224bfb5

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

docs/03-rules.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@
3030

3131
## Iterable Rules
3232

33-
- [EachValue](03x-rules-eachvalue.md)
33+
- [EachValue](03x-rules-each-value.md)
34+
- [EachKey](03x-rules-each-key.md)

docs/03x-rules-each-key.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## EachKey
2+
3+
Validates every key of an `array` or object implementing `\Traversable` with a given set of rules.
4+
5+
```php
6+
EachKey(
7+
Validator $validator,
8+
string $message = 'Invalid key: {{ message }}'
9+
);
10+
```
11+
12+
## Basic Usage
13+
14+
```php
15+
Validator::eachKey(Validator::notBlank()->type('string'))->validate(['red' => '#f00', 'green' => '#0f0']); // true
16+
Validator::eachKey(Validator::notBlank()->type('string'))->validate(['red' => '#f00', 1 => '#0f0']); // false
17+
```
18+
19+
> **Note**
20+
> An `UnexpectedValueException` will be thrown when the input value is not an `array` or an object implementing `\Traversable`.
21+
22+
## Options
23+
24+
### `validator`
25+
26+
type: `Validator` `required`
27+
28+
Validator that will validate each key of an `array` or object implementing `\Traversable`.
29+
30+
### `message`
31+
32+
type: `string` default: `Invalid key: {{ message }}`
33+
34+
Message that will be shown if at least one input value key is invalid according to the given `validator`.
35+
36+
```php
37+
Validator::eachKey(Validator::notBlank())->assert(['red' => '#f00', 1 => '#0f0'], 'Test');
38+
// Throws: Invalid key: The "Test" key should be of type "string", "1" given.
39+
```
40+
41+
The following parameters are available:
42+
43+
| Parameter | Description |
44+
|-----------------|--------------------------------------------------|
45+
| `{{ value }}` | The current invalid value |
46+
| `{{ name }}` | Name of the invalid value |
47+
| `{{ key }}` | The key of the invalid iterable element |
48+
| `{{ element }}` | The value of the invalid iterable element |
49+
| `{{ message }}` | The rule message of the invalid iterable element |
File renamed without changes.

0 commit comments

Comments
 (0)