Skip to content

Commit

Permalink
Backports for 5.2.0 (#368)
Browse files Browse the repository at this point in the history
* Add URI translation for retrieval & add local copies of spec schema

* Add use line for InvalidArgumentException & adjust scope (#372)

Fixes issue #371

* add quiet option (#382)

* add quiet option

* use verbose instead of quiet

* add quiet option

* always output dump-schema

* always output dump-schema-url

* fix typo and ws

* [BUGFIX] Add provided schema under a dummy / internal URI (fixes #376) (#378)

* Add provided schema under a dummy / internal URI (fixes #376)

In order to resolve internal $ref references within a user-provided
schema, SchemaStorage needs to know about the schema. As user-supplied
schemas do not have an associated URI, use a dummy / internal one instead.

* Remove dangling use

* Change URI to class constant on SchemaStorage

* Add option to disable validation of "format" constraint (#383)

* Add more unit tests (#366)

* Add test coverage for coercion API

* Complete test coverage for SchemaStorage

* Add test coverage for ObjectIterator

* Add exception test for JsonPointer

* MabeEnum\Enum appears to use singletons - add testing const

* Don't check this line for coverage

mbstring is on all test platforms, so this line will never be reached.

* Add test for TypeConstraint::validateTypeNameWording()

* Add test for exception on TypeConstraint::validateType()

* PHPunit doesn't like an explanation with its @codeCoverageIgnore...

* Add various tests for UriRetriever

* Add tests for FileGetContents

* Add tests for JsonSchema\Uri\Retrievers\Curl

* Add missing bad-syntax test file

* Restrict ignore to the exception line only

* Fix exception scope

* Allow the schema to be an associative array (#389)

* Allow the schema to be an associative array

Implements #388.

* Use json_decode(json_encode()) for array -> object cast

* Skip exception check on PHP versions < 5.5.0

* Skip test on HHVM, as it's happy to encode resources

* Enable FILTER_FLAG_EMAIL_UNICODE for email format if present (#398)

* Don't throw exceptions until after checking anyOf / oneOf (#394)

Fixes #393

* Fix infinite recursion on some schemas when setting defaults (#359) (#365)

* Don't try to fetch files that don't exist

Throws an exception when the ref can't be resolved to a useful file URI,
rather than waiting for something further down the line to fail after
the fact.

* Refactor defaults code to use LooseTypeCheck where appropriate

* Test for not treating non-containers like arrays

* Update comments

* Rename variable for clarity

* Add CHECK_MODE_ONLY_REQUIRED_DEFAULTS

If CHECK_MODE_ONLY_REQUIRED_DEFAULTS is set, then only apply defaults
if they are marked as required.

* Workaround for $this scope issue on PHP-5.3

* Fix infinite recursion via $ref when applying defaults

* Add missing second test for array case

* Add test for setting a default value for null

* Also fix infinite recursion via $ref for array defaults

* Move nested closure into separate method

* $parentSchema will always be set when $name is, so don't check it

* Handle nulls properly - fixes issue #377

* Add option to also validate the schema (#357)

* Remove stale files from #357 (obviated by #362) (#400)

* Stop #386 sneaking in alongside another PR backport
  • Loading branch information
erayd authored and bighappyface committed Mar 22, 2017
1 parent ef3ee83 commit e3c9bcc
Show file tree
Hide file tree
Showing 68 changed files with 1,742 additions and 601 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ third argument to `Validator::validate()`, or can be provided as the third argum
| `Constraint::CHECK_MODE_TYPE_CAST` | Enable fuzzy type checking for associative arrays and objects |
| `Constraint::CHECK_MODE_COERCE_TYPES` | Convert data types to match the schema where possible |
| `Constraint::CHECK_MODE_APPLY_DEFAULTS` | Apply default values from the schema if not set |
| `Constraint::CHECK_MODE_ONLY_REQUIRED_DEFAULTS` | When applying defaults, only set values that are required |
| `Constraint::CHECK_MODE_EXCEPTIONS` | Throw an exception immediately if validation fails |
| `Constraint::CHECK_MODE_DISABLE_FORMAT` | Do not validate "format" constraints |
| `Constraint::CHECK_MODE_VALIDATE_SCHEMA` | Validate the schema as well as the provided document |

Please note that using `Constraint::CHECK_MODE_COERCE_TYPES` or `Constraint::CHECK_MODE_APPLY_DEFAULTS`
will modify your original data.
Expand Down
124 changes: 69 additions & 55 deletions bin/validate-json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ function __autoload($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
Expand All @@ -29,6 +28,49 @@ function __autoload($className)
}
}

// support running this tool from git checkout
if (is_dir(__DIR__ . '/../src/JsonSchema')) {
set_include_path(__DIR__ . '/../src' . PATH_SEPARATOR . get_include_path());
}

$arOptions = array();
$arArgs = array();
array_shift($argv);//script itself
foreach ($argv as $arg) {
if ($arg{0} == '-') {
$arOptions[$arg] = true;
} else {
$arArgs[] = $arg;
}
}

if (count($arArgs) == 0
|| isset($arOptions['--help']) || isset($arOptions['-h'])
) {
echo <<<HLP
Validate schema
Usage: validate-json data.json
or: validate-json data.json schema.json
Options:
--dump-schema Output full schema and exit
--dump-schema-url Output URL of schema
--verbose Show additional output
--quiet Suppress all output
-h --help Show this help
HLP;
exit(1);
}

if (count($arArgs) == 1) {
$pathData = $arArgs[0];
$pathSchema = null;
} else {
$pathData = $arArgs[0];
$pathSchema = getUrlFromPath($arArgs[1]);
}

/**
* Show the json parse error that happened last
*
Expand All @@ -44,7 +86,7 @@ function showJsonError()
}
}

echo 'JSON parse error: ' . $json_errors[json_last_error()] . "\n";
output('JSON parse error: ' . $json_errors[json_last_error()] . "\n");
}

function getUrlFromPath($path)
Expand Down Expand Up @@ -84,48 +126,18 @@ function parseHeaderValue($headerValue)
return $arData;
}


// support running this tool from git checkout
if (is_dir(__DIR__ . '/../src/JsonSchema')) {
set_include_path(__DIR__ . '/../src' . PATH_SEPARATOR . get_include_path());
}

$arOptions = array();
$arArgs = array();
array_shift($argv);//script itself
foreach ($argv as $arg) {
if ($arg{0} == '-') {
$arOptions[$arg] = true;
} else {
$arArgs[] = $arg;
/**
* Send a string to the output stream, but only if --quiet is not enabled
*
* @param $str A string output
*/
function output($str) {
global $arOptions;
if (!isset($arOptions['--quiet'])) {
echo $str;
}
}

if (count($arArgs) == 0
|| isset($arOptions['--help']) || isset($arOptions['-h'])
) {
echo <<<HLP
Validate schema
Usage: validate-json data.json
or: validate-json data.json schema.json
Options:
--dump-schema Output full schema and exit
--dump-schema-url Output URL of schema
-h --help Show this help
HLP;
exit(1);
}

if (count($arArgs) == 1) {
$pathData = $arArgs[0];
$pathSchema = null;
} else {
$pathData = $arArgs[0];
$pathSchema = getUrlFromPath($arArgs[1]);
}

$urlData = getUrlFromPath($pathData);

$context = stream_context_create(
Expand All @@ -141,14 +153,14 @@ $context = stream_context_create(
);
$dataString = file_get_contents($pathData, false, $context);
if ($dataString == '') {
echo "Data file is not readable or empty.\n";
output("Data file is not readable or empty.\n");
exit(3);
}

$data = json_decode($dataString);
unset($dataString);
if ($data === null) {
echo "Error loading JSON data file\n";
output("Error loading JSON data file\n");
showJsonError();
exit(5);
}
Expand Down Expand Up @@ -182,9 +194,9 @@ if ($pathSchema === null) {

//autodetect schema
if ($pathSchema === null) {
echo "JSON data must be an object and have a \$schema property.\n";
echo "You can pass the schema file on the command line as well.\n";
echo "Schema autodetection failed.\n";
output("JSON data must be an object and have a \$schema property.\n");
output("You can pass the schema file on the command line as well.\n");
output("Schema autodetection failed.\n");
exit(6);
}
}
Expand All @@ -202,9 +214,9 @@ try {
exit();
}
} catch (Exception $e) {
echo "Error loading JSON schema file\n";
echo $urlSchema . "\n";
echo $e->getMessage() . "\n";
output("Error loading JSON schema file\n");
output($urlSchema . "\n");
output($e->getMessage() . "\n");
exit(2);
}
$refResolver = new JsonSchema\SchemaStorage($retriever, $resolver);
Expand All @@ -221,17 +233,19 @@ try {
$validator->check($data, $schema);

if ($validator->isValid()) {
echo "OK. The supplied JSON validates against the schema.\n";
if(isset($arOptions['--verbose'])) {
output("OK. The supplied JSON validates against the schema.\n");
}
} else {
echo "JSON does not validate. Violations:\n";
output("JSON does not validate. Violations:\n");
foreach ($validator->getErrors() as $error) {
echo sprintf("[%s] %s\n", $error['property'], $error['message']);
output(sprintf("[%s] %s\n", $error['property'], $error['message']));
}
exit(23);
}
} catch (Exception $e) {
echo "JSON does not validate. Error:\n";
echo $e->getMessage() . "\n";
echo "Error code: " . $e->getCode() . "\n";
output("JSON does not validate. Error:\n");
output($e->getMessage() . "\n");
output("Error code: " . $e->getCode() . "\n");
exit(24);
}
174 changes: 174 additions & 0 deletions dist/schema/json-schema-draft-03.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
{
"$schema": "http://json-schema.org/draft-03/schema#",
"id": "http://json-schema.org/draft-03/schema#",
"type": "object",

"properties": {
"type": {
"type": [ "string", "array" ],
"items": {
"type": [ "string", { "$ref": "#" } ]
},
"uniqueItems": true,
"default": "any"
},

"properties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},

"patternProperties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},

"additionalProperties": {
"type": [ { "$ref": "#" }, "boolean" ],
"default": {}
},

"items": {
"type": [ { "$ref": "#" }, "array" ],
"items": { "$ref": "#" },
"default": {}
},

"additionalItems": {
"type": [ { "$ref": "#" }, "boolean" ],
"default": {}
},

"required": {
"type": "boolean",
"default": false
},

"dependencies": {
"type": "object",
"additionalProperties": {
"type": [ "string", "array", { "$ref": "#" } ],
"items": {
"type": "string"
}
},
"default": {}
},

"minimum": {
"type": "number"
},

"maximum": {
"type": "number"
},

"exclusiveMinimum": {
"type": "boolean",
"default": false
},

"exclusiveMaximum": {
"type": "boolean",
"default": false
},

"minItems": {
"type": "integer",
"minimum": 0,
"default": 0
},

"maxItems": {
"type": "integer",
"minimum": 0
},

"uniqueItems": {
"type": "boolean",
"default": false
},

"pattern": {
"type": "string",
"format": "regex"
},

"minLength": {
"type": "integer",
"minimum": 0,
"default": 0
},

"maxLength": {
"type": "integer"
},

"enum": {
"type": "array",
"minItems": 1,
"uniqueItems": true
},

"default": {
"type": "any"
},

"title": {
"type": "string"
},

"description": {
"type": "string"
},

"format": {
"type": "string"
},

"divisibleBy": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true,
"default": 1
},

"disallow": {
"type": [ "string", "array" ],
"items": {
"type": [ "string", { "$ref": "#" } ]
},
"uniqueItems": true
},

"extends": {
"type": [ { "$ref": "#" }, "array" ],
"items": { "$ref": "#" },
"default": {}
},

"id": {
"type": "string",
"format": "uri"
},

"$ref": {
"type": "string",
"format": "uri"
},

"$schema": {
"type": "string",
"format": "uri"
}
},

"dependencies": {
"exclusiveMinimum": "minimum",
"exclusiveMaximum": "maximum"
},

"default": {}
}
Loading

0 comments on commit e3c9bcc

Please sign in to comment.