|
2 | 2 |
|
3 | 3 | [](https://codecov.io/gh/WebdevCave/schema-validator-php) |
4 | 4 |  |
| 5 | + |
| 6 | +A simple schema validation library for PHP. This package allows you to define rules for your data and validate it easily. |
| 7 | + |
| 8 | +## Table of Contents |
| 9 | +- [Installation](#installation) |
| 10 | +- [Usage](#usage) |
| 11 | + - [Basic Usage](#basic-usage) |
| 12 | + - [Array Schema Example](#dataset-validation-example) |
| 13 | +- [Contributing](#contributing) |
| 14 | +- [License](#license) |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +To install the Schema Validator PHP library, you can use Composer. Run the following command: |
| 19 | + |
| 20 | +```bash |
| 21 | +composer require webdevcave/schema-validator-php |
| 22 | +``` |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +### Basic Usage |
| 27 | + |
| 28 | +```php |
| 29 | +use Webdevcave\SchemaValidator\Validator; |
| 30 | + |
| 31 | +$validator = Validator::string() |
| 32 | + ->min(3) |
| 33 | + ->max(50); |
| 34 | + |
| 35 | +// Validate the data |
| 36 | +$isValid = $validator->validate('Carlos'); |
| 37 | + |
| 38 | +if ($isValid) { |
| 39 | + echo "Data is valid!"; |
| 40 | +} else { |
| 41 | + echo "Data is invalid!"; |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +### Dataset Validation Example |
| 46 | + |
| 47 | +The library also allows you to define more complex validation rules for nested structures or arrays. For example: |
| 48 | + |
| 49 | +```php |
| 50 | +use Webdevcave\SchemaValidator\Validator; |
| 51 | + |
| 52 | +$validator = Validator::array([ |
| 53 | + 'name' => Validator::string() |
| 54 | + ->min(3) |
| 55 | + ->max(50), |
| 56 | + 'email' => Validator::string() |
| 57 | + ->pattern('/^\w+@(\w+|\.)+$/'), |
| 58 | + 'address' => Validator::array([ |
| 59 | + 'street' => Validator::string()->max(200), |
| 60 | + 'city' => Validator::string()->max(50), |
| 61 | + 'postal_code' => Validator::string() |
| 62 | + ->min(1) |
| 63 | + ->max(15), |
| 64 | + ]) |
| 65 | +]); |
| 66 | + |
| 67 | +// Data to be validated |
| 68 | +$data = [ |
| 69 | + 'name' => 'Alice Johnson', |
| 70 | + |
| 71 | + 'address' => [ |
| 72 | + 'street' => '123 Maple St', |
| 73 | + 'city' => 'Somewhere', |
| 74 | + 'postal_code' => '12345' |
| 75 | + ] |
| 76 | +]; |
| 77 | + |
| 78 | +// Validate the data |
| 79 | +$isValid = $validator->validate($data); |
| 80 | + |
| 81 | +if ($isValid) { |
| 82 | + echo "Data is valid!"; |
| 83 | +} else { |
| 84 | + echo "Data is invalid!"; |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +For objects, just use `Validator::object()` in a similar way. |
| 89 | + |
| 90 | +### Validation Error Handling |
| 91 | + |
| 92 | +If the data is invalid, you can get more detailed error information: |
| 93 | + |
| 94 | +```php |
| 95 | +use Webdevcave\SchemaValidator\Validator; |
| 96 | + |
| 97 | +// Data to be validated |
| 98 | +$data = [ |
| 99 | + 'name' => 'John Doe', |
| 100 | + 'age' => 'thirty' |
| 101 | +]; |
| 102 | + |
| 103 | +// Define the validation schema |
| 104 | +$validator = Validator::array([ |
| 105 | + 'name' => Validator::string(), |
| 106 | + 'age' => Validator::numeric() |
| 107 | + ->integer('Only integer numbers are allowed') |
| 108 | + ->positive('Age field must be positive') |
| 109 | +]); |
| 110 | + |
| 111 | +// Validate the data |
| 112 | +if (!$validator->validate($data)) { |
| 113 | + // Get and print the validation errors |
| 114 | + print_r($validator->errorMessages()); |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +This will output something like: |
| 119 | +``` |
| 120 | +Array |
| 121 | +( |
| 122 | + [age] => Array |
| 123 | + ( |
| 124 | + [0] => Only integer numbers are allowed, |
| 125 | + [1] => Age field must be positive, |
| 126 | + ) |
| 127 | +) |
| 128 | +``` |
| 129 | + |
| 130 | +## Contributing |
| 131 | + |
| 132 | +We welcome contributions to this project! If you'd like to help improve the Schema Validator PHP library, please follow these steps: |
| 133 | + |
| 134 | +### How to Contribute |
| 135 | + |
| 136 | +1. Fork this repository to your GitHub account. |
| 137 | +2. Create a new branch for your feature or fix. |
| 138 | +3. Make your changes and test them thoroughly. |
| 139 | +4. Submit a pull request with a description of your changes and why they're needed. |
| 140 | + |
| 141 | +### Code Style |
| 142 | + |
| 143 | +Please follow the [PSR-12](https://www.php-fig.org/psr/psr-12/) coding standards for PHP when making contributions. |
| 144 | + |
| 145 | +### Issues |
| 146 | + |
| 147 | +If you encounter any bugs or have suggestions for improvements, please open an issue in the GitHub repository. |
| 148 | + |
| 149 | +## License |
| 150 | + |
| 151 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments