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
feat(validator)!: return empty errors list from tryValidate on success
- Return [] instead of null so consumers can iterate without a null-guard
- Drop ?? [] guards in schema validators that consume nested tryValidate errors
- Update docs, CHANGELOG, and tests for the new tuple shape
BREAKING CHANGE: tryValidate() now returns an empty errors list on success instead of null. Check $valid or $errors === [] rather than $errors === null.
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
+
### Changed
8
+
9
+
-**BREAKING:** the errors element of the `tryValidate()` tuple is now an empty list `[]` on success (previously `null`), so consumers can iterate or count the errors without a null-guard. Code that detected success via `$errors === null` should check `$valid` (or `$errors === []`) instead; `$valid` remains the canonical success signal
Copy file name to clipboardExpand all lines: docs/getting-started/core-concepts.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,7 +40,7 @@ All validators extend `FieldValidator`, which provides:
40
40
### Core Validation Methods
41
41
42
42
-`validate(mixed $value): mixed` - Throws exception on failure
43
-
-`tryValidate(mixed $value): array` - Returns `[bool $valid, mixed $data, ?array $errors]`, where `$errors` is a list of `ValidationError` objects
43
+
-`tryValidate(mixed $value): array` - Returns `[bool $valid, mixed $data, array $errors]`, where `$errors` is a list of `ValidationError` objects (empty on success)
- `tryValidate(mixed $value, string $key = '', mixed $input = null)` returns tuple `[bool $valid, mixed $data, ?array $errors]`, where `$errors` is a flat list of `ValidationError` objects (or `null` on success).
26
+
- `tryValidate(mixed $value, string $key = '', mixed $input = null)` returns tuple `[bool $valid, mixed $data, array $errors]`, where `$errors` is a flat list of `ValidationError` objects (empty on success, so it can be iterated without a null-guard).
27
27
- **Fail-Fast:** Each validator chain stops at the first failing rule; schema validators aggregate errors across fields.
28
28
- **Error Structure:** Validation errors are a flat list of `ValidationError` value objects, each with `getPath()` (dotted, `''` at root), `getCode()` (stable code, see `ValidationCode`), `getMessage()`, and `getParams()`. Access via `ValidationException::getErrors(?string $path = null)` or the `tryValidate` tuple. `getErrors()` (no arg) returns every error; `getErrors($path)` filters to a field and everything nested beneath it (`getErrors('')` returns only root-level errors; the trailing-dot match means `getErrors('name')` won't catch `name_full`). Match on codes, not message text. `satisfies()` accepts optional `(?string $code, array $params)`; params double as `{name}` message placeholders. `ValidationError` is `JsonSerializable` (`{path, code, message, params}`), so `json_encode($e->getErrors())` is API-ready; malformed UTF-8 param keys are normalized during serialization, and unencodable param values render as `"(complex value)"`.
29
29
- **Null Handling:** Validators skip `null` values unless `required()` is called. `default()` fills in null as a last-resort fallback after the pipeline; `required()` enforces presence at the very end.
0 commit comments