EachKey(Validator $validator)
Validates whether each key in the input is valid according to another validator.
$releaseDates = [
'validation' => '2010-01-01',
'template' => '2011-01-01',
'relational' => '2011-02-05',
];
v::eachKey(v::stringType())->assert($releaseDates);
// Validation passes successfullyThis validator is the key-type counterpart to Each. While Each validates
values, EachKey validates keys. This allows composable validation of array key types:
v::eachKey(v::stringType())->assert(['a' => 1, 'b' => 2]);
// Validation passes successfully
v::eachKey(v::stringType())->assert([0 => 'x', 1 => 'y']);
// → - Each key of `["x", "y"]` must be valid
// → - Key `.0` must be a string
// → - Key `.1` must be a stringYou can combine EachKey with Each via AllOf to validate both
keys and values independently:
v::allOf(v::eachKey(v::stringType()), v::each(v::intType()))->assert(['a' => 1, 'b' => 2]);
// Validation passes successfullyThis validator will pass if the input is empty. Use Length with GreaterThan to perform a stricter check:
v::eachKey(v::stringType())->assert([]);
// Validation passes successfully
v::length(v::greaterThan(0))->eachKey(v::stringType())->assert([]);
// → The length of `[]` must be greater than 0| Mode | Template |
|---|---|
default |
Each key of {{subject}} must be valid |
inverted |
Each key of {{subject}} must be invalid |
| Mode | Template |
|---|---|
default |
Key |
inverted |
Key |
| Mode | Template |
|---|---|
default |
Each key of |
inverted |
Each key of |
- TEMPLATE_STANDARD: Uses
{{subject}}— the validated input or the custom validator name (if specified). - TEMPLATE_NESTED: Does not use placeholders. Composes with the inner
validator's template via
asAdjacentOfto produce messages like "Key.0must be a string".
- Arrays
- Nesting
| Version | Description |
|---|---|
| 3.2.0 | Created |