khalyomede / validator
Validate arrays.
Requires
- php: >=7.2.0
- khalyomede/array-get: 0.*
Requires (Dev)
- phpunit/phpunit: 7.*
README
Validate arrays.
$validator = new Validator([ 'name' => ['required', 'string'], 'hobbies' => ['array'], 'hobbies.*' => ['string'] ]); $validator->validate([ 'name' => 'John', 'hobbies' => ['programming', 'comics', 'workout'] ]); var_dump($validator->failed()); // false
Summary
Installation
In your project root:
composer require khalyomede\validator:0.*
Examples
- Example 1: validating a string
- Example 2: validating a required element
- Example 3: add a new rule
- Example 4: check if a rule already exist or not
- Example 5: using dot syntax to validate multiple elements
Example 1: validating a string
require __DIR__ . '/../vendor/autoload.php'; use Khalyomede\Validator; use Khalyomede\Exception\RuleNotFoundException; $validator = new Validator([ 'name' => ['string'] ]); try { $validator->validate(['name' => 'John']); var_dump($validator->failed()); // false } catch( RuleNotFoundException $exception ) { echo "rule {$exception->getRule()} does not exists"; exit(1); }
Example 2: validating a required element
require __DIR__ . '/../vendor/autoload.php'; use Khalyomede\Validator; use Khalyomede\Exception\RuleNotFoundException; $validator = new Validator(['name' => ['required', 'string']]); try { $validator->validate(['name' => 'John']); var_dump($validator->failed()); // false } catch( RuleNotFoundException $exception ) { echo "rule {$exception->getRule()} does not exist"; exit(1); }
Example 3: add a new rule
equire __DIR__ . '/../vendor/autoload.php'; use Khalyomede\Validator; use Khalyomede\Exception\RuleAlreadyExistException; use Khalyomede\Exception\RuleNotFoundException; try { Validator::extends('longitude', function($value, $key, $items) { return is_float($value) && ($value >= -180) && ($value <= 180); }); } catch( RuleAlreadyExistException $exception ) { echo "rule {$exception->getRule()} already exist"; exit(1); }
Example 4: check if a rule already exist or not
require __DIR__ . '/../vendor/autoload.php'; use Khalyomede\Validator; if (Validator::has('ip') === false) { echo "rule ip does not exist yet"; }
Example 5: using dot syntax to validate multiple elements
require __DIR__ . '/../vendor/autoload.php'; use Khalyomede\Validator; $validator = new Validator([ 'sith' => ['required', 'array'], 'sith.*' => ['string'] ]); $validator->validate([ 'sith' => ['Darth Maul', 'Darth Vador', 'Darth Sidious'] ]); var_dump( $validator->failed() ) // "false", hm... should have been true after all these guys did but anyway
Rules
array
Validate that a key is an array.
$validator = new Validator([ 'hobbies' => ['array'] ]);
date
Validate that a key is filled with a valid date in format yyyy-mm-dd
(ISO 8601).
$validator = new Validator([ 'created_at' => ['date'] ]);
datetime
Validate that a key is filled with a valid date in formt yyyy-mm-dd hh:mm:ss
(ISO 8601).
$validator = new Validator([ 'updated_at' => ['datetime'] ]);
Validate that a key is filled with an email.
$validator = new Validator([ 'contact' => ['email'] ]);
filled
Validate that a key is filled with a non empty value.
$validator = new Validator([ 'name' => ['filled'] ]);
integer
Validate that a key is filled with an integer.
$validator = new Validator([ 'age' => ['integer'] ]);
lower
Validate that a key is filled only with lowercases (non-alpha characters are allowed as well).
$validator = new Validator([ 'street' => ['lower'] ]);
present
Validate that a key exists.
$validator = new Validator([ 'lastname' => ['present'] ]);
required
Validate that a key is present. The key can be empty by the way.
$validator = new Validator([ 'name' => ['required'] ]);
same
Validate that two keys are the same.
$validator = new Validator([ 'password' => ['string', 'same:confirmation'], 'confirmation' => ['string'] ]);
slug
Validate that a string is a slug (only lowercases, dashes -
allowed).
$validator = new Validator([ 'title' => ['slug'] ]);
string
Validate that a key is a string.
$validator = new Validator([ 'name' = ['string'] ]);
time
Validate that a key is filled with a time with the format hh:mm:ss
.
$validator = new Validator([ 'duration' => ['time'] ]);
upper
Validate that a string is only in uppercase.
$validator = new Validator([ 'name' => ['upper'] ]);
Helpers
extends
Add a new rule.
Validator::extends('jedi', function($value, $key, $items) { return in_array($value, ['qui-gon jinn', 'obiwan', 'luke']); });
has
Check if the rule already exist.
Validator::has('sith'); // bool(false)