This repository was archived by the owner on Mar 13, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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.
You can’t perform that action at this time.
0 commit comments