respect-validation/docs/rules/KeySet.md

69 lines
1.2 KiB
Markdown
Raw Permalink Normal View History

2015-07-20 12:16:46 +02:00
# KeySet
- `KeySet(Key ...$rule)`
2015-07-20 12:16:46 +02:00
Validates a keys in a defined structure.
```php
2015-10-18 03:44:47 +02:00
$dict = ['foo' => 42];
2015-07-20 12:16:46 +02:00
v::keySet(
2015-10-07 16:46:57 +02:00
v::key('foo', v::intVal())
)->validate($dict); // true
2015-07-20 12:16:46 +02:00
```
Extra keys are not allowed:
```php
2015-10-18 03:44:47 +02:00
$dict = ['foo' => 42, 'bar' => 'String'];
2015-07-20 12:16:46 +02:00
v::keySet(
2015-10-07 16:46:57 +02:00
v::key('foo', v::intVal())
)->validate($dict); // false
2015-07-20 12:16:46 +02:00
```
Missing required keys are not allowed:
```php
2015-10-18 03:44:47 +02:00
$dict = ['foo' => 42, 'bar' => 'String'];
2015-07-20 12:16:46 +02:00
v::keySet(
2015-10-07 16:46:57 +02:00
v::key('foo', v::intVal()),
2015-10-07 16:52:03 +02:00
v::key('bar', v::stringType()),
2015-10-07 16:30:29 +02:00
v::key('baz', v::boolType())
)->validate($dict); // false
2015-07-20 12:16:46 +02:00
```
Missing non-required keys are allowed:
```php
2015-10-18 03:44:47 +02:00
$dict = ['foo' => 42, 'bar' => 'String'];
2015-07-20 12:16:46 +02:00
v::keySet(
2015-10-07 16:46:57 +02:00
v::key('foo', v::intVal()),
2015-10-07 16:52:03 +02:00
v::key('bar', v::stringType()),
2015-10-07 16:30:29 +02:00
v::key('baz', v::boolType(), false)
)->validate($dict); // true
2015-07-20 12:16:46 +02:00
```
It is not possible to negate `keySet()` rules with `not()`.
2015-07-20 12:16:46 +02:00
The keys' order is not considered in the validation.
## Categorization
- Arrays
- Nesting
- Structures
## Changelog
Version | Description
--------|-------------
2.3.0 | KeySet is NonNegatable, fixed message with extra keys
1.0.0 | Created
***
2015-07-20 12:16:46 +02:00
See also:
2018-12-11 13:31:50 +01:00
- [ArrayVal](ArrayVal.md)
- [Key](Key.md)
2018-12-11 13:31:50 +01:00
- [KeyValue](KeyValue.md)