Skip to content

Latest commit

 

History

History
93 lines (67 loc) · 2.9 KB

README.md

File metadata and controls

93 lines (67 loc) · 2.9 KB

PHP Smart Validator

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.

Features

  • Supports common validation rules like required
  • Easy to extend with new validation rules
  • PSR-4 autoloading for seamless integration

Installation

You can install the package via Composer. Run the following command in your terminal:

composer require GeekGroveOfficial/php-smart-validator

Supported Validation Rules

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.

Basic Usage

Setting Up

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());
}

Running Tests

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

Contributing

Contributions are welcome! If you have suggestions, improvements, or bug fixes, please follow these steps:

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature/your-feature).
  3. Commit your changes (git commit -am 'Add new feature').
  4. Push to the branch (git push origin feature/your-feature).
  5. Open a pull request.

Please ensure that your code adheres to the existing style and includes appropriate tests.