Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage multiple JSON schema files?

I'm trying to validate my JSON API using node.js + json-schema.js from commonjs-utils. Just single validation was easy but could not find right way how to manage multiple schema files to enable referencing each other.

Suppose that I got two Models & two APIs.

// book {   "type": "object",   "properties": {       "title": { "type": "string" },       "author": { "type": "string" }   } } // author {   "type": "object",   "properties": {       "first_name": { "type": "string" },       "last_name": { "type": "string" }   } }   // authors API {   "type": "array",   "items": { "$ref": "author" } } // books API: list of books written by same author {   "type": "object",   "properties": {     "author": { "$ref": "author" }      "books": { "type": "array", "items": { "$ref": "book" } }   } }   

Each schema should be divided in separate file and be online? Or Can I combine into single schema file like below? If it is possible, how can I reference local schema?

// single schema file {     "book": { ... },     "author": { ... },     "authors": { ... },     "books": { ... } } 
like image 978
Ray Yun Avatar asked Nov 18 '11 07:11

Ray Yun


People also ask

How do you reference a JSON Schema in another JSON Schema?

A schema can reference another schema using the $ref keyword. The value of $ref is a URI-reference that is resolved against the schema's Base URI. When evaluating a $ref , an implementation uses the resolved identifier to retrieve the referenced schema and applies that schema to the instance.

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 a JSON file reference another JSON file?

JSON Reference allows a JSON value to reference another value in a JSON document. This module implements utilities for exploring these objects.

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

In JSON Schemas, you can either put a schema per file and then access them using their URL (where you stored them), or a big schema with id tags.

Here is for one big file:

{     "id": "#root",     "properties": {         "author": {             "id": "#author",             "properties": {                 "first_name": {                     "type": "string"                 },                 "last_name": {                     "type": "string"                 }             },             "type": "object"         },         // author         "author_api": {             "id": "#author_api",             "items": {                 "$ref": "author"             },             "type": "array"         },         // authors API         "book": {             "id": "#book",             "properties": {                 "author": {                     "type": "string"                 },                 "title": {                     "type": "string"                 }             },             "type": "object"         },         // books API: list of books written by same author         "books_api": {             "id": "#books_api",             "properties": {                 "author": {                     "$ref": "author"                 },                 "books": {                     "items": {                         "$ref": "book"                     },                     "type": "array"                 }             },             "type": "object"         }     } } 

You can then reference your validator to one of those sub schemas (which are defined with an id).

From outside of your schema, this:

{ "$ref": "url://to/your/schema#root/properties/book" } 

is equivalent to this:

{ "$ref": "url://to/your/schema#book" } 

… which is equivalent, from inside, to this:

{ "$ref": "#root/properties/book" } 

or this (still from inside):

{ "$ref": "#book" } 

See my answer here for more information.

like image 101
Flavien Volken Avatar answered Sep 28 '22 07:09

Flavien Volken