Reputation: 968
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
}
}
}
Upvotes: 6
Views: 5711
Reputation: 12315
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
Upvotes: 7