The `findByPath()` method was failing to return results when using nested dot-notation paths such as `user.email` or `items.1`. However, it’s returning `null` instead of the expected result in some cases. The root cause was a mismatch between how paths are stored vs searched: - Storage: Validators like Key and Each create results where the path is stored as a linked list. For `user.email`, the "email" result has `path="email"` with `parent="user"`. - Search (old): The method expected a tree structure where it would find a child with `path="user"`, then search that child for `path="email"`. But no child had `path="user"` - only "email" (with "user" as its parent). The fix computes each result's full path by walking up the parent chain and compares it against the search path. Also converts numeric strings to integers when parsing paths (e.g., `items.1` → `['items', 1]`) since array indices are stored as integers. While working on this fix, I also realised that to expose the result's status, it’s best to use `hasFailed()` instead of `isValid()` in `ResultQuery`, since users will mostly use results when validation failed, not when it passed. Assisted-by: Claude Code (Opus 4.5)
2.4 KiB
Getting Started
Welcome to Respect\Validation!
This guide will help you get up and running with the library quickly.
Installation
To install Respect\Validation, use Composer:
composer require respect/validation:^3.0
Ensure you have PHP 8.5 or above installed.
Basic usage
The ValidatorBuilder (aliased as v for convenience) provides a fluent interface for building validators and running them.
Validating using exceptions
The assert() method throws an exception when validation fails. Handle these exceptions with try/catch for robust error handling:
try {
v::intType()->assert($input);
} catch (Throwable $exception) {
echo 'Validation failed: ' . $exception->getMessage();
}
Validating without exceptions
The validate() method returns a ResultQuery object that allows you to inspect and display validation results:
$result = v::intType()->validate($input);
if ($result->hasFailed()) {
echo 'Validation failed: ' . $result->getMessage();
}
Validating using booleans
Use the isValid() method to check if your input meets specific validation criteria:
if (!v::intType()->isValid($input)) {
echo 'The input you gave me is not an integer';
}
Key Features
Complex validation
Combine multiple validators for complex validation rules:
v::numericVal()->positive()->between(1, 255)->assert($input);
Custom error messages
Define your own error messages when validation fails:
v::between(1, 256)->assert($input, '{{subject}} is not what I was expecting');
Custom exceptions
Throw your own exceptions when the validation fails:
try {
v::between(1, 256)->assert($input, new DomainException('Not within the expected range'));
} catch (DomainException $exception) {
echo 'Custom exception caught: ' . $exception->getMessage();
}
Reusing validators
Create validators once and reuse them across multiple inputs:
$validator = v::alnum()->lowercase();
$validator->assert('respect');
$validator->assert('validation');
Next steps
- Explore the Feature Guide for more advanced usage.
- Check out the List of Validators by Category for a comprehensive list of available validators.