Download the PHP package ava239/validator without Composer
On this page you can find all versions of the php package ava239/validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package validator
Validator library
Inspired by yup (JavaScript library)
Can validate strings, numbers, arrays by shape.
Also supports custom validators.
Basic usage example:
More examples in tests
Factory object
This will return factory object. It`s basic for your later usage and only one instance is really needed (but you can create as many as you wish).
It can produce concrete validator instances which implement interface and supposed to validate single variable of defined type.
Types implemented at this moment:
- string (created by method)
- number (created by method)
- array (created by method)
This object also responsible for adding custom validators to concrete validation objects created by it.
Use to add it, where:
- is one of ,,. defines data type where validator will be available
- is validator name. you will call it by that name
- is closure with at least one parameter which returns boolean. First parameter passed to function will be thing we are validating
Running custom validations
Custom validations called after adding custom validator to factory object.
Full example:
You call function on concrete validator with such params:
- validator name
- any number of additional params (optional)
List of included validators by data type
String
-
- tests if string contains $substring
-
- tests if string length is at least $length
-
- tests if string length does not exceed $length
-
- tests if string is not empty
Number
- tests if string is not empty
-
- tests if number is positive (> 0)
-
- tests if number is between $min and $max
-
- tests if value is a number (without this null is also valid value)
Array
- tests if value is a number (without this null is also valid value)
-
- tests if array contains at least $length elements
-
- tests if value is an array (without this null is also valid value)
-
- tests if array is described by a $shape, where $shape is associative array where values are validator objects created by a .
This method returns true only if every sub-validator is valid.
Allows any reasonable level of nesting (you can put array with shape validation as a key for shape validation).
- tests if array is described by a $shape, where $shape is associative array where values are validator objects created by a .
Error messages
As of version 1.1.0 you can add error messages for each validation.
To get error list there was method added which will act as with a difference it will return validator object, so you can chain method.
Let's get to example:
Here we define shape with error messages for most of the validations.
You can define error message as last parameter of any validation function.
So if it is validation with params you call it like that:
You also can define parameter name to use it in error messages. To do so just add as parameter on validator creation call (, or ).
It can be accessed as in any validation message:
If error occurs message will be parsed to "test msg test name"
Error array structure
It is flat associative array (no matter how much nesting you used) .
Keys have such structure:
- validation name for any basic validation
- for shape validation
- nested keys represented by dot notation.
Keys for example above:
Notice above how data type errors handled.
It is expected what you convert error messages structure as you wish after you get them.