2017 © Pedro Peláez
 

library validation

Everest - Validation Component

image

everest/validation

Everest - Validation Component

  • Wednesday, July 25, 2018
  • by inceddy
  • Repository
  • 1 Watchers
  • 0 Stars
  • 48 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 20 Versions
  • 12 % Grown

The README.md

Everest - Validation Component

The Validation Component of Everest is ment to validate user input in a simple and intuitive way., (*1)

Installing

Simply go to your project directory where the composer.json file is located and type:, (*2)

    composer require everest/validation

Usage

This package offers two methods of data validation. You can either validate a single value on a specific requirement (type) or you can validate one or more validation chains on each key of an array or array like object., (*3)

Validating single value

use Everest\Validation\Validation;

$int = Validation::integer('10'); // $int -> 10
$noint = Validation::integer('foo'); // Will throw \Everest\Validation\InvalidValidationException

Validating an array of values using a validation chain

use Everest\Validation\Validate;

$data = [
    'foo' => '10',
    'bar' => 'foo'
];

$data = Validate::lazy($data)
    ->that('foo')->integer()->between(0, 20)
    ->that('bar')->enum(['bar', 'foo'])->upperCase()
    ->execute();

// $data -> ['foo' => 10, 'bar' => 'FOO']

You can use additional validation chains by seperating them with ->or(), (*4)

use Everest\Validation\Validate;

$data = [
    'bar' => 'foo'
];

$data = Validate::lazy($data)
    ->that('bar')->integer()->between(0, 20)
    ->or()->string()->minLength(2)
    ->execute();

// $data -> ['bar' => 'FOO']

Strict and lazy validation

One can choose between Validate::strict() and Validate::lazy(). First will throw an InvalidValidationException on the first occuring error and the last will collect all occuring errors and will throw a InvalidLazyValidationException, which provices a ::getErrors() and ::getErrorsGroupedByKey() method to access all bundled InvalidValidationException exceptions., (*5)

Validating nested arrays

One can use dot-notation to validate nested values., (*6)

use Everest\Validation\Validate;

$data = [
    'foo' => [
        ['bar' => 1, 'name' => 'Some name'],
        ['bar' => 2, 'name' => 'Some other name']
    ]
];

$data = Validate::lazy($data)
    ->that('foo.*.bar')->integer()->between(0, 20)
    ->that('foo.*.name')->string()->upperCase()
    ->execute();

// $data -> [
//   'foo' => [
//     ['bar' => 1, 'name' => 'SOME NAME'],
//     ['bar' => 2, 'name' => 'SOME OTHER NAME']
//   ]
// ]

Optional parameters

Parameters can be marked as optional and as optional with default. If the validation ueses a default value as fallback this value is NOT validated by the validation chain anymore!, (*7)

use Everest\Validation\Validate;

$data = ['foo' => 10];

$result = Validate::lazy($data)
    ->that('foo')->integer()
    ->that('bar')->optional(/* no default */)->integer()
    ->execute();

// $result -> ['foo' => 10]

$result = Validate::lazy($data)
    ->that('foo')->integer()
    ->that('bar')->optional(null)->integer()
    ->execute();

// $result -> ['foo' => 10, 'bar' => null]

Types

Types are rules that a supplied value has to fulfill., (*8)

Array

Validation::array($value), validates that given value is an array., (*9)

Between

Validation::between($value, numeric $min, numeric $max), validates that given value holds $min <= $value <= $max., (*10)

Boolean

Validation::boolean($value), validates that given value is a boolean or booleanish value. The result will be casted to a boolean. This type inteprets $value as follows:, (*11)

Value Result
true true
'true' true
1 true
'1' true
false false
'false' false
0 false
'0' false

Every other value will throw an InvalidValidationException., (*12)

DateTime

Validation::dateTime($value, string $pattern), validates that given value matches the supplied date pattern and returns a new DateTime instance., (*13)

DateTimeImmutable

Same as DateTime but returns a new DateTimeImmutable instance., (*14)

Enum

Validation::dateTime($value, array $enum), validates that given value matches one of the $enum values. If $enum is an assoc array it tryes to match $value against the keys and returns the associated value., (*15)

Float

Validation::float($value), validates that given value is numeric. The result will be casted to a float., (*16)

