Merge branch 'master' into ref

This commit is contained in:
Tyler Akins 2013-02-20 13:16:07 -06:00
commit 183d1558aa
6 changed files with 134 additions and 5 deletions

99
bin/validate-json Executable file
View 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);
}

View File

@ -5,7 +5,6 @@
"homepage": "https://github.com/justinrainbow/json-schema",
"type": "library",
"license": "BSD-3-Clause",
"version": "1.2.1",
"authors": [
{
"name": "Bruno Prieto Reis",
@ -29,5 +28,6 @@
},
"autoload": {
"psr-0": { "JsonSchema": "src/" }
}
},
"bin": ["bin/validate-json"]
}

View File

@ -40,8 +40,11 @@ class Format extends Constraint
break;
case 'date-time':
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)));
if (!$this->validateDateTime($element, 'Y-m-d\TH:i:s\Z') &&
!$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;
@ -155,4 +158,4 @@ class Format extends Constraint
{
return preg_match('/^[_a-z]+\.([_a-z]+\.?)+$/i', $host);
}
}
}

View File

@ -73,6 +73,8 @@ class FormatTest extends BaseTestCase
array('23:59:59', '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'),
@ -127,6 +129,8 @@ class FormatTest extends BaseTestCase
array('25:00:00', '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(PHP_INT_MAX, 'utc-millisec'),

View File

@ -52,6 +52,17 @@ class NumberAndIntegerTypesTest extends BaseTestCase
"number":{"type":"number"}
}
}'
),
array(
'{
"number": "1.4"
}',
'{
"type":"object",
"properties":{
"number":{"type":"number"}
}
}'
)
);
}

View File

@ -75,6 +75,18 @@ class PhpTypeCastModeTest extends BaseTestCase
}',
Validator::CHECK_MODE_TYPE_CAST
),
array(
'{
"a":"1.337"
}',
'{
"type":"object",
"properties":{
"a":{"type":"number","maximum":8.0}
}
}',
Validator::CHECK_MODE_TYPE_CAST
),
array(
'{
"a":"9e42"