Download the PHP package philiprehberger/php-schema-validator without Composer
On this page you can find all versions of the php package philiprehberger/php-schema-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download philiprehberger/php-schema-validator
More information about philiprehberger/php-schema-validator
Files in philiprehberger/php-schema-validator
Package php-schema-validator
Short Description Fluent data schema validator with nested objects, arrays, and dot-notation errors
License MIT
Homepage https://github.com/philiprehberger/php-schema-validator
Informations about the package php-schema-validator
PHP Schema Validator
Fluent data schema validator with nested objects, arrays, and dot-notation errors.
Requirements
- PHP 8.2+
Installation
Usage
Nested Objects
Typed Arrays
Optional and Nullable Fields
String Validators
Enum Values
Any Value
Custom Validation Rules
Add custom validation logic to any schema type using custom(). The callable receives the value and returns null if valid, or an error message string if invalid.
Custom validators only run when all built-in checks pass.
Value Transformers
Use transform() to normalize a value before validation. The callable receives the raw value and returns the transformed value.
Transformers run before any type or constraint checks (but after the null check).
Cross-Field Validation
Use crossField() on an ObjectSchema to validate relationships between fields. Each callable receives the full data array and returns null if valid, or an error message string.
Cross-field validators only run when all individual field validations pass.
Conditional Fields
Use when() on an ObjectSchema to conditionally require additional fields based on the value of another field.
Schema Composition
Use extend() to create a new schema that combines the fields of the current schema with additional fields.
Custom Error Messages
Use withMessages() on a ValidationResult to replace default error messages for specific fields.
API
Schema (static factory)
| Method | Returns | Description |
|---|---|---|
Schema::object(array $fields) |
ObjectSchema |
Create an object schema with field definitions |
Schema::string() |
StringSchema |
Create a string schema |
Schema::int() |
IntSchema |
Create an integer schema |
Schema::float() |
FloatSchema |
Create a float schema |
Schema::bool() |
BoolSchema |
Create a boolean schema |
Schema::arrayOf(SchemaType $item) |
ArraySchema |
Create a typed array schema |
Schema::enum(array $values) |
EnumSchema |
Create an enum schema |
Schema::any() |
AnySchema |
Create a schema that accepts any value |
ValidationResult
| Method | Returns | Description |
|---|---|---|
passes() |
bool |
True if validation passed |
fails() |
bool |
True if validation failed |
errors() |
array<string> |
All error messages |
firstError() |
?string |
First error message or null |
withMessages(array $messages) |
ValidationResult |
Replace errors for matching field paths with custom messages |
ObjectSchema extras
| Method | Description |
|---|---|
crossField(callable $validator) |
Add a cross-field validator (receives full data array, returns ?string) |
when(string $field, mixed $value, array $thenSchema) |
Conditionally validate additional fields when a field matches a value |
extend(array $additionalFields) |
Create a new schema combining current fields with additional fields |
Common Modifiers
All schema types support:
| Method | Description |
|---|---|
optional() |
Field may be absent from the parent object |
nullable() |
Field may be null |
custom(callable $validator) |
Add a custom validation callback (receives value, returns ?string) |
transform(callable $transformer) |
Transform the value before validation |
Development
Support
If you find this project useful:
License
MIT