Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsonschema extending and no additional properties

I am using jsonschema for validating entrytypes that describe how entries (of given types) are displayed. these entries can have pages and are separated into aspects.

Both pages and aspects can be conditioned and I want to reuse a basic schema, even tho the condition on aspects can have 2 other properties that page conditions don't have.

This is a general issue that I always bump into. I want to extend a schema, while being able to have "additionalProperties" set to false in all cases.

I also dont see a possibility to fix it with anyOf, allOf without duplicates.

Or should I rather let go of additionalProperties or accpet the duplicates?


  {
    "$comment": "page condition",
    "type": "object",
    "properties": {
      "condition": {
        "type": "object",
        "properties": {
          "aspect": {
            "type": "string"
          },
          "value": {
            "type": "string"
          },
          "compare": {
            "type": "string"
          }
        },
        "required": [
          "aspect",
          "value"
        ],
        "additionalProperties": false
      }
    }
  }

...
  {
    "$comment": "aspect condition",
    "type": "object",
    "properties": {
      "condition": {
        "type": "object",
        "properties": {
          "aspect": {
            "type": "string"
          },
          "value": {
            "type": "string"
          },
          "compare": {
            "type": "string"
          },
          "disabled_text": {
            "type": "string"
          },
          "default_pass": {
            "type": "boolean"
          }
        },
        "required": [
          "aspect",
          "value"
        ],
        "additionalProperties": false
      }
    }
  }
like image 984
ramin Avatar asked Nov 09 '19 22:11

ramin


People also ask

What does Exclusiveminimum property in JSON Schema mean?

Gets or sets a flag indicating whether the value can not equal the number defined by the minimum attribute (Minimum).

What is additional properties in JSON?

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.

Can you extend JSON Schema?

With the package ajv-merge-patch you can use the keywords $merge and $patch that allow extending JSON Schemas with patches using formats JSON Merge Patch (RFC 7396) and JSON Patch (RFC 6902) .

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.


1 Answers

Unfortunatly there's no way round this with draft-7 JSON Schema.

You need to remove additionalProperties: false from any schema you wish to reference.

One approach to minimise duplication is, in your referring schema, re-define the properties, but just with a value of true. This means the validation part still happens in the referenced schemas themselves.

I showed this as an example problem to fix in a recent talk from this slide: https://stoic-agnesi-d0ac4a.netlify.com/32

A part of the resulting schema:

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "definitions": {
    "geneticsPatient": {
      "type": [
        "object"
      ]
    },
    "regularPatient": {
      "type": [
        "object"
      ]
    }
  },
  "properties": {
    "patient": {
      "additionalProperties": false,
      "properties": {
        "name": true,
        "phenotypicFeatures": true,
        "genomicFeatures": true
      },
      "allOf": [
        {
          "$ref": "#/definitions/regularPatient"
        },
        {
          "$ref": "#/definitions/geneticsPatient"
        }
      ]
    }
  }
}

In draft 2019-09, which is only recently released, we added a new keyword, although you still need to not define additionalProperties: false in the referenced schema.

You can find out more about that from some of my other slides: https://speakerdeck.com/relequestual/json-schema-draft-8-to-vocabularies-and-beyond

like image 136
Relequestual Avatar answered Oct 10 '22 19:10

Relequestual