'merge two JSON schemas (extend a JSON schema)
I have a "parent" JSON schema that I would like to extend to get various "child" schemas. I'll have about 20 childs so it's important for me to find something consistant here.
Here's the parent (base schema):
{
"definitions":{
"selector":{
"$id": "#/definitions/selector",
"type": "object",
"title": "Selectors used to extract datas.",
"required": [],
"properties":{
"path":{
"type": "string",
"title": "Selector path",
"default": "",
"examples": []
},
"attr":{
"type": "string",
"title": "HTML attribute",
"default": "",
"examples": ["src"]
},
"regex":{
"type": "string",
"title": "Regex pattern",
"default": "",
"examples": []
},
"multiple":{
"type": "boolean",
"title": "Multiple values",
"default": false,
"readOnly": true
}
}
}
},
"type": "object",
"title": "Track importer schema",
"properties":{
"artist":{
"$ref": "#/definitions/selector",
"title": "Track artist"
},
"title":{
"$ref": "#/definitions/selector",
"title": "Track title"
},
"album":{
"$ref": "#/definitions/selector",
"title": "Track album"
}
}
}
Here's the child (based on the parent)
{
'properties':{
'artist':{
'properties':{
'path':{
'default':'.//artist',
'readOnly':true
}
}
},
'title':{
'properties':{
'path':{
'default':'.//title',
'readOnly':true
}
}
},
'album':{
'properties':{
'path':{
'default':'.//album',
'readOnly':true
}
}
},
'image':{
'properties':{
'path':{
'default':'.//albumart',
'readOnly':true
}
}
}
}
}
I would like to get a new schema that would be the sum of the parent and child schemas.
Actually, I've found a way to have this working in Ruby by deep merging the two JSON schemas objects.
But since I need to use the schemas in multiple langages (Ruby, Javascript, etc), I would like to know if there is a way to do this "natively" with the JSON schemas.
Is that possible and how ?
Thanks !
Solution 1:[1]
"allOf": [
{ "$ref": "schema1.json" },
{ "$ref": "schema2.json" }
]
There is also the $dynamicAnchor and $dynamicRef keywords, which let you augment particular definitions with new properties, but it isn't necessary in your case because all of your properties are at the top level of the object, so two $refs will work fine.
By the way, your $ids are invalid: identifiers cannot start with or include a fragment (at least not in any specification version that permits using $ref alongside other keywords, i.e. draft2019-09 or later).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ether |
