PHP code example of nezarfadle / jsonschemavalidator
1. Go to this page and download the library: Download nezarfadle/jsonschemavalidator 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/ */
nezarfadle / jsonschemavalidator example snippets
use Json\Validation\JsonSchemaValidator;
use Json\Validation\Validators\ArrayValueValidator;
use Json\Validation\Validators\NumericValueValidator;
use Json\Validation\Validators\ObjectValidator;
use Json\Validation\Validators\StringValueValidator;
use Json\Validation\Validators\RequiredStringValueValidator;
// Exceptions
use Json\Validation\Exceptions\EmptySchemaException;
use Json\Validation\Exceptions\InvalidJsonPayloadException;
use Json\Validation\Exceptions\EmptyPayloadException;
use Json\Validation\Exceptions\NotExistsPropertyException;
$json = <<<EOL
{
"id" : 1
}
EOL;
$validator = new JsonSchemaValidator();
$schema = [
'id' => new NumericValueValidator() // validate whether the id is numeric or not
];
try {
$validator->validate( $json, $schema );
echo "Valid numeric value";
}
catch (EmptySchemaException $e) {}
catch (InvalidJsonPayloadException $e) {}
catch (EmptyPayloadException $e) {}
catch (\InvalidArgumentException $e) {}
catch (NotExistsPropertyException $e) {}