Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive Self Referencing JSON-Schema

Tags:

jsonschema

Is it a valid json schema:

  object:
    $ref: '#/definitions/object'

Would you recommend to use such format?

like image 390
Rana Avatar asked Feb 07 '16 06:02

Rana


People also ask

How do you reference a 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.

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 is a recursive schema?

A recursive schema is one that has a reference to its own root, identified by the empty fragment URI reference ("#"). Simply stated, a "$recursiveRef" behaves identically to "$ref", except when its target schema contains "$recursiveAnchor" with a value of true.

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.


1 Answers

Self references are allowed and useful. However, your example looks like it would just be a referential infinite loop. Here is an example of a JSON Schema that uses recursive references to define a tree structure of unlimited depth.

{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "tree": { "$ref": "#/definitions/tree" }
  },
  "definitions": {
    "tree": {
      "type": "object",
      "properties": {
        "value": { "type": "string" },
        "branches": {
          "type": "array",
          "items": { "$ref": "#/definitions/tree" },
          "minItems": 1
        }
      },
      "required": ["value"]
    }
  }
}
like image 142
Jason Desrosiers Avatar answered Sep 19 '22 02:09

Jason Desrosiers