dev-master
9999999-devA little library for checking JSON object via rules
MIT
The Requires
- php >=5.3.0
Wallogit.com
2017 © Pedro Peláez
A little library for checking JSON object via rules
This is small library for validation JSON objects in PHP (via regex). You can describe required attributes and check it on requred values., (*2)
Please update your composer.json. Add this to your require section, (*3)
"krydos/json-rules-checker":"dev-master"
Very often I have a code like this:, (*4)
try {
$json = json_decode(getJsonStringFromAnyPlace());
if(isset($json->attr1)) {
// do something with $json->attr1
if(isset($json->attr2)) {
//do something with $json->attr2
}
else {
throw new Exception('attr2 required');
}
}
else {
throw new Exception('attr1 required');
}
}
catch(Exception $e) {
// do something
}
I really don't like it. I wrote this library because I want to validate JSON using some rules., (*5)
Like that:, (*6)
try {
$json = json_decode(getJsonStringFromAnyPlace());
// let's imagine that JSON looks something like this:
// {"root":{"attr1":"value1", "attr2":"value2", "attr3":{"attr3_1":"123"}}}
// let's write a rules
$rules = array(
'root' => array(
'attr1' => '/^value1$/',
'attr2' => '', // if you don't care what value has this attribute
// then just use empty string
'attr3' => array(
'attr3_1'=>'/^\d+$/' // just use regex for value validating
)
)
);
$validation_result = \JSONRulesChecker\JSONChecker::checkJSON($json, $rules); // return true
if(!$validation_result) {
throw new Excpetion('Please check you params');
}
}
catch(Exception $e) {
//do something
}
I like it :), (*7)
\JSONRulesChecker\JSONChecker::checkJSON($json, $rules);
\JSONRulesChecker\JSONChecker::checkJSON($json, $rules, true);
[@Sbrilenko][1] - for regex idea, (*8)
Please feel free to Pull Requests, (*9)
<3, (*10)
A little library for checking JSON object via rules
MIT