You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
4
4
5
5
## [Unreleased]
6
6
7
+
### Added
8
+
-`getFlattenedErrors()` method on `ValidationException` returning a flattened list of errors suitable for API consumption. Each error entry contains `path` (field path with `'_root'` for root-level errors) and `message` (error message). Supports nested structures, array items, and maintains consistent path notation.
9
+
-`ValidationException::flattenErrors()` static method for flattening errors from `tryValidate()` results without needing to catch exceptions. Returns empty array when passed `null`.
Copy file name to clipboardExpand all lines: README.md
+12-1Lines changed: 12 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,6 +30,7 @@ Rather than reimplementing every possible transformation or validation rule, Lem
30
30
-**Form Safety First**: Empty strings coerce to `null` (not dangerous `0`/`false`) to prevent real-world issues like accidental zero bank balances
31
31
-**Fluent API with Execution Order Guarantee**: Validation rules read like natural language and execute in the exact order written -- `Validator::isString()->pipe('trim')->nullifyEmpty()->required()`
32
32
-**Comprehensive Error Collection**: All validation errors are collected, not just the first failure
33
+
-**API-Friendly Error Format**: Flattened errors with field paths (`'_root'` for root-level, dot notation for nested) perfect for frontend consumption
33
34
-**Type-Aware Transformations**: Intelligent transformation system that maintains type context and handles coercion automatically
34
35
-**Extensible Architecture**: Generic transformation methods work with any PHP callable or external library
35
36
-**Strict Typing**: All files use `declare(strict_types=1);`, so scalar mismatches raise clear errors; opt into `coerce()` when you need form-friendly conversions.
@@ -41,6 +42,7 @@ Rather than reimplementing every possible transformation or validation rule, Lem
41
42
-**Type-safe validation** for strings, integers, floats, arrays, and objects
42
43
-**Fluent, chainable API** with guaranteed execution order -- methods execute exactly as written in the chain
43
44
-**Comprehensive error collection** with detailed, structured feedback
45
+
-**API-friendly flattened errors** with field paths for easy frontend integration (`getFlattenedErrors()`, `ValidationException::flattenErrors()`)
44
46
-**Intuitive custom validation** with `satisfies()` method and optional error messages
45
47
-**Logical combinators** (`Validator::allOf()`, `Validator::anyOf()`, `Validator::not()`) for complex validation logic
46
48
-**Form-safe coercion** - empty strings become `null` (not dangerous `0`/`false`) for real-world safety
Copy file name to clipboardExpand all lines: TASKS.md
+15-6Lines changed: 15 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,14 +2,23 @@
2
2
3
3
Public repo; keep this list short (about 7) and up to date. Rules: no numbering (keeps churn low), prune completed items, and replace them with the next priority. Contributors: pick one task, keep PRs focused, and update the list as things land.
4
4
5
-
- Universal enum/const
6
-
- Add `enum()` and `const()`validators available on all types; cover mixed scalar inputs and document usage.
5
+
- Universal enum/const validators
6
+
- Add `enum()` and `const()`methods available on all validator types; handle mixed scalar inputs and document usage patterns.
7
7
8
-
- Error metadata
9
-
- Introduce structured error codes and enhanced error paths for nested schemas; keep backward compatibility for existing error shapes.
8
+
- Structured error codes
9
+
- Add programmatic error codes to validation errors (e.g., 'STRING_TOO_SHORT', 'INVALID_EMAIL') for better error handling and i18n support; keep backward compatibility.
10
+
11
+
-`in()` alias for `oneOf()`
12
+
- Add `in()` method as an alias for `oneOf()` for more intuitive API (`->in(['active', 'inactive'])` reads better than `->oneOf()`).
13
+
14
+
-`between()` convenience methods
15
+
- Add `between(min, max)` shorthand for strings (length) and numerics (range) to reduce boilerplate (`->between(3, 50)` vs `->minLength(3)->maxLength(50)`).
16
+
17
+
-`notEmpty()` validation method
18
+
- Add explicit `notEmpty()` method for validating non-empty strings/arrays; clearer than custom validation for common use case.
10
19
11
20
- Mutation testing pilot
12
-
- Wire Infection (or similar) to the suite, add baseline config, and document how to run it locally.
21
+
- Wire Infection (or similar) to the test suite, add baseline config, and document how to run it locally for enhanced test quality verification.
13
22
14
23
- Property-based tests
15
-
- Add a small property-based test set for key validators (string patterns, numeric constraints) to harden edge cases.
24
+
- Add a small property-based test set for key validators (string patterns, numeric constraints) to harden edge cases and catch regressions.
Copy file name to clipboardExpand all lines: docs/guides/error-handling.md
+86Lines changed: 86 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -62,6 +62,92 @@ try {
62
62
}
63
63
```
64
64
65
+
## Flattened Errors for API Consumption
66
+
67
+
For API responses, you often need a flat list of errors with field paths. The `ValidationException` class provides methods to convert nested error structures into a flattened format suitable for frontend consumption.
68
+
69
+
### Using getFlattenedErrors() with Exceptions
70
+
71
+
When catching a `ValidationException`, use `getFlattenedErrors()` to get a flattened list:
72
+
73
+
```php
74
+
use Lemmon\Validator\ValidationException;
75
+
76
+
try {
77
+
$schema->validate($input);
78
+
} catch (ValidationException $e) {
79
+
$flattened = $e->getFlattenedErrors();
80
+
// Returns: [
81
+
// ['path' => 'name', 'message' => 'Value is required'],
82
+
// ['path' => 'email', 'message' => 'Value must be a valid email address'],
Base64Variant::UrlSafe => preg_match('/^[A-Za-z0-9+\/]*={0,2}$/', $value) === 1// Try standard Base64 first // URL-safe parsers accept both standard and URL-safe variants
0 commit comments