PHP code example of rethink / json-validator
1. Go to this page and download the library: Download rethink/json-validator library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rethink / json-validator example snippets
$validator->defineType('User', [
'name' => 'string',
'gender' => 'string',
'age' => '?integer',
'rating' => '?integer|boolean',
]);
$validator->defineType('UserCollection', ['User']);
$validator->defineType('timestamp', function ($value) {
if ((!is_string($value) && !is_numeric($value)) || strtotime($value) === false) {
return false;
}
$date = date_parse($value);
return checkdate($date['month'], $date['day'], $date['year']);
});
use rethink\jsv\Validator;
$validator = new Validator();
// $validator->defineType(...) Add your custom type if necessary
$matched = $validator->matches($data, 'User');
if ($matched) {
// Validation passed
} else {
$errors = $validator->getErrors();
}
$data = [
'name' => 'Bob',
'gender' => 'Male',
'age' => 19,
'phone' => null, // This property is unnecessary
];
$matched = $validator->matches($data, 'User', true); // strict mode is turned on
var_dump($matched); // false is returned