Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Liquid Studio: How to write a JSON schema $ref to another file

Tags:

I'm trying to refer to a JSON schema located in a different file using "$ref" with Liquid Studio 2017. Both the referring JSON schema and the referred JSON schema are located within the same directory.

I tried it using relative paths:

"$ref": "referredSchema.json/propertyName" 

and using absolute paths:

"$ref": "file:///C:/JSON/referredSchema.json/propertyName" "$ref": "file:///JSON/referredSchema.json/propertyName" "$ref": "file:///JSON/referredSchema.json#/propertyName" 

and a few other variations. None of them worked, I always get an error message "Invalid URI". Also the documentation only mentions that refs to other documents are possible without giving a reasonable example.

So I wonder, what the expected URI format would be.

like image 940
hounce Avatar asked Mar 24 '17 12:03

hounce


People also ask

What does $Ref mean in JSON schema?

In a JSON schema, a $ref keyword is a JSON Pointer to a schema, or a type or property in a schema. A JSON pointer takes the form of A # B in which: A is the relative path from the current schema to a target schema. If A is empty, the reference is to a type or property in the same schema, an in-schema reference.

Can JSON reference another JSON?

No, you can't do that, at least not directly. For a start, JSON doesn't support circular references. You might consider using a library such as jsonpath that allows you to reference elements by pattern, but you would tend to need apriori whether a value was an actual value or a reference to some other branch.

What is JSON schema additionalProperties?

The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword or match any of the regular expressions in the patternProperties keyword. By default any additional properties are allowed.

What is id in JSON schema?

$id is a reserved keyword. It serves for: Declaring an identifier for the schema or subschema. Declaring a base URL against which $ref URLs are resolved.


1 Answers

You can reference schemas defined in the local file or external files using the $ref property.

The issue you have is the fragment part (the bit after the #). This references a schema within the definitions property on the root schema.

The following example should show how to do this for local files and external files

Main.json

{     "$schema": "http://json-schema.org/draft-04/schema#",     "type": "object",     "additionalProperties": false,     "properties": {         "ReferenceToLocalSchema": {             "$ref": "#/definitions/LocalType"         },         "ReferenceToExternalSchema": {             "$ref": "Common.json#/definitions/ExternalType"         }     },     "definitions": {         "LocalType": {             "type": "object",             "additionalProperties": false,             "properties": {                 "no-write": {                     "type": "boolean",                     "default": false                 }             }         }     } } 

JSON Schema Diagram

Common.json

{     "$schema": "http://json-schema.org/draft-04/schema#",     "type": "object",     "additionalProperties": false,     "definitions": {         "ExternalType": {             "type": "object",             "additionalProperties": false,             "properties": {                 "src": {                     "type": "array",                     "items": {                         "type": "string",                         "minLength": 1                     }                 }             },             "required": [                 "src"             ]         }     } } 

JSON Schema Diagram

Notice the reference to the local schema

"$ref": "#/definitions/LocalType" 

and the remote schema

"$ref": "Common.json#/definitions/ExternalType" 

I've shown this with a relative url, but it could be a fully qualified url

"$ref": "file:///Common.json#/definitions/ExternalType" 

One thing to note. At the moment the list of possible options presented in the UI will only show the definitions that are defined in the local file. References to external files will have to be entered in the code view.

enter image description here

If you still have questions please add the schema to the question.

like image 56
Sprotty Avatar answered Sep 22 '22 08:09

Sprotty