Download the PHP package krak/validation without Composer
On this page you can find all versions of the php package krak/validation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download krak/validation
More information about krak/validation
Files in krak/validation
Package validation
Short Description Functional validation library
License MIT
Homepage https://github.com/krakphp/validation
Informations about the package validation
Krak Validation
The Krak Validation library is a functional and simple approach and interface to validation. It was made out of the need a validation library with a simple interface and configuration. Other leading validators like Respect, Zend, or Symfony have a lot of complexity in their API and adding an actual validator will require adding several classes in some instances.
Krak Validation changes all of this by taking a functional approach to validation where every validator is just a function or instance of Krak\Validation\Validator
. This design lends itself easily for extending, custom validators, and decorators. It comes bundled with a Krak\Validation\Kernel
which manages validators to provide a simple, fluent, and customizable interface for utilizing validators.
- Installation
- Usage
- Validators
- Violations
- Throwing Violations
- Validation Packages
- Creating a Validation Package
- Core Validation Package
- Doctrine Validation Package
- API
Installation
Install with composer at krak/validation
Usage
This will print something like this:
Validators
A validator can be a Krak\Validation\Validator
, or any callable that accepts a value and returns null on success and a violation on failure.
Validators implement the following signature:
The second parameter can typically be left out, but is used to pass additional information into the validator. A good example is a PSR Container so that dependencies can be lazy loaded at time of actual validation.
Violations
Creating violations is easy with the violate
or violations
function. For most validators, you'll simply be creating one violation.
In some cases, one validator can return multiple violations. In that case, we just use the violations
to create a violation collection. You can checkout the Krak\Validation\Validators\collection
validator to see how to create a violation collection.
Throwing Violations
Once you have a violation or a violation collection, you can optionally throw them as exceptions to be handled upstream.
Asserting Data
A very common practice is using the Validation Kernel to make and validate domain data and then throw a ViolationException if any violations occur. This can be done simply via the assert
method.
You can then easily catch the ViolationException
upstream and format the violation into readable errors:
Validation Packages
Validation Packages are simple extensions to the Validation\Kernel that register validators and messages into the system.
Creating a Validation Package
To create a validation package, you need to extend the ValidationPackage
interface.
From there, you can configure the validation kernel any which way you need.
Typically, you'll just use the validators
, messages
, and aliases
methods to add keyed validations and corresponding methods or aliases.
An example Validation Package would look like:
The validators would be defined in different files like so:
Core Validation Package
The Core Validation package defines a bunch of the normal useful validators.
all | alpha | alpha_num | array |
between | boolean | date | digits |
double | exists | float | |
in | integer | length | max |
min | null | nullable | number |
numeric | optional | regex | regex_exclude |
required | string |
all
Validates an array with the given validator. If any element in the array fails the validation, a violation will be returned.
Definition: Krak\Validation\Validators\forAll($validator)
Simple Usage:
Advanced Usage:
Standalone Usage:
alpha
Wraps the ctype_alpha
function and validates a string to verify only alpha characteres.
Definition: Krak\Validation\Validators\alpha()
Simple Usage:
Standalone Usage:
alpha_num
Wraps the ctype_alnum
function and validates a string to make sure it's alpha numeric.
Definition: Krak\Validation\Validators\alphaNum()
Simple Usage:
Standalone Usage:
array
Verifies that the value is an array using is_array
.
Definition: Krak\Validation\Validators\typeArray()
Simple Usage:
Standalone Usage:
between
Validates a value's size is between two values inclusively.
Definition: Krak\Validation\Validators\between($min, $max)
Simple Usage:
Standalone Usage:
boolean
Validates a value is a boolean. true
, false
, "0"
, "1"
, 0
, 1
are all instances of boolean.
Definition: Krak\Validation\Validators\typeBoolean()
Simple Usage:
Standalone Usage:
date
Validates a string is a valid date using strototime
. Anything that strtotime
accepts is accepted here.
Definition: Krak\Validation\Validators\date()
Simple Usage:
Standalone Usage:
digits
Validates a string is of type digits using the ctype_digits
function.
Definition: Krak\Validation\Validators\digits()
Simple Usage:
Standalone Usage:
double
Validates a value is a double using is_double
.
Definition: Krak\Validation\Validators\double()
Simple Usage:
Standalone Usage:
Validates that a string matches an email regex.
Definition: Krak\Validation\Validators\regexEmail()
Simple Usage:
Standalone Usage:
exists
Alias of required
.
float
Validates a value is a float using is_float
.
Definition: Krak\Validation\Validators\float()
Simple Usage:
Standalone Usage:
in
Validates if a values is within a given array.
Definition: Krak\Validation\Validators\inArray
Simple Usage:
Standalone Usage:
integer
Validates that a value is an integer.
Definition: Krak\Validation\Validators\typeInteger()
Simple Usage:
Standalone Usage:
length
Validates that a value's size is exactly the given length.
Definition: Krak\Validation\Validators\length($size)
Simple Usage:
Standalone Usage:
max
Validates that a value's size is less than or equal to a given max.
Definition: Krak\Validation\Validators\max($max)
Simple Usage:
Standalone Usage:
min
Validates that a value's size is greater than or equal to a given min.
Definition: Krak\Validation\Validators\min($min)
Simple Usage:
Standalone Usage:
null
Validates that a value's type is null.
Definition: Krak\Validation\Validators\typeNull()
Simple Usage:
Standalone Usage:
nullable
Validates that a type is null or not. This is typically used in a chain of validators and will stop validation if the value is null or let the validation continue on else.
Definition: Krak\Validation\Validators\nullable()
Simple Usage:
Standalone Usage:
number
Validates that a given value is either a float, double, or integer. This is not the same as is_numeric
.
Definition: Krak\Validation\Validators\number()
Simple Usage:
Standalone Usage:
numeric
Validates that a given value is numeric using the is_numeric
function.
Definition: Krak\Validation\Validators\typeNumeric()
Simple Usage:
Standalone Usage:
optional
Validates that a keyed array is optional and is not required to be present in the array. This validator only make sense within the context of a collection.
Definition: Krak\Validation\Validators\optional()
Simple Usage:
Standalone Usage:
regex
Validates that a given value is either a float, double, or integer. This is not the same as is_numeric
.
Definition: Krak\Validation\Validators\regexMatch($regex, $exclude = false)
Simple Usage:
Note defining a regex via the string can be tricky because of how the validation rule parser will validate |
, :
, and ,
. You'll almost always want to call use actual validator itself.
Advanced Usage:
Standalone Usage:
regex_exclude
Exactly like regex
except it matches that the value excludes the specific regular expression.
Definition: Krak\Validation\Validators\regexExclude($regex)
Simple Usage:
Note defining a regex via the string can be tricky because of how the validation rule parser will validate |
, :
, and ,
. You'll almost always want to call use actual validator itself.
Advanced Usage:
Standalone Usage:
required
Validates that a given value is required in a collection. This validator only makes sense within the context of a collection.
Definition: Krak\Validation\Validators\required()
Simple Usage:
Standalone Usage:
string
Validates that a given value is a string.
Definition: Krak\Validation\Validators\typeString()
Simple Usage:
Standalone Usage:
Doctrine Validation Package
The Doctrine Validation Package defines doctrine related validators.
doctrine_all_exist | doctrine_entities | doctrine_entity | doctrine_exists |
doctrine_unique | doctrine_unique_entity |
To enable the doctrine package, you can do the following:
doctrine_all_exist
Validates that a set of strings or integers all exist in a given table.
Definition: class Krak\Validation\Validators\Doctrine\AllExist($table_name, $field = 'id', $type = 'int')
Simple Usage:
Standalone Usage:
doctrine_entities
Validates that a a set of ORM Entities exist from a unique key. The given entity name is joined with the doctrine.model_prefix
if one is given.
Definition: class Krak\Validation\Validators\Doctrine\Entities($entity_name, $field = 'id')
Simple Usage:
Standalone Usage:
doctrine_entity
Validates that an entity exists in the db with the given value.
Definition: class Krak\Validation\Validators\Doctrine\Entity($entity_name, $field = 'id')
Simple Usage:
Standalone Usage:
doctrine_exists
Validates that a given value exists in a table in a certain field.
Definition: class Krak\Validation\Validators\Doctrine\Exists($table_name, $field = 'id')
Simple Usage:
Standalone Usage:
doctrine_unique
Validates that a given value is unique and doesn't exist in a table in a field.
Definition: class Krak\Validation\Validators\Doctrine\Unique($table_name, $field = 'id')
Simple Usage:
Standalone Usage:
doctrine_unique_entity
Validates that a given value exists in a table in a certain field.
Definition: class Krak\Validation\Validators\Doctrine\UniqueEntity($entity_name, $field = 'id')
Simple Usage:
Standalone Usage:
API
All of the following are defined in the Krak\Validation\Validators
namespace.
collection($validators, $err_on_extra = true)
Validates a map of validators with attribute names mapping to other validators. $err_on_extra
is a flag that will determine whether or not to validate if extra fields are in the input array.
This function will return either a Violation, ViolationCollection, or null depending on the input value.
toSize($value)
Gets the size of a variable. If string, it returns the string length. If array, it returns the count, else it assumes numeric and returns the value itself.
All versions of validation with dependencies
krak/invoke Version ^0.1.0
nikic/iter Version ^1.4
peridot-php/leo Version ^1.6