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
}
}
}
Gets or sets a flag indicating whether the value can not equal the number defined by the minimum attribute (Minimum).
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.
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) .
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With