Download the PHP package simplecomplex/validate without Composer
On this page you can find all versions of the php package simplecomplex/validate. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download simplecomplex/validate
More information about simplecomplex/validate
Files in simplecomplex/validate
Package validate
Short Description Validate shallowly and recursively (elements of object/array).
License MIT
Homepage https://github.com/simplecomplex/php-validate
Informations about the package validate
Validate
Validate almost any kind of PHP data structure.
Simple shallow validation
Test a variable against one of s 60+ rule methods
- is it of a particular type or class?
- does it's value match a pattern?
- is it emptyish?
- a container (array|object|traversable)?
- a numerically indexed array?
Test against a set of rules
A validation rule set is a list of rules ( methods)
– in effect you can combine all the above questions into a single questions.
You can also declare that "well if it isn't an [object|string|whatever], then null|false|0 will do just fine".
Validate objects/arrays recursively
Validation rule sets can be nested – indefinitely.
traverses the subject according to the rule set (+ descendant rule sets),
and checks that the subject at any level/position accords with the rules at that level/position.
Record reason(s) for validation failure
memorizes all wrongdoings along the way,
and returns a list of violations (including where detected, and what was wrong).
Example of use
SimpleComplex Http uses validation rule sets to check the body of HTTP (REST) responses.
Rule sets are defined in JSON (readable and portable), and cached as PHP s (using SimpleComplex Cache).
Non-rule flags
Multi-dimensional object/array rule sets support a little more than proper validation rules.
- optional: the bucket is allowed to be missing
- allowNull: the value is allowed to be null
- alternativeEnum: list of alternative (scalar|null) values that should make a subject pass,
even though another rule is violated - tableElements: the subject (an object|array) must contain these keys,
and for every key there's a sub rule set -
listItems: the subject (an object|array) must be a list consisting of a number of buckets,
which all have the same type/pattern/structureis great for defining "the bucket must be object or null".
and are allowed to co-exist.
Relevant if you have a – kind of malformed – object that may contain list items as well as non-list elements.
Data originating from XML may easily have that structure (XML stinks ;-)
tableElements sub flags
- (obj|arr) rulesByElements (required): table of rule sets by keys;
~ for every key there's a rule set - (bool) exclusive: the object|array must only contain the keys specified by
- (arr) whitelist: the object|array must – apart from – only contain these keys
- (arr) blacklist: the object|array must not contain these keys
listItems sub flags
- (obj|arr) listItems (required): a rule set that every list item must comply to
- (int) minOccur: there must be at least that many list items
- (int) maxOccur: guess what
Example
Requirements
- PHP >=7.0
- SimpleComplex Utils