Integer

Validation::integer($value), validates that given value is an integer or integerish. The result will be casted to an integer., (*17)

KeyExists

Validation::keyExisits($value, $key), validates that given value is an array and that the supplied key exists in this array., (*18)

Length

Validation::length($value, int $length), validates that given value matches the supplied string length using strlen., (*19)

LengthBetween

Validation::lengthBetween($value, int $min, int $max), validates that given values string length is between supplied minimum and maximum., (*20)

LengthMax

Validation::lengthMax($value, int $max), validates that given values string length lower or equal supplied maximum., (*21)

LengthMin

Validation::lengthMin($value, int $min), validates that given values string length greater or equal supplied minimum., (*22)

Max

Validation::max($value, int $max), validates that the given numerical value is lower or equal supplied maximum., (*23)

Min

Validation::min($value, int $max), validates that the given numerical value is greater or equal supplied minimum., (*24)

NotEmpty

Validation::notEmpty($value), validates that the given value is not empty., (*25)

Null

Validation::null($value), validates that the given value is null., (*26)

String

Validation::string($value), validates that the given value is a string., (*27)

Filters

Filters can be used to transfrom the validated result., (*28)

LowerCase

Validation::lowerCase($value), executes strtolower on the supplied value., (*29)

StripTags

Validation::stripTags($value), executes strip_tags on the supplied value., (*30)

Trim

Validation::trim($value), executes trim on the supplied value., (*31)

UpperCase

Validation::upperCase($value), executes strtoupper on the supplied value., (*32)

Custom Types

One can add custom types by creating a new class that extends from Everest\Validation\Types\Type., (*33)

<?php

class CustomType extends \Everest\Validation\Types\Type {

    public static $errorName = 'invalid_custom_error';
    public static $errorMessage = '%s is not a valid custom type.';

    public function __invoke($value, $message = null, string $key = null, $customArg1 = null, $customArg2 = null)
    {
        if (/* Your invalid condition here */) {
            $message = sprintf(
                self::generateErrorMessage($message ?: self::$errorMessage),
                self::stringify($value)
            );

            throw new InvalidValidationException(self::$errorName, $message, $key, $value);
        }

        /**
         * You may transform/cast the result before retuning it.
         * In this case it is usefull to add a custom argument as 
         * `$doCast` = false flag
         */


        return $value;
    }
}

In the next step you need to connect your type with the Everest\Validation\Validation class., (*34)

<?php

// Add as class. A singleton instance will be created when the type is requested the first time
\Everest\Validation\Validation::addType('custom_name', CustomType::CLASS);

// Add as instance. You can also supply a instance of your custom type. 
// E.g. when you need to do some configuration in `__construct()`
\Everest\Validation\Validation::addType('custom_name', new CustomType());

If you want to overload an existing type you need to pass true as third argument to \Everest\Validation\Validation::addType., (*35)

Now you can use the custom type by Validation::custom_type($var) or in a validation chain with ->custom_type()., (*36)

License

This project is licensed under the MIT License - see the LICENSE.md file for details, (*37)

The Versions

25/07 2018

dev-master

9999999-dev

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

25/07 2018

2.5.0

2.5.0.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

24/07 2018

2.4.1

2.4.1.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

08/05 2018

2.4.0

2.4.0.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

30/04 2018

2.3.3

2.3.3.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

30/04 2018

2.3.2

2.3.2.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

30/04 2018

2.3.1

2.3.1.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

30/04 2018

2.3.0

2.3.0.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

03/03 2018

2.2.1

2.2.1.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

19/02 2018

2.2.0

2.2.0.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

18/02 2018

2.1.1

2.1.1.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

16/02 2018

2.1.0

2.1.0.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

10/02 2018

2.0.2

2.0.2.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

10/02 2018

2.0.1

2.0.1.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

09/02 2018

2.0.0

2.0.0.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

26/01 2018

1.1.1

1.1.1.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

21/12 2017

1.1.0

1.1.0.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

12/12 2017

1.0.2

1.0.2.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

12/12 2017

1.0.1

1.0.1.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe

04/11 2017

1.0.0

1.0.0.0

Everest - Validation Component

  Sources   Download

MIT

The Requires

  • php ^7.1

 

by Philipp Steingrebe