Introduction
EasyJsonValidator - simple Json validator for PHP.
Very faster, easy to configuration.
This validator work without JsonSchema(http://json-schema.org/)., (*1)
Capability
- Simple type check, like field
name - string
- Range check, like filed
number - form 1 to 10, or field date -> from 2019-01-01 to 2019-02-01
- Require check, like field
id is require
- Nested objects check, like multidimensional array with all available validation
- DateTime check in any date format
- Check by pattern (preg_match), like field
email - /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i
- Check by callback, like field
email - function($email) { return filter_var($email, FILTER_VALIDATE_EMAIL); }
- Customisation error message for any rule
Installation
composer require powar/json-validator
Usage
get any json, (*2)
$json = json_encode(['id' => 11, 'name' => 'JohnDoe', 'balance' => null, 'date' => date(DateTime::ISO8601)]);
create rules, (*3)
$rules = [
'id' => [
Validator::KEY_TYPE => Validator::TYPE_INTEGER,
Validator::KEY_MAX_VAL => 10,
],
'name' => Validator::TYPE_STRING,
'balance' => Validator::TYPE_NULL,
'date' => Validator::TYPE_DATETIME,
];
validate, (*4)
$validator = new \Powar\JsonValidator\Validator();
$validator->validate($json, $rules);
check errors exist, (*5)
if ($validator->hasErrors()) {
print_r($validator->getErrors());
}
array(1) {
'id' =>
string(20) "must be less than 10"
}
Documentation
Make rules, (*6)
The validate method takes a second parameter with array of rules for validation, (*7)
simple example:, (*8)
$rules = ['id' => 'int']
$validator->validate($json, $rules);
This rule will only check the incoming json for matching the id:integer field., (*9)
Note, you can write explicitly 'int' or another type, but I recommend using Validator::TYPE* constants., (*10)
example with constant:, (*11)
$rules = ['id' => Validator::TYPE_INTEGER];
List of constants for rules:, (*12)
| type |
constant |
available configuration |
| string |
Validator::TYPE_STRING |
KEY_REQUIRE, KEY_MAX_STR, KEY_MIN_STR, KEY_PATTERN, KEY_CALLBACK, KEY_LABEL |
| int |
Validator::TYPE_INTEGER |
KEY_REQUIRE, KEY_MAX_STR, KEY_MIN_STR, KEY_MIN_VAL, KEY_MAX_VAL,KEY_LABEL |
| number |
Validator::TYPE_NUMBER |
KEY_REQUIRE, KEY_MAX_VAL, KEY_MIN_VAL, KEY_LABEL |
| bool |
Validator::TYPE_BOOLEAN |
KEY_REQUIRE, KEY_BOOL_VAL, KEY_LABEL |
| array |
Validator::TYPE_ANY |
KEY_REQUIRE, KEY_LABEL |
| any |
Validator::TYPE_BOOLEAN |
KEY_REQUIRE, KEY_CALLBACK, KEY_LABEL |
| datetime |
Validator::TYPE_DATETIME |
KEY_REQUIRE, KEY_MIN_VAL, KEY_MAX_VAL, KEY_FORMAT, KEY_LABEL |
| null |
Validator::TYPE_NULL |
KEY_REQUIRE, KEY_LABEL |
example with configure:, (*13)
$rules = ['id' => [
Validator::KEY_TYPE => Validator::TYPE_INTEGER,
Validator::KEY_MAX_VAL => 10,
]
];
Note that now id contains an array where the required key is Validator :: KEY_TYPE, which must contain the type as in the simple case, (*14)
List of constants for configure:, (*15)
| constant |
string |
reqire |
description |
| Validator::KEY_TYPE |
type |
+ |
required key if you use array with configuration rule |
| Validator::KEY_LABEL |
label |
- |
for customization error message |
| Validator::KEY_REQUIRE |
require |
- |
just checks whether json contains element. Default true |
| Validator::KEY_MIN_STR |
min-str |
- |
Minimum number of characters (php strlen) |
| Validator::KEY_MAX_STR |
max-str |
- |
Maximum number of characters (php strlen) |
| Validator::KEY_MIN_VAL |
min-val |
- |
Minimum value (php <) |
| Validator::KEY_MAX_VAL |
max-val |
- |
Maximum value (php >) |
| Validator::KEY_PATTERN |
pattern |
- |
check by regexp (php preg_match) |
| Validator::KEY_FORMAT |
format |
- |
create date object from json, by format (php DateTime::createFromFormat) |
| Validator::KEY_BOOL |
format |
- |
create date object from json, by format (php DateTime::createFromFormat) |
| Validator::KEY_CALLBACK |
callback |
- |
check by callback (php is_callable -> call) |
| Validator::KEY_RULE |
rule |
- |
include rule |
Some examples
You can use existence rule if you json contains included object, (*16)
$someRule = [
'some_field' => [
Validator::KEY_TYPE => Validator::TYPE_INTEGER,
Validator::KEY_MAX_VAL => 10,
Validator::KEY_MAX_STR => 2,
],
'some_date' => [
Validator::KEY_TYPE => Validator::TYPE_DATETIME,
Validator::KEY_FORMAT => 'Y-m-d\TH:i:sP',
Validator::KEY_REQUIRE => false,
]
];
$rules = [
'some_id' => Validator::TYPE_INTEGER,
'some_clildren_element' => [
Validator::KEY_TYPE => Validator::TYPE_ARRAY,
Validator::KEY_RULE => $someRule,
]
];
Or you can create some small rules like this:, (*17)
$idRule = [
Validator::KEY_TYPE => Validator::TYPE_INTEGER,
Validator::KEY_MIN_STR => 1,
Validator::KEY_MAX_STR => 11,
];
$dateRule = [
Validator::KEY_TYPE => Validator::TYPE_DATETIME,
Validator::KEY_FORMAT => DateTime::ISO8601,
Validator::KEY_REQUIRE => false,
];
And use it for any rules:, (*18)
$rules = [
'some_id' => $idRule,
'some_date' => $dateRule,
];
NOTE! In case when you create rule for TYPE_DATETIME, you need be careful, because KEY_MIN_VAL and KEY_MAX_VAL can work is not obvious, (*19)
For Example, you need to create check for day of birth (Over 18 years of age, you know ;) ), (*20)
$minDate = new DateTime();
$minDate->modify('-18 year');
$format = 'Y-m-d';
$rule = [
'day_of_birth' => [
Validator::KEY_TYPE => Validator::TYPE_DATETIME,
Validator::KEY_FORMAT => $format,
Validator::KEY_MIN_VAL => $minDate->format($format), //<< WRONG
]
];
Should be with Validator::KEY_MAX_VAL :, (*21)
$minDate = new DateTime();
$minDate->modify('-18 year');
$format = 'Y-m-d';
$rule = [
'day_of_birth' => [
Validator::KEY_TYPE => Validator::TYPE_DATETIME,
Validator::KEY_FORMAT => $format,
Validator::KEY_MAX_VAL => $minDate->format($format),
]
];
Callback example, (*22)
$rule = [
'email' => [
Validator::KEY_TYPE => Validator::TYPE_STRING,
Validator::KEY_CALLBACK => function($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
]
];
Custom error message, (*23)
$someRule = [
'some_field' => [
Validator::KEY_TYPE => Validator::TYPE_INTEGER,
Validator::KEY_MAX_VAL => 10,
Validator::KEY_MAX_STR => 2,
Validator::KEY_LABEL => 'validator.error.some_field',
],
];
getErrors() -> ['some_field' => 'validator.error.some_field'], (*24)
More examples you can see in unit tests, (*25)