|
1 | 1 | # Lemmon Validator Project Summary |
2 | 2 |
|
3 | | -This project is a lightweight, fluent validation library for PHP, inspired by Valibot and Zod. It provides a structured way to validate various data types, including associative arrays, plain arrays, strings, integers, and booleans, with support for coercion, required fields, default values, and `oneOf` constraints. |
| 3 | +This project is a lightweight, fluent validation library for PHP, inspired by Valibot and Zod. It provides a structured way to validate various data types, including associative arrays, `stdClass` objects, plain arrays, strings, integers, and booleans. It features a symmetrical and predictable API for handling different data structures. |
4 | 4 |
|
5 | 5 | ## Key Components: |
6 | 6 |
|
7 | | -* **`Validator.php`**: Acts as a factory for creating different validator instances (e.g., `isAssociative()`, `isArray()`, `isString()`). The `isAssociative()` method can now be called without arguments to create an empty schema. |
8 | | -* **`SchemaValidator.php`**: Handles validation for associative arrays, allowing definition of a schema with `FieldValidator` instances. **Crucially, it now extends `FieldValidator.php`**, inheriting its `validate()` and `tryValidate()` methods. Its core schema validation logic resides in its `validateType()` method. |
9 | | -* **`FieldValidator.php`**: An abstract base class for individual field validators. It defines common validation methods like `required()`, `default()`, `coerce()`, and `oneOf()`. It implements a unified `validate()` (throws exception) and `tryValidate()` (returns status, data, errors) pattern. Error messages are now generic, with field context provided by the error object's structure. |
10 | | -* **Specific Validators (e.g., `StringValidator.php`, `IntValidator.php`, `BoolValidator.php`, `ArrayValidator.php`)**: Implement the abstract methods from `FieldValidator.php` to provide type-specific validation and coercion logic. The `ArrayValidator.php` now coerces empty strings to empty arrays. |
11 | | -* **`ValidationException.php`**: Custom exception class used to report validation errors. |
| 7 | +* **`Validator.php`**: Acts as a factory for creating different validator instances (e.g., `isAssociative()`, `isObject()`, `isArray()`). |
| 8 | +* **`AssociativeValidator.php`**: Handles validation for associative arrays. It can be configured with `->coerce()` to automatically convert a `stdClass` object into an associative array before validation. |
| 9 | +* **`ObjectValidator.php`**: Handles validation for `stdClass` objects. It can be configured with `->coerce()` to automatically convert an associative array into a `stdClass` object before validation. |
| 10 | +* **`FieldValidator.php`**: An abstract base class for all individual validators. It defines common validation methods like `required()`, `default()`, `coerce()`, and `oneOf()`. It implements a unified `validate()` (throws exception) and `tryValidate()` (returns result tuple) pattern. |
| 11 | +* **Specific Validators (e.g., `StringValidator.php`, `IntValidator.php`, `ArrayValidator.php`)**: Implement type-specific validation and coercion logic. |
| 12 | +* **`ValidationException.php`**: Custom exception class used to report validation errors, capable of holding a nested structure of error messages. |
12 | 13 |
|
13 | 14 | ## Development Setup: |
14 | 15 |
|
15 | 16 | * **Dependencies**: Managed by Composer (`composer.json`). |
16 | | -* **Testing**: Uses Pest PHP (`tests/ValidatorTest.php`). |
17 | | -* **Code Style**: Enforced by PHP-CS-Fixer. |
18 | | -* **Static Analysis**: Performed by PHPStan. |
19 | | -* **Local Testing**: A `temp/` directory is used for ad-hoc CLI tests, with a `_bootstrap.php` file for common setup (Composer autoloader, Symfony ErrorHandler). The `temp/` directory is excluded from Git via `.gitignore`. |
20 | | -* **Debugging**: `symfony/var-dumper` is included as a dev dependency for easy debugging. |
21 | | -* **Error Handling**: `symfony/error-handler` is included as a dev dependency and registered in the bootstrap for improved error reporting. |
| 17 | +* **Testing**: Uses Pest PHP (`tests/ValidatorTest.php`). The test suite is run with `composer test`. |
| 18 | +* **Code Style**: Enforced by PHP-CS-Fixer. Can be checked with `composer lint` and fixed with `composer fix`. |
| 19 | +* **Static Analysis**: Performed by PHPStan. Can be run with `composer analyse`. |
| 20 | +* **Debugging**: `symfony/var-dumper` is included as a dev dependency. |
| 21 | +* **Error Handling**: `symfony/error-handler` is included as a dev dependency. |
0 commit comments