respect-validation/docs/rules/KeySet.md
Henrique Moody b5ad7aa47a
Make Validator immutable
Mutable objects can be challenging to work with in larger codebases
because different parts of a system may modify the same instance, making
it difficult to trace where and when changes occurred. This becomes
especially problematic when debugging unexpected behaviour.

By making `Validator` immutable, we ensure that adding rules via
`with()` returns a new instance rather than mutating the original, and
we use the `with()` method inside `__call()`, making every call to a
rule into a clone of the current `Validator`.

This provides several benefits:

1. Predictability: A `Validator` instance will always behave the same
   way throughout its lifetime, regardless of what other parts of the
   codebase do.

2. Safe dependency injection: Users can now confidently inject a base
   `Validator` from a DI container, knowing that any modifications made
   elsewhere will not affect their instance.

3. Easier debugging: Since validators cannot be mutated after creation,
   there's no need to track down where an unexpected rule was added.

4. Reusability: Users can create an initial `Validator` with some base
   rules and reuse it by just adding new rules to the chain without
   affecting the base `Validator`.
2026-01-02 15:45:23 +01:00

124 lines
3.2 KiB
Markdown

# KeySet
- `KeySet(KeyRelated $rule, KeyRelated ...$rules)`
Validates a keys in a defined structure.
```php
v::keySet(
v::keyExists('foo'),
v::keyExists('bar')
)->isValid(['foo' => 'whatever', 'bar' => 'something']); // true
```
It will validate the keys in the array with the rules passed in the constructor.
```php
v::keySet(
v::key('foo', v::intVal())
)->isValid(['foo' => 42]); // true
v::keySet(
v::key('foo', v::intVal())
)->isValid(['foo' => 'string']); // false
```
Extra keys are not allowed:
```php
v::keySet(
v::key('foo', v::intVal())
)->isValid(['foo' => 42, 'bar' => 'String']); // false
```
Missing required keys are not allowed:
```php
v::keySet(
v::key('foo', v::intVal()),
v::key('bar', v::stringType()),
v::key('baz', v::boolType())
)->isValid(['foo' => 42, 'bar' => 'String']); // false
```
Missing non-required keys are allowed:
```php
v::keySet(
v::key('foo', v::intVal()),
v::key('bar', v::stringType()),
v::keyOptional('baz', v::boolType())
)->isValid(['foo' => 42, 'bar' => 'String']); // true
```
Alternatively, you can pass a chain of key-related rules to `keySet()`:
```php
v::keySet(
v::init()
->key('foo', v::intVal())
->key('bar', v::stringType())
->keyOptional('baz', v::boolType())
)->isValid(['foo' => 42, 'bar' => 'String']); // true
```
It is not possible to negate `keySet()` rules with `not()`.
The keys' order is not considered in the validation.
## Templates
### `KeySet::TEMPLATE_STANDARD`
| Mode | Template |
| ---------- | ----------------------------- |
| `default` | {{subject}} validation failed |
| `inverted` | {{subject}} validation passed |
### `KeySet::TEMPLATE_BOTH`
| Mode | Template |
| ---------- | ------------------------------------------------ |
| `default` | {{subject}} contains both missing and extra keys |
| `inverted` | {{subject}} contains no missing or extra keys. |
### `KeySet::TEMPLATE_EXTRA_KEYS`
| Mode | Template |
| ---------- | ---------------------------------- |
| `default` | {{subject}} contains extra keys |
| `inverted` | {{subject}} contains no extra keys |
### `KeySet::TEMPLATE_MISSING_KEYS`
| Mode | Template |
| ---------- | ------------------------------------ |
| `default` | {{subject}} contains missing keys |
| `inverted` | {{subject}} contains no missing keys |
## Template placeholders
| Placeholder | Description |
| ----------- | ---------------------------------------------------------------- |
| `subject` | The validated input or the custom validator name (if specified). |
## Categorization
- Arrays
- Nesting
- Structures
## Changelog
| Version | Description |
| ------: | ----------------------------------------------------- |
| 3.0.0 | Requires at least one key-related rule |
| 2.3.0 | KeySet is NonNegatable, fixed message with extra keys |
| 1.0.0 | Created |
---
See also:
- [ArrayVal](ArrayVal.md)
- [Key](Key.md)