webdevcave / schema-validator
Schema validation, 'Zod' like validation
Requires
- php: >=8.1
- ext-mbstring: *
- webdevcave/utility: ^1.0
Requires (Dev)
- phpunit/phpunit: ^10.5
- rregeer/phpunit-coverage-check: ^0.3.1
This package is auto-updated.
Last update: 2025-01-14 03:23:55 UTC
README
A simple schema validation library for PHP. This package allows you to define rules for your data and validate it easily.
Table of Contents
Installation
To install the Schema Validator PHP library, you can use Composer. Run the following command:
composer require webdevcave/schema-validator-php
Usage
Basic Usage
use Webdevcave\SchemaValidator\Validator; $validator = Validator::string() ->min(3) ->max(50); // Validate the data $isValid = $validator->validate('Carlos'); if ($isValid) { echo "Data is valid!"; } else { echo "Data is invalid!"; }
Dataset Validation Example
The library also allows you to define more complex validation rules for nested structures or arrays. For example:
use Webdevcave\SchemaValidator\Validator; $validator = Validator::array([ 'name' => Validator::string() ->min(3) ->max(50), 'email' => Validator::string() ->pattern('/^\w+@(\w+|\.)+$/'), 'address' => Validator::array([ 'street' => Validator::string()->max(200), 'city' => Validator::string()->max(50), 'postal_code' => Validator::string() ->min(1) ->max(15), ]) ]); // Data to be validated $data = [ 'name' => 'Alice Johnson', 'email' => 'alice.johnson@example.com', 'address' => [ 'street' => '123 Maple St', 'city' => 'Somewhere', 'postal_code' => '12345' ] ]; // Validate the data $isValid = $validator->validate($data); if ($isValid) { echo "Data is valid!"; } else { echo "Data is invalid!"; }
For objects, just use Validator::object()
in a similar way.
Validation Error Handling
If the data is invalid, you can get more detailed error information:
use Webdevcave\SchemaValidator\Validator; // Data to be validated $data = [ 'name' => 'John Doe', 'age' => 'thirty' ]; // Define the validation schema $validator = Validator::array([ 'name' => Validator::string(), 'age' => Validator::numeric() ->integer('Only integer numbers are allowed') ->positive('Age field must be positive') ]); // Validate the data if (!$validator->validate($data)) { // Get and print the validation errors print_r($validator->errorMessages()); }
This will output something like:
Array
(
[age] => Array
(
[0] => Only integer numbers are allowed,
[1] => Age field must be positive,
)
)
Contributing
We welcome contributions to this project! If you'd like to help improve the Schema Validator PHP library, please follow these steps:
How to Contribute
- Fork this repository to your GitHub account.
- Create a new branch for your feature or fix.
- Make your changes and test them thoroughly.
- Submit a pull request with a description of your changes and why they're needed.
Code Style
Please follow the PSR-12 coding standards for PHP when making contributions.
Issues
If you encounter any bugs or have suggestions for improvements, please open an issue in the GitHub repository.
License
This project is licensed under the MIT License - see the LICENSE file for details.