respect-validation/docs/rules/Key.md

52 lines
835 B
Markdown
Raw Normal View History

2015-01-30 09:40:06 +01:00
# Key
- `Key(mixed $key)`
- `Key(mixed $key, Validatable $rule)`
- `Key(mixed $key, Validatable $rule, bool $mandatory)`
2015-01-30 09:40:06 +01:00
Validates an array key.
```php
2015-10-18 03:44:47 +02:00
$dict = [
2015-01-30 09:40:06 +01:00
'foo' => 'bar'
2015-10-18 03:44:47 +02:00
];
2015-01-30 09:40:06 +01:00
v::key('foo')->validate($dict); // true
2015-01-30 09:40:06 +01:00
```
You can also validate the key value itself:
```php
v::key('foo', v::equals('bar'))->validate($dict); // true
2015-01-30 09:40:06 +01:00
```
Third parameter makes the key presence optional:
```php
2015-10-07 16:52:03 +02:00
v::key('lorem', v::stringType(), false)->validate($dict); // true
2015-01-30 09:40:06 +01:00
```
The name of this validator is automatically set to the key name.
## Categorization
- Arrays
- Nesting
- Structures
## Changelog
Version | Description
--------|-------------
0.3.9 | Created
***
2015-01-30 09:40:06 +01:00
See also:
2018-12-11 13:31:50 +01:00
- [ArrayVal](ArrayVal.md)
- [Attribute](Attribute.md)
2018-12-11 13:31:50 +01:00
- [Each](Each.md)
- [KeyNested](KeyNested.md)
- [KeySet](KeySet.md)
- [KeyValue](KeyValue.md)