2017 © Pedro Peláez
 

library validation

Validation framework.

image

limoncello-php/validation

Validation framework.

  • Wednesday, July 25, 2018
  • by neomerx
  • Repository
  • 1 Watchers
  • 0 Stars
  • 2,218 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 49 Versions
  • 6 % Grown

The README.md

Scrutinizer Code Quality Code Coverage Build Status License, (*1)

This validation library fast, easy to use yet very powerful and flexible solution. Unlike many other libraries it does not try to give you 'validation rules' for all possible cases because those implementations might not fit your requirements and using such libraries is a pain. Instead it provides an extremely simple way of adding custom validation rules., (*2)

Also it supports caching of validation rules which makes it very fast. Custom error codes and messages are supported as well. Error messages could be customized/localized and support placeholders., (*3)

Usage sample, (*4)

$validator = v::validator([
    'sku'           => r::required(r::sku()),
    'amount'        => r::required(r::amount(5)),
    'delivery_date' => r::nullable(r::deliveryDate()),
    'email'         => r::email(),
    'address1'      => r::required(r::address1()),
    'address2'      => r::address2(),
    'accepted'      => r::required(r::areTermsAccepted()),
]);

$input = [
    'sku'    => '...',
    'amount' => '...',
    ...
];

if ($validator->validate($input)) {
    // use validated/converted/sanitized inputs
    $validated = $validator->getCaptures();
} else {
    // print validation errors
    $errors = $validator->getErrors();
}

Full sample code is here., (*5)

As you can see such custom rules as sku, amount, deliveryDate, address1, address2 and areTermsAccepted could be perfectly combined with built-in required and nullable. It makes the rules reusable in CREATE and UPDATE operations where typically inputs are required on creation and optional on update., (*6)

How easy to write those rules? Many could be made from built-in ones below (e.g. amount, address1, address2 and areTermsAccepted), (*7)

equals, notEquals, inValues, lessThan, lessOrEquals, moreThan, moreOrEquals, between, stringLengthBetween, stringLengthMin, stringLengthMax, regexp, nullable, stringToBool, stringToDateTime, stringToFloat, stringToInt, stringArrayToIntArray, andX, orX, ifX, success, fail, required, enum, filter, isArray, isString, isBool, isInt, isFloat, isNumeric, isDateTime, (*8)

class Rules extends \Limoncello\Validation\Rules
{
    public static function sku(): RuleInterface
    {
        return static::stringToInt(new IsSkuRule());
    }

    public static function amount(int $max): RuleInterface
    {
        return static::stringToInt(static::between(1, $max));
    }

    public static function deliveryDate(): RuleInterface
    {
        return static::stringToDateTime(DateTime::ISO8601, new IsDeliveryDateRule());
    }

    public static function email(): RuleInterface
    {
        return static::isString(
            static::filter(FILTER_VALIDATE_EMAIL, null, Errors::IS_EMAIL, static::stringLengthMax(255))
        );
    }

    public static function address1(): RuleInterface
    {
        return static::isString(static::stringLengthBetween(1, 255));
    }

    public static function address2(): RuleInterface
    {
        return static::nullable(static::isString(static::stringLengthMax(255)));
    }

    public static function areTermsAccepted(): RuleInterface
    {
        return static::stringToBool(static::equals(true));
    }
}

Custom rule such as IsSkuRule might require quering database and could be added with minimal overhead, (*9)

class IsSkuRule extends ExecuteRule
{
    public static function execute($value, ContextInterface $context): array
    {
        $pdo   = $context->getContainer()->get(PDO::class);
        $isSku = ...;

        return $isSku === true ?
            self::createSuccessReply($value) :
            self::createErrorReply($context, $value, Errors::IS_VALID_SKU);
    }
}

When validator is created a developer can pass PSR Container with custom services and have access to this container from validation rules. Thus validation could be easily integrated with application logic., (*10)

Sample application, (*11)

Installation

$ composer require limoncello-php/validation

Note: for message translation PHP-intl is needed., (*12)

Issues

Any related issues please send to limoncello., (*13)

Testing

$ composer test

The Versions

25/07 2018
25/07 2018
27/06 2017
01/06 2017
01/06 2017
01/06 2017
01/06 2017
01/06 2017
01/06 2017
01/06 2017
01/06 2017
01/06 2017
25/05 2017
09/05 2017
09/05 2017
09/05 2017
09/05 2017
09/05 2017
09/05 2017

0.6.2

0.6.2.0 https://github.com/limoncello-php/framework/tree/master/components/Validation

Limoncello framework validation support

  Sources   Download

Apache-2.0

The Requires

  • php >=7.0.0
  • ext-intl *

 

The Development Requires

by Avatar neomerx

framework validation limoncello

06/05 2017

0.6.0

0.6.0.0 https://github.com/limoncello-php/framework/tree/master/components/Validation

Limoncello framework validation support

  Sources   Download

Apache-2.0

The Requires

  • php >=7.0.0
  • ext-intl *

 

The Development Requires

by Avatar neomerx

framework validation limoncello

06/05 2017

0.6.1

0.6.1.0 https://github.com/limoncello-php/framework/tree/master/components/Validation

Limoncello framework validation support

  Sources   Download

Apache-2.0

The Requires

  • php >=7.0.0
  • ext-intl *

 

The Development Requires

by Avatar neomerx

framework validation limoncello

18/03 2017

0.6.x-dev

0.6.9999999.9999999-dev https://github.com/limoncello-php/framework/tree/master/components/Validation

Limoncello framework validation support

  Sources   Download

Apache-2.0

The Requires

  • php >=7.0.0
  • ext-intl *

 

The Development Requires

by Avatar neomerx

framework validation limoncello

18/03 2017

0.5.2

0.5.2.0 https://github.com/limoncello-php/framework/tree/master/components/Validation

Limoncello framework validation support

  Sources   Download

Apache-2.0

The Requires

  • php >=7.0.0
  • ext-intl *

 

The Development Requires

by Avatar neomerx

framework validation limoncello

18/03 2017

0.5.3

0.5.3.0 https://github.com/limoncello-php/framework/tree/master/components/Validation

Limoncello framework validation support

  Sources   Download

Apache-2.0

The Requires

  • php >=7.0.0
  • ext-intl *

 

The Development Requires

by Avatar neomerx

framework validation limoncello

18/03 2017

0.5.4

0.5.4.0 https://github.com/limoncello-php/framework/tree/master/components/Validation

Limoncello framework validation support

  Sources   Download

Apache-2.0

The Requires

  • php >=7.0.0
  • ext-intl *

 

The Development Requires

by Avatar neomerx

framework validation limoncello

18/03 2017

0.5.5

0.5.5.0 https://github.com/limoncello-php/framework/tree/master/components/Validation

Limoncello framework validation support

  Sources   Download

Apache-2.0

The Requires

  • php >=7.0.0
  • ext-intl *

 

The Development Requires

by Avatar neomerx

framework validation limoncello

17/03 2017

0.5.0

0.5.0.0 https://github.com/limoncello-php/framework/tree/master/components/Validation

Limoncello framework validation support

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0
  • ext-intl *

 

The Development Requires

by Avatar neomerx

framework validation limoncello

17/03 2017

0.5.1

0.5.1.0 https://github.com/limoncello-php/framework/tree/master/components/Validation

Limoncello framework validation support

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0
  • ext-intl *

 

The Development Requires

by Avatar neomerx

framework validation limoncello