Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override inherited json schema

Tags:

jsonschema

how can I override the validation rules which are defined in a json schema which is inherited by the "allOf" keyword?

Example:

{
  "$schema": "http://json-schema.org/draft-06/schema",
  "title": "My JSON Schema",
  "description": "",
  "definitions": {
    "a": {
      "type": "object",
      "properties": {
        "b": {
          "type": "object",
          "properties": {
            "c": {
              "type": "string",
              "minLength": 1,
              "maxLength": 100
            }
          },
          "required": [
            "c"
          ]
        }
      },
      "required": [
        "b"
      ]
    }
  },
  "properties": {
    "main": {
      "type": "object",
      "allOf": [
        {
          "$ref": "#/definitions/a"
        }
      ]
    },
    "sub": {
      "type": "object",
      "allOf": [
        {
          "$ref": "#/definitions/a"
        }
      ]
    }
  }
}

The json schema defines two objects:

  • main
  • sub

Both objects inherit their properties from the defined object "a" But the object "sub" should have other validation rules for property b.c (currently it is minLength 1 and maxLength 100).

So of course following json is invalid:

{
  "main" :{
    "b": {
      "c": "This property has a min length"
    }
  },"sub" : {
    "b": {
      "c": ""
    }
  }
}

How can I override the validation rules for property b.c?

like image 773
Andreas G. Avatar asked Jul 11 '18 08:07

Andreas G.


People also ask

Does JSON Schema support inheritance?

Because there is no such thing as schema inheritance currently defined. When using allOf , you require that all schemas in allOf match; and if you are strict about what can exist in this or that JSON, you'll have added additionalProperties to false . As such, you cannot inherit.

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.

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 $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

There is currently no way to do this perscribed by the JSON Schema specification.

like image 157
Relequestual Avatar answered Sep 29 '22 19:09

Relequestual