respect-validation/docs/Key.md

35 lines
605 B
Markdown
Raw Normal View History

2015-01-30 09:40:06 +01:00
# Key
- `new Key(string $name)`
- `new Key(string $name, Validatable $rule)`
- `new Key(string $name, 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.
***
2015-01-30 09:40:06 +01:00
See also:
* [Attribute](Attribute.md)