mirror of
https://github.com/justinrainbow/json-schema.git
synced 2025-05-05 22:15:27 +02:00
Merge branch 'master' into ref
This commit is contained in:
commit
183d1558aa
99
bin/validate-json
Executable file
99
bin/validate-json
Executable file
@ -0,0 +1,99 @@
|
|||||||
|
#!/usr/bin/env php
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* JSON schema validator
|
||||||
|
*
|
||||||
|
* @author Christian Weiske <christian.weiske@netresearch.de>
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dead simple autoloader
|
||||||
|
*
|
||||||
|
* @param string $className Name of class to load
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function __autoload($className)
|
||||||
|
{
|
||||||
|
$className = ltrim($className, '\\');
|
||||||
|
$fileName = '';
|
||||||
|
$namespace = '';
|
||||||
|
if ($lastNsPos = strrpos($className, '\\')) {
|
||||||
|
$namespace = substr($className, 0, $lastNsPos);
|
||||||
|
$className = substr($className, $lastNsPos + 1);
|
||||||
|
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
|
||||||
|
}
|
||||||
|
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
|
||||||
|
require_once $fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show the json parse error that happened last
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function showJsonError()
|
||||||
|
{
|
||||||
|
$constants = get_defined_constants(true);
|
||||||
|
$json_errors = array();
|
||||||
|
foreach ($constants['json'] as $name => $value) {
|
||||||
|
if (!strncmp($name, 'JSON_ERROR_', 11)) {
|
||||||
|
$json_errors[$value] = $name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
echo 'JSON parse error: ' . $json_errors[json_last_error()] . "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// support running this tool from git checkout
|
||||||
|
if (is_dir(__DIR__ . '/../src/JsonSchema')) {
|
||||||
|
set_include_path(__DIR__ . '/../src' . PATH_SEPARATOR . get_include_path());
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($argc < 3) {
|
||||||
|
echo "Usage: validate-json schema.json data.json\n";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$pathSchema = $argv[1];
|
||||||
|
$pathData = $argv[2];
|
||||||
|
|
||||||
|
if (!is_readable($pathSchema)) {
|
||||||
|
echo "Schema file is not readable.\n";
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_readable($pathData)) {
|
||||||
|
echo "Data file is not readable.\n";
|
||||||
|
exit(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = json_decode(file_get_contents($pathData));
|
||||||
|
|
||||||
|
if ($data === null) {
|
||||||
|
echo "Error loading JSON data file\n";
|
||||||
|
showJsonError();
|
||||||
|
exit(5);
|
||||||
|
}
|
||||||
|
|
||||||
|
$schema = json_decode(file_get_contents($pathSchema));
|
||||||
|
|
||||||
|
if ($schema === null) {
|
||||||
|
echo "Error loading JSON schema file\n";
|
||||||
|
showJsonError();
|
||||||
|
exit(6);
|
||||||
|
}
|
||||||
|
|
||||||
|
$validator = new JsonSchema\Validator();
|
||||||
|
$validator->check($data, $schema);
|
||||||
|
|
||||||
|
if ($validator->isValid()) {
|
||||||
|
echo "OK. The supplied JSON validates against the schema.\n";
|
||||||
|
} else {
|
||||||
|
echo "JSON does not validate. Violations:\n";
|
||||||
|
foreach ($validator->getErrors() as $error) {
|
||||||
|
echo sprintf("[%s] %s\n", $error['property'], $error['message']);
|
||||||
|
}
|
||||||
|
exit(23);
|
||||||
|
}
|
@ -5,7 +5,6 @@
|
|||||||
"homepage": "https://github.com/justinrainbow/json-schema",
|
"homepage": "https://github.com/justinrainbow/json-schema",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"license": "BSD-3-Clause",
|
"license": "BSD-3-Clause",
|
||||||
"version": "1.2.1",
|
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "Bruno Prieto Reis",
|
"name": "Bruno Prieto Reis",
|
||||||
@ -29,5 +28,6 @@
|
|||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-0": { "JsonSchema": "src/" }
|
"psr-0": { "JsonSchema": "src/" }
|
||||||
}
|
},
|
||||||
|
"bin": ["bin/validate-json"]
|
||||||
}
|
}
|
||||||
|
@ -40,8 +40,11 @@ class Format extends Constraint
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'date-time':
|
case 'date-time':
|
||||||
if (!$this->validateDateTime($element, 'Y-m-d\TH:i:s\Z')) {
|
if (!$this->validateDateTime($element, 'Y-m-d\TH:i:s\Z') &&
|
||||||
$this->addError($path, sprintf('Invalid date time %s, expected format YYYY-MM-DDTHH:MM:SSZ', json_encode($element)));
|
!$this->validateDateTime($element, 'Y-m-d\TH:i:sP') &&
|
||||||
|
!$this->validateDateTime($element, 'Y-m-d\TH:i:sO')
|
||||||
|
) {
|
||||||
|
$this->addError($path, sprintf('Invalid date time %s, expected format YYYY-MM-DDTHH:MM:SSZ or YYYY-MM-DDTHH:MM:SS+HH:MM', json_encode($element)));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -73,6 +73,8 @@ class FormatTest extends BaseTestCase
|
|||||||
array('23:59:59', 'time'),
|
array('23:59:59', 'time'),
|
||||||
|
|
||||||
array('2000-05-01T12:12:12Z', 'date-time'),
|
array('2000-05-01T12:12:12Z', 'date-time'),
|
||||||
|
array('2000-05-01T12:12:12+0100', 'date-time'),
|
||||||
|
array('2000-05-01T12:12:12+01:00', 'date-time'),
|
||||||
|
|
||||||
array('0', 'utc-millisec'),
|
array('0', 'utc-millisec'),
|
||||||
|
|
||||||
@ -127,6 +129,8 @@ class FormatTest extends BaseTestCase
|
|||||||
array('25:00:00', 'time'),
|
array('25:00:00', 'time'),
|
||||||
|
|
||||||
array('1999-1-11T00:00:00Z', 'date-time'),
|
array('1999-1-11T00:00:00Z', 'date-time'),
|
||||||
|
array('1999-01-11T00:00:00+100', 'date-time'),
|
||||||
|
array('1999-01-11T00:00:00+1:00', 'date-time'),
|
||||||
|
|
||||||
array('-1', 'utc-millisec'),
|
array('-1', 'utc-millisec'),
|
||||||
array(PHP_INT_MAX, 'utc-millisec'),
|
array(PHP_INT_MAX, 'utc-millisec'),
|
||||||
|
@ -52,6 +52,17 @@ class NumberAndIntegerTypesTest extends BaseTestCase
|
|||||||
"number":{"type":"number"}
|
"number":{"type":"number"}
|
||||||
}
|
}
|
||||||
}'
|
}'
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'{
|
||||||
|
"number": "1.4"
|
||||||
|
}',
|
||||||
|
'{
|
||||||
|
"type":"object",
|
||||||
|
"properties":{
|
||||||
|
"number":{"type":"number"}
|
||||||
|
}
|
||||||
|
}'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -75,6 +75,18 @@ class PhpTypeCastModeTest extends BaseTestCase
|
|||||||
}',
|
}',
|
||||||
Validator::CHECK_MODE_TYPE_CAST
|
Validator::CHECK_MODE_TYPE_CAST
|
||||||
),
|
),
|
||||||
|
array(
|
||||||
|
'{
|
||||||
|
"a":"1.337"
|
||||||
|
}',
|
||||||
|
'{
|
||||||
|
"type":"object",
|
||||||
|
"properties":{
|
||||||
|
"a":{"type":"number","maximum":8.0}
|
||||||
|
}
|
||||||
|
}',
|
||||||
|
Validator::CHECK_MODE_TYPE_CAST
|
||||||
|
),
|
||||||
array(
|
array(
|
||||||
'{
|
'{
|
||||||
"a":"9e42"
|
"a":"9e42"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user