Skip to content

The Validator component provides tools to validate data

License

Notifications You must be signed in to change notification settings

Morebec/Validator

Repository files navigation

Validator

Build Status

The Validator component makes data validation easy. Its primary use is intended to ensure that the data of forms, Rest API and configuration files is in an appropriate and valid state.

A lot of validation libraries rely on an exception mechanism. This forces developers to check multiple related data fields one by one. This component instead uses validation errors that can be used for multiple pieces of data together. This is useful for example to return all errors for a given form at once.

Installation

composer require morebec/validator

Usage

use Morebec\Validator\Rule as Assert;
use Morebec\Validator\Validator;


// Validate a single rule for a given field
Validator::validate($form['email_address'], new Assert\One(
    new Assert\IsString('The email address field was expected to be a string')
));

// Validate Multiple Rules for a given field
Validator::validate($form['email_address'], new Assert\All([
    new Assert\IsString('The email address field was expected to be a string'),
    new Assert\NotBlank('The email address field was expected not to be blank'),
    new Assert\IsEmail('The email address field was expected to be a valid email address'),
]));


// Ensure At least one rule is valid
Validator::validate($form['email_address'], new Assert\AtLeastOne([
    new Assert\IsString('The email address field was expected to be a string'),
    new Assert\NotBlank('The email address field was expected not to be blank'),
    new Assert\IsEmail('The email address field was expected to be a valid email address'),
]));

Running Tests

php vendor/bin/phpunit --bootstrap vendor/autoload.php tests

Contributing

Thank you for your interest :)
To contribute, please read the CONTRIBUTING.md guidelines.

Getting help

To get help, open a new issue on this repository.
For Morebec team members, please use the appropriate internal channels.

About

The Validator component provides tools to validate data

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages