carlosv2 / can
Tool for easy and powerful data validations
Installs: 1 256
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: ~5.4 || ~7.0
Requires (Dev)
- phpspec/phpspec: ^2.5
This package is not auto-updated.
Last update: 2024-11-09 20:12:20 UTC
README
Tool for easy and powerful data validations.
Why
Imagine I give you this data:
$data = [
'key1' => 123,
'key2' => 'abc',
'key3' => true
];
And then I ask you to validate it according to the following constraints:
- It must be an array.
key1
must contain an integer between100
and200
.key2
must contain a non-empty string.key3
must contain either a boolean or a callable.- If
key4
is set, it must be an array with a maximum of3
items. - It cannot have any other key.
How long would your code to validate $data
be?
Using this project, it can be validated with this code:
can($data)->claim(To::beArray()
->withKey('key1')->expected(To::beInteger()->withMin(100)->withMax(200))
->withKey('key2')->expected(To::beString()->withMinLength(1))
->withKey('key3')->expected(To::beOneOf(To::beBoolean(), To::beCallable()))
->withKey('key4')->expected(To::beOptionalAnd(To::beArray()->withMaxCount(3)))
->withNoOtherKeys()
);
Install
Open a command console, enter your project directory and execute the following command to download the latest stable version of this project:
$ composer require carlosv2/can
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Documentation
Available documentation: