2017 © Pedro PelĂĄez
 

library jarvis

A simple but powerful way to validate data in PHP.

image

kilbiller/jarvis

A simple but powerful way to validate data in PHP.

  • Thursday, July 19, 2018
  • by kilbiller
  • Repository
  • 1 Watchers
  • 0 Stars
  • 207 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 15 Versions
  • 18 % Grown

The README.md

Jarvis

CircleCI codecov, (*1)

A simple but powerful way to validate data in PHP., (*2)

Requires php >= 7.0, (*3)

Why ?

I needed to be able to make complex validation logic easily and to get a nicely formatted array with the validation errors associated with their keys. I couldn't find anything that satisfied these requirements so I made Jarvis., (*4)

Features

  • Fluent interface
  • Treats custom validation logic as first class citizen. They're just functions !!
  • Get validation errors in a simple array, each error is associated to the input name. Only the first error for each field is kept.

Api

Validator

->addRule($key: string, $validationFunction: Function | [$validationFunction: Function], $message: string): void
$key: Can be nested (i.e. 'user.firstname').
$validationFunction: Can accept two parameter: the value corresponding to the key currently validated and the whole array. Should return a boolean.
$message: The error message, ${key} will be replaced by the key. Default message is : '${key} is not valid.'

->validate($dataToValidate: array): boolean

->getErrors(): array
->getErrors($dataToValidate: array): array

Example

use Jarvis\Validator;
use function Jarvis\rules\{between, lengthBetween, noWhiteSpace};

$data = ['age' => 45, 'firstname' => 'Jonathan', 'lastname' => 'Blow'];
$data2 = ['age' => 53, 'firstname' => 'Johnny', 'lastname' => 'Depp'];

$validator = (new Validator())
->addRule('age', between(15, 48));
->addRule('firstname', function ($firstname) {
    return $firstname === 'Jonathan';
}, '${key} is not Jonathan.');
->addRule('lastname', [noWhiteSpace(), lengthBetween(0, 50)]),

$validator->validate($data); // -> true
$validator->getErrors(); // -> []

$validator->validate($data2); // -> false
$validator->getErrors(); // -> ['age' => 'age is not valid.', 'firstname' => 'firstname is not Jonathan.']

Built-in rules

  • between(min, max)
  • isBoolean()
  • isDate(format = 'Y-m-d')
  • isJson()
  • isNull()
  • isNumber()
  • isPositive()
  • isUppercase()
  • isString()
  • lengthBetween(min, max)
  • matchRegex(regex)
  • notEmpty()
  • noWhiteSpace()
  • oneOf(...$functions)

Pro tips

There is two way to apply muliple validation rules for a single key :, (*5)

  • Use an array of validation functions -> this way you can only use a single error message
  • Call addRule multiple times with the same key -> this way you can be more precise with your error messages

All function are curried so you can built-in rules without any argument rather than '\Jarvis\rules\isNumber'., (*6)

use Jarvis\Validator;
use function Jarvis\rules\isNumber;

$validator = (new Validator())
->addRule('age', isNumber());

Todo

  • Accept objects
  • Add more default rules

The Versions

19/07 2018

dev-master

9999999-dev

A simple but powerful way to validate data in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Rémy Peru

19/07 2018

v0.1.3

0.1.3.0

A simple but powerful way to validate data in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Rémy Peru

12/07 2018

v0.1.2

0.1.2.0

A simple but powerful way to validate data in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Rémy Peru

12/07 2018

v0.1.1

0.1.1.0

A simple but powerful way to validate data in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Rémy Peru

16/05 2018

v0.1.0

0.1.0.0

A simple but powerful way to validate data in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Rémy Peru

27/10 2017

v0.0.10

0.0.10.0

A simple but powerful way to validate data in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Rémy Peru

30/08 2017

v0.0.9

0.0.9.0

A simple but powerful way to validate data in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Rémy Peru

18/05 2017

v0.0.8

0.0.8.0

A simple but powerful way to validate data in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Rémy Peru

16/05 2017

v0.0.7

0.0.7.0

A simple but powerful way to validate data in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Rémy Peru

14/05 2017

v0.0.6

0.0.6.0

A simple but powerful way to validate data in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Rémy Peru

13/05 2017

v0.0.5

0.0.5.0

A simple but powerful way to validate data in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Rémy Peru

13/05 2017

v0.0.4

0.0.4.0

A simple but powerful way to validate data in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Rémy Peru

13/05 2017

v0.0.3

0.0.3.0

A simple but powerful way to validate data in PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Rémy Peru

12/05 2017

v0.0.2

0.0.2.0

A simple but powerful way to validate data in PHP.

  Sources   Download

MIT

The Development Requires

by Rémy Peru

12/05 2017

v0.0.1

0.0.1.0

A simple but powerful way to validate data in PHP.

  Sources   Download

MIT

The Development Requires

by Rémy Peru