Tool for easy and powerful data validations

1.4.1 2017-07-24 22:53 UTC

This package is not auto-updated.

Last update: 2024-12-21 20:46:42 UTC


README

Tool for easy and powerful data validations.

License Build Status SensioLabsInsight

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 between 100 and 200.
  • 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 of 3 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: