2017 © Pedro PelĆ”ez
 

library valit

Validate http requests, input-data and method arguments at runtime using self-documenting code

image

moccalotto/valit

Validate http requests, input-data and method arguments at runtime using self-documenting code

  • Sunday, July 29, 2018
  • by moccalotto
  • Repository
  • 1 Watchers
  • 0 Stars
  • 48 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 30 Versions
  • 0 % Grown

The README.md

Valit

Travis Build Status PHP Versions Latest Stable Version License, (*1)

Validate variables using a fluent syntax., (*2)

Installation

Execute the following composer command in your terminal:, (*3)

composer require moccalotto/valit

Usage

Ensure::that($age)
    ->isNumeric()
    ->isGreaterThanOrEqual(18)
    ->isLowerThanOrEqual(75);

The example above uses the Valit\Ensure facade to validate a variable. If any of the assertions fail, a Valit\Exceptions\InvalidValueException is thrown., (*4)

Validating values

If you don't want exceptions to be thrown, use the Check facade., (*5)

You can use the throwExceptionIfNotSuccessful method to throw an exception if one or more assertions fail. The advantage of this is that the exception thrown will contain all the failed assertions, not just the first one., (*6)

use Valit\Check;

$age = 42; // the variable to validate

$validation = Check::that($age)
    ->isInt()                   // Success
    ->isGreaterThanOrEqual(42)  // Success
    ->isLessThan(100);          // Success

$validation->throwExceptionIfNotSuccessful();

// no exception cast, we continue

See also: * Using the »Ensure« facade * Status Messages * Error Messags * InvalidValueException, (*7)

Validating containers

You can easily test an entire array, for instance posted fields or a json response, in a structured and well defined way like the example below:, (*8)

$checks = Check::that($input)->contains([
    'name'      => 'string & shorterThan(100)',
    'email'     => 'email & shorterThan(255)',
    'address'   => 'string',
    'age'       => 'naturalNumber & greaterThanOrEqual(18) & lowerThanOrEqual(100)',

    'orderLines'                => 'conventionalArray',
    'orderLines/*'              => 'associative',
    'orderLines/*/productId'    => 'uuid',
    'orderLines/*/count'        => 'integer & greaterThan(0)',
    'orderLines/*/comments'     => 'optional & string & shorterThan(1024)',
]);

As you can see, check for nested data via the / character., (*9)

You can get the error messages for a single field like so:, (*10)

// get the errors associated with the top level field 'age'.
$errors = $checks->errorMessagesByPath('age');

// get the errors for the productId of the first orderLine.
$errors = $checks->errorMessagesByPath('orderLines/0/productId');

// get the error associated with the second orderLine
$errors = $checks->errorMessagesByPath('orderLines/1');

Utilities

Valit provides the Val facade that lets to do quick type juggling and assertions., (*11)

Below are ways of testing if a variable is iterable, that is agnostic of your php version., (*12)

use Valit\Util\Val;

if (!Val::is($container, 'iterable')) {
    throw new LogicException('$container should be iterable');
}

Or alternatively:, (*13)

use Valit\Util\Val;

// an InvalidArgumentException will be thrown if $container is not iterable.
Val::mustBe($container, 'iterable');

Or with your own custom exception, (*14)

use Valit\Util\Val;

$myException = throw LogicException('$container should be iterable');

// $myException will be thrown if $container is not iterable.
Val::mustBe($container, 'iterable', $myException);

Below are some of the type validations you can make., (*15)

$type Validation
null is_null()
object is_object()
int is_int()
integer is_int()
bool is_bool()
boolean is_bool()
string is_string()
float is_float()
double is_float()
numeric is_numeric()
intable stringable that can be converted to an int
nan is_nan()
inf is_inf()
callable is_callable()
iterable is_array() or is_a($value, 'Traversable')
countable is_array() or is_a($value, 'Cointable')
arrayable is_array() or is_a($value, 'ArrayAccess')
container iterable, countable and arrayable
stringable scalar or object with a__toString() method
class name is_a()
foo[] array of foo

Code examples:, (*16)

// single type
Val::mustBe($value, 'callable');

// multiple allowed types via the pipe character
Val::mustBe($value, 'float | int');

// check that $foo is an array of floats
// or an array of integers
Val::mustBe($value, 'float[] | int[]');

// mixing classes, interfaces and basic types.
Val::mustBe($value, 'int|DateTime|DateTimeImmutable');

// multiple types via array notation
Val::mustBe($value, ['object', 'array']);

// a strict array with 0-based numeric index
Val::mustBe($value, 'mixed[]');

// a strict array of strict arrays
Val::mustBe($value, 'mixed[][]');

The Versions

29/07 2018

dev-master

9999999-dev https://moccalotto.github.io/docs/valit

Validate http requests, input-data and method arguments at runtime using self-documenting code

  Sources   Download

MIT

The Requires

  • php >=5.5.9|>=7.0.8

 

The Development Requires

test validation assert ensure input verification guard expect data guard

29/07 2018

2.0.0-alpha

2.0.0.0-alpha https://moccalotto.github.io/docs/valit

Validate http requests, input-data and method arguments at runtime using self-documenting code

  Sources   Download

MIT

The Requires

  • php >=5.5.9|>=7.0.8

 

The Development Requires

test validation assert ensure input verification guard expect

29/07 2018

dev-logic-if

dev-logic-if https://moccalotto.github.io/docs/valit

Validate http requests, input-data and method arguments at runtime using self-documenting code

  Sources   Download

MIT

The Requires

  • php >=5.5.9|>=7.0.8

 

The Development Requires

test validation assert ensure input verification guard expect

05/01 2018

1.0.2

1.0.2.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

  • php >=5.5.9|>=7.0.8

 

The Development Requires

validation input verification assertion data guard

05/01 2018

1.0.1

1.0.1.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

  • php >=5.5.9|>=7.0.8

 

The Development Requires

validation input verification assertion data guard

05/01 2018

1.0.0

1.0.0.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

  • php >=5.5.9|>=7.0.8

 

The Development Requires

validation input verification assertion data guard

29/12 2017

dev-control-structures

dev-control-structures https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

  • php >=5.5.9|>=7.0.8

 

The Development Requires

validation input verification assertion data guard

28/11 2017

0.8.x-dev

0.8.9999999.9999999-dev https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

28/11 2017

0.8.1

0.8.1.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

28/11 2017

dev-test-old-php-versions

dev-test-old-php-versions https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

validation verification data guard

06/07 2017

0.8.0

0.8.0.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

03/07 2017

0.7.5

0.7.5.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

03/07 2017

0.7.6

0.7.6.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

03/07 2017

0.7.4

0.7.4.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

14/06 2017

0.7.3

0.7.3.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

26/05 2017

0.7.2

0.7.2.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

26/05 2017

0.7.1

0.7.1.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

11/05 2017

0.7.0

0.7.0.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

11/05 2017

0.6.0

0.6.0.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

30/03 2017

dev-check-container-for-errors

dev-check-container-for-errors https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

31/10 2016

0.5.9

0.5.9.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

31/10 2016

0.5.8

0.5.8.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

31/10 2016

0.5.7

0.5.7.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

27/09 2016

0.5.6

0.5.6.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

17/08 2016

0.5.5

0.5.5.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

11/08 2016

0.5.4

0.5.4.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

11/08 2016

0.5.3

0.5.3.0 https://moccalotto.github.io/docs/valit

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

11/08 2016

0.5.2

0.5.2.0

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

MIT

The Requires

 

The Development Requires

validation verification data guard

09/08 2016

0.5.1

0.5.1.0

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

The Requires

 

The Development Requires

validation verification data guard

09/08 2016

0.5.0

0.5.0.0

validate method arguments, http requests and input-data at runtime using self-documenting code

  Sources   Download

The Requires

 

The Development Requires

validation verification data guard