limoncello-php / validation
Validation framework.
Requires
- php: >=7.3.0
- limoncello-php/common: ^0.10.0
- limoncello-php/contracts: ^0.10.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpmd/phpmd: ^2.8
- phpunit/phpunit: ^9.0
- scrutinizer/ocular: ^1.4
- squizlabs/php_codesniffer: ^2.9
- dev-master
- dev-develop / 0.10.x-dev
- 0.10.0
- 0.9.3
- 0.9.2
- 0.9.1
- 0.9.0
- 0.8.10
- 0.8.9
- 0.8.8
- 0.8.7
- 0.8.6
- 0.8.5
- 0.8.4
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8.0
- 0.7.4
- 0.7.3
- 0.7.2
- 0.7.1
- 0.7.0
- 0.6.x-dev
- 0.6.18
- 0.6.17
- 0.6.16
- 0.6.15
- 0.6.14
- 0.6.13
- 0.6.12
- 0.6.11
- 0.6.10
- 0.6.9
- 0.6.8
- 0.6.7
- 0.6.6
- 0.6.5
- 0.6.4
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.5
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
This package is auto-updated.
Last update: 2024-11-10 01:49:23 UTC
README
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.
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
.
Usage sample
$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.
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.
How easy to write those rules? Many could be made from built-in ones below (e.g. amount
, address1
, address2
and areTermsAccepted
)
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
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
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.
Installation
$ composer require limoncello-php/validation
Note: for message translation PHP-intl is needed.
Issues
Any related issues please send to limoncello.
Testing
$ composer test