respect-validation/docs/validators/Lazy.md
Alexandre Gomes Gaigalas 2a7f345e32 Streamline validators.md index
Makes it so the index looks more like a cheatsheet, condensing
information instead of making long lists that require lots of
scrolling to explore.

Additionally, the happy path for each validator was also
added, providing a quick reference use for comparison.

The direct markdown links were replaced by titled markdown
references, offering mouse-over tooltips over links that
display the validator one-line description.

To ensure a proper source of truth for these new index
goodies, the AssertionMessageLinter was modified to
verify that the first assertion in each doc is a
single-line validator that passes (a happy path), further
making our documentation conventions more solid.
2026-01-28 12:47:08 +00:00

1.8 KiB
Raw Blame History

Lazy

  • Lazy(callable(mixed): Validator $validatorCreator)

Validates the input using a validator that is created from a callback.

v::lazy(static fn($input) => v::boolVal())->assert(true);
// Validation passes successfully

This validator is particularly useful when creating validators that rely on the input. A good example is validating whether a confirmation field matches the password field when processing data from a form.

v::key('confirmation', v::equals($_POST['password'] ?? null))->assert($_POST);
// → `.confirmation` must be present

The issue with the code is that its hard to reuse because youre relying upon the input itself ($_POST). That means you can create a chain of validators and use it everywhere.

The lazy() validator makes this job much simpler and more elegantly:

v::lazy(static fn($input) => v::key('confirmation', v::equals($input['password'] ?? null)))->assert($_POST);
// → `.confirmation` must be present

The code above is similar to the first example, but the biggest difference is that the creation of the validator doesn't rely on the input itself ($_POST), but it will use any input thats given to the validator

Templates

Template placeholders

Placeholder Description
subject The validated input or the custom validator name (if specified).

Categorization

  • Callables
  • Nesting

Changelog

Version Description
3.0.0 Created from KeyValue

See Also