respect-validation/docs/rules/Key.md
Henrique Moody 41245f663f
Replace "KeyValue" with "LazyConsecutive" rule
I want to avoid having the Factory inside the rules. If a rule needs to
create another, it can simply instantiate that. The "KeyValue" rule does
too many things under the hood, and the behavior can be unpredictable.

The "LazyConsecutive" rule makes the validation more explicit and way
more flexible, as there could be other cases in which someone only wants
to validate something if the previous validator passes.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-02-22 20:45:50 +01:00

807 B

Key

  • Key(mixed $key)
  • Key(mixed $key, Validatable $rule)
  • Key(mixed $key, Validatable $rule, bool $mandatory)

Validates an array key.

$dict = [
    'foo' => 'bar'
];

v::key('foo')->validate($dict); // true

You can also validate the key value itself:

v::key('foo', v::equals('bar'))->validate($dict); // true

Third parameter makes the key presence optional:

v::key('lorem', v::stringType(), false)->validate($dict); // true

The name of this validator is automatically set to the key name.

Categorization

  • Arrays
  • Nesting
  • Structures

Changelog

Version Description
0.3.9 Created

See also: