Can someone tell me how to make the JustinRainbow Json schema validator be able to find references.
This is the schema of foobar I'm trying to validate:
{
"title": "foobar schema",
"type": "object",
"properties": {
"pagination": {
"$ref": "#/definitions/pagination"
}
},
"required": ["pagination"]
}
And the definition of the pagination schema is contained in a separate file on my computer.
Trying to validate that without telling the JSON validator how to resolve references like this:
$uriRetriever = new JsonSchema\Uri\UriRetriever();
$refResolver = new JsonSchema\RefResolver($uriRetriever, $uriResolver);
$schema = $refResolver->resolve("file://".realpath(__DIR__."/foobar.json"));
Gives an error message:
File: file://features/foobar.json is found, but could not resolve fragment: #/definitions/pagination (JsonSchema\Exception\UnresolvableJsonPointerException)
Which is fair enough as there is no way for the validator to know how to find the file that contains the pagination schema definition....so how can I tell the RefResolver how to find the definition of the pagination schema?
I would prefer to be able to resolve the file through the local filesystem, rather than having to use URL's on a webserver.
The kind of reference used in your schema is a json pointer referring to another part of your schema file. You need to specify definition/pagination properties to get rid of the error.
{
"title": "foobar schema",
"type": "object",
"properties": {
"pagination": {
"$ref": "#/definitions/pagination"
}
},
"required": ["pagination"],
"definitions": {
"pagination": {
}
}
}
In order to refer to a definition in an other file you can write something like:
"pagination": {
"$ref": "pagination.schema.json#"
}
Or even specify a particular node in the external file:
"pagination": {
"$ref": "external_definitions.schema.json#/definitions/pagination"
}
The external_definitions.schema.json
/ pagination.schema.json
should be looked for in the same folder as your schema containing the reference. The library should also provide some api to configure this lookup.
In my project I use JustinRainbow JsonValidator for php. It does not allow to provide a custom resolver, but the location where the referred files are search for can be influenced by "id" value in the schema.
Example, schema in main.schema.json
:
{
"id": "http://myweb.com/schemas/main.schema.json#",
"title": "foobar schema",
"type": "object",
"properties": {
"pagination": {
"$ref": "pagination.schema.json#"
}
},
"required": ["pagination"]
}
when validating this schema, so the validator tries to load the pagination.schema.json using:
php_get_contents('http://myweb.com/schemas/pagination.schema.json');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With