respect-validation/docs/rules/KeyExists.md
Henrique Moody d36572cc25
Split the "Property" rule
Currently, the Property rule has a third parameter that allows the
validation of the wrapped rule to be optional, meaning that the
validation will only happen if the property exists. That parameter makes
the rule harder to understand at times.

I'm splitting the Property rule into Property, PropertyExists, and
PropertyOptional. That way, it becomes apparent when someone wants only
to validate whether a property exists or if they will validate the value
of the property only when it exists.

I deliberately didn't create an abstract class because those rules are
different enough not to have an abstraction. In fact, I can see myself
deleting the AbstractRelated after I refactor the KeyNested rule.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-03-05 00:48:31 +01:00

1.1 KiB

KeyExists

  • KeyExists(int|string $key)

Validates if the given key exists in an array.

v::keyExists('name')->validate(['name' => 'The Respect Panda']); // true
v::keyExists('name')->validate(['email' => 'therespectpanda@gmail.com']); // false

v::keyExists(0)->validate(['a', 'b', 'c']); // true
v::keyExists(4)->validate(['a', 'b', 'c']); // false

v::keyExists('username')->validate(new ArrayObject(['username' => 'therespectpanda'])); // true
v::keyExists(5)->validate(new ArrayObject(['a', 'b', 'c'])); // false

Notes

  • To validate an array against a given rule if the key exists, use KeyOptional instead.
  • To validate an array against a given rule requiring the key to exist, use Key instead.

Categorization

  • Arrays
  • Structures

Changelog

Version Description
3.0.0 Created from Key

See also: