php-smart-validator
is a flexible and extensible PHP package for validating data. It provides a set of common validation rules and allows for custom rule definitions. This package is designed to be easy to use and integrate into any PHP project.
- Supports common validation rules like
required
- Easy to extend with new validation rules
- PSR-4 autoloading for seamless integration
You can install the package via Composer. Run the following command in your terminal:
composer require GeekGroveOfficial/php-smart-validator
The following validation rules are supported out of the box:
- required: Ensures the field is present and not empty.
- min: Ensures the field value is at least a certain length or size.
- max: Ensures the field value does not exceed a certain length or size.
- string: Ensures the field value is a string.
- integer or int: Ensures the field value is an integer.
- boolean or bool: Ensures the field value is a boolean.
- email: Ensures the field value is a valid email address.
- date: Ensures the field value is a valid date.
- array: Ensures the field value is an array.
- in: Ensures the field value is within a specified set of values.
- url: Ensures the field value is a valid URL.
- ip: Ensures the field value is a valid IP address.
- json: Ensures the field value is a valid JSON string.
- regex: Ensures the field value matches a specified regular expression pattern.
- uuid: Ensures the field value is a valid UUID.
- image: Ensures the field value is a valid image file.
- files: Ensures the field value is valid files according to specified rules.
First, create a validation instance by passing the data and rules:
use GeekGroveOfficial\PhpSmartValidator\Validator\Validator;
$data = [
'email' => '[email protected]',
'password' => 'secret'
];
$rules = [
'email' => ['required','email'],
'password' => ['required','min:6']
];
$validator = new Validator($data, $rules);
if ($validator->validate()) {
echo "Validation passed!";
} else {
print_r($validator->errors());
}
To ensure everything is working correctly, you can run the tests using PHPUnit. First, install PHPUnit if you haven't already:
composer require --dev phpunit/phpunit
Then run the tests:
./vendor/bin/phpunit
Contributions are welcome! If you have suggestions, improvements, or bug fixes, please follow these steps:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/your-feature
). - Commit your changes (
git commit -am 'Add new feature'
). - Push to the branch (
git push origin feature/your-feature
). - Open a pull request.
Please ensure that your code adheres to the existing style and includes appropriate tests.