Json Schema Validation: Dependencies on subschema
I am new to json schema validation and I am having trouble validating a required field based on the existence and value of a field deeper in the json
Below is my current schema and an example json.
I need to have it say if "SW Large" exist and the value is true then it requires that "SW Words" higher exist within the Register object
The "anyOf" I have in my below schema was my attempt.
It works to say if "SW Large" is true then "SW Words" is required else not but if the field "SW Large" is not present it still will insist that "SW Words" is required.
How can I change the "anyOf" and "allOf" to check if "SW Large" actually exists?
Edit: Stripping back the json to be a bit more manageable
Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"additionalProperties": false,
"required": [
"Registers"
],
"properties": {
"Registers": {
"$id": "#/properties/Registers",
"type": "array",
"items": {
"$id": "#/properties/Registers/items",
"type": "object",
"additionalProperties": false,
"required": [
"Name",
"Address",
"Fields"
],
"anyOf": [
{
"allOf": [
{ "not": { "properties": { "Fields" : { "items": { "properties": { "SW Large": { "const": true } } } } } } }
]
},
{
"required": ["SW Words"]
}
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Name",
"type": "string"
},
"Address": {
"$id": "#/properties/Registers/items/properties/Address",
"type": "string"
},
"SW Words": {
"$id": "#/properties/Sections/items/properties/Blocks/items/properties/SW Words",
"type": "integer",
"default": 2,
"minimum": 2
},
"Fields": {
"$id": "#/properties/properties/Registers/items/properties/Fields",
"type": "array",
"items": {
"$id": "#/properties/Registers/items/properties/Fields/items",
"type": "object",
"additionalProperties": false,
"required": [
"Name"
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/Name",
"type": "string"
},
"SW Large": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/SW Large",
"type": "boolean"
}
}
}
}
}
}
}
}
}
Example Json
{
"Registers": [
{
"Name": "device",
"Address": "100",
"SW Words": 2,
"Fields": [
{
"Name" : "Product",
"SW Large" : true
},
{
"Name": "Version"
}
]
}
]
}
json jsonschema
add a comment |
I am new to json schema validation and I am having trouble validating a required field based on the existence and value of a field deeper in the json
Below is my current schema and an example json.
I need to have it say if "SW Large" exist and the value is true then it requires that "SW Words" higher exist within the Register object
The "anyOf" I have in my below schema was my attempt.
It works to say if "SW Large" is true then "SW Words" is required else not but if the field "SW Large" is not present it still will insist that "SW Words" is required.
How can I change the "anyOf" and "allOf" to check if "SW Large" actually exists?
Edit: Stripping back the json to be a bit more manageable
Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"additionalProperties": false,
"required": [
"Registers"
],
"properties": {
"Registers": {
"$id": "#/properties/Registers",
"type": "array",
"items": {
"$id": "#/properties/Registers/items",
"type": "object",
"additionalProperties": false,
"required": [
"Name",
"Address",
"Fields"
],
"anyOf": [
{
"allOf": [
{ "not": { "properties": { "Fields" : { "items": { "properties": { "SW Large": { "const": true } } } } } } }
]
},
{
"required": ["SW Words"]
}
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Name",
"type": "string"
},
"Address": {
"$id": "#/properties/Registers/items/properties/Address",
"type": "string"
},
"SW Words": {
"$id": "#/properties/Sections/items/properties/Blocks/items/properties/SW Words",
"type": "integer",
"default": 2,
"minimum": 2
},
"Fields": {
"$id": "#/properties/properties/Registers/items/properties/Fields",
"type": "array",
"items": {
"$id": "#/properties/Registers/items/properties/Fields/items",
"type": "object",
"additionalProperties": false,
"required": [
"Name"
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/Name",
"type": "string"
},
"SW Large": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/SW Large",
"type": "boolean"
}
}
}
}
}
}
}
}
}
Example Json
{
"Registers": [
{
"Name": "device",
"Address": "100",
"SW Words": 2,
"Fields": [
{
"Name" : "Product",
"SW Large" : true
},
{
"Name": "Version"
}
]
}
]
}
json jsonschema
add a comment |
I am new to json schema validation and I am having trouble validating a required field based on the existence and value of a field deeper in the json
Below is my current schema and an example json.
I need to have it say if "SW Large" exist and the value is true then it requires that "SW Words" higher exist within the Register object
The "anyOf" I have in my below schema was my attempt.
It works to say if "SW Large" is true then "SW Words" is required else not but if the field "SW Large" is not present it still will insist that "SW Words" is required.
How can I change the "anyOf" and "allOf" to check if "SW Large" actually exists?
Edit: Stripping back the json to be a bit more manageable
Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"additionalProperties": false,
"required": [
"Registers"
],
"properties": {
"Registers": {
"$id": "#/properties/Registers",
"type": "array",
"items": {
"$id": "#/properties/Registers/items",
"type": "object",
"additionalProperties": false,
"required": [
"Name",
"Address",
"Fields"
],
"anyOf": [
{
"allOf": [
{ "not": { "properties": { "Fields" : { "items": { "properties": { "SW Large": { "const": true } } } } } } }
]
},
{
"required": ["SW Words"]
}
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Name",
"type": "string"
},
"Address": {
"$id": "#/properties/Registers/items/properties/Address",
"type": "string"
},
"SW Words": {
"$id": "#/properties/Sections/items/properties/Blocks/items/properties/SW Words",
"type": "integer",
"default": 2,
"minimum": 2
},
"Fields": {
"$id": "#/properties/properties/Registers/items/properties/Fields",
"type": "array",
"items": {
"$id": "#/properties/Registers/items/properties/Fields/items",
"type": "object",
"additionalProperties": false,
"required": [
"Name"
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/Name",
"type": "string"
},
"SW Large": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/SW Large",
"type": "boolean"
}
}
}
}
}
}
}
}
}
Example Json
{
"Registers": [
{
"Name": "device",
"Address": "100",
"SW Words": 2,
"Fields": [
{
"Name" : "Product",
"SW Large" : true
},
{
"Name": "Version"
}
]
}
]
}
json jsonschema
I am new to json schema validation and I am having trouble validating a required field based on the existence and value of a field deeper in the json
Below is my current schema and an example json.
I need to have it say if "SW Large" exist and the value is true then it requires that "SW Words" higher exist within the Register object
The "anyOf" I have in my below schema was my attempt.
It works to say if "SW Large" is true then "SW Words" is required else not but if the field "SW Large" is not present it still will insist that "SW Words" is required.
How can I change the "anyOf" and "allOf" to check if "SW Large" actually exists?
Edit: Stripping back the json to be a bit more manageable
Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"additionalProperties": false,
"required": [
"Registers"
],
"properties": {
"Registers": {
"$id": "#/properties/Registers",
"type": "array",
"items": {
"$id": "#/properties/Registers/items",
"type": "object",
"additionalProperties": false,
"required": [
"Name",
"Address",
"Fields"
],
"anyOf": [
{
"allOf": [
{ "not": { "properties": { "Fields" : { "items": { "properties": { "SW Large": { "const": true } } } } } } }
]
},
{
"required": ["SW Words"]
}
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Name",
"type": "string"
},
"Address": {
"$id": "#/properties/Registers/items/properties/Address",
"type": "string"
},
"SW Words": {
"$id": "#/properties/Sections/items/properties/Blocks/items/properties/SW Words",
"type": "integer",
"default": 2,
"minimum": 2
},
"Fields": {
"$id": "#/properties/properties/Registers/items/properties/Fields",
"type": "array",
"items": {
"$id": "#/properties/Registers/items/properties/Fields/items",
"type": "object",
"additionalProperties": false,
"required": [
"Name"
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/Name",
"type": "string"
},
"SW Large": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/SW Large",
"type": "boolean"
}
}
}
}
}
}
}
}
}
Example Json
{
"Registers": [
{
"Name": "device",
"Address": "100",
"SW Words": 2,
"Fields": [
{
"Name" : "Product",
"SW Large" : true
},
{
"Name": "Version"
}
]
}
]
}
json jsonschema
json jsonschema
edited Nov 15 '18 at 11:11
Adam
asked Nov 15 '18 at 10:54
AdamAdam
32
32
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Yuk, upward dependencies can be really unpleaseant. Does the model have to be shaped that way?
Solution
You are on the right track. What you are missing is checking correctly if at least one element of "Fields" array had that "SW Large" : true and then shape the proper dependency.
Since draft-06 it is solved with "contains" keyword. In order to not repeat content, I'd reccommend to read following:
JSON Schema: How to check that an array contains at least one object with a property with a given value?
Json Schema: Require a property only when a specific property is present in a deep nested object (very educational!)
https://json-schema.org/understanding-json-schema/reference/array.html
Your schema re-worked below.
See the "definitions" section
First of all it's adding "contains" : { schema } to "Fields" definition. I need it as separate schema in order to use it as a condition in logical implication.
"definitions" : {
"Fields-contains-at-least-1-element-with-SW-Large-true" : {
"properties": {
"Fields" : {
"contains" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
}
},
}
},
If you would add it permanently, it might look like:
"Fields" : {
"type" : "array",
"contains" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
"items": {...},
}
which translates to "at least one item of "Fields" array must contain object with said property name and said value". Every JSON with "Fields" array that contains no item with "SW Large" : true would fail validation against such schema. And if you reverse the "contains" schema definition like:
"Fields" : {
"type" : "array",
"contains" : {
"not" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
}
"items": {...},
}
it would translate to "no item of "Fields" array can contain object with said property name and said value". Every JSON with "Fields" array that contains at least one item with "SW Large" : true would fail validation against such schema.
I don't want any of above to happen. I want to link "Fields/contains" condition with requiring or not requiring the "SW Words" property one level above, hence getting schema excluded to "definitions" section and making a proper use of it.
The "upward-dependency" is defined using that check and proper logical implication with "anyOf" keyword
It doesn't contain at least 1 item with "SW Large" : true OR "SW Words" is required
"definitions" : {
"upward-dependency" : {
"anyOf" : [
{ "not" : {"$ref" : "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true"} },
{ "required" : ["SW Words"] }
]
},
},
At the level of single item of "Registers" array I've added the "dependencies". Whenever "Fields" item appear in one of "Registers" items, it's been checked if it contains the unfortunate "SW Large" : true and if it does - the "SW Words" becomes required. Voila!
"items" : {
"$id": "#/properties/Registers/items
"type" : "object",
"properties" : {
"Fields": {
"$id": "#/properties/Registers/items/properties/Fields",
"type": "array",
"items": {
"$id": "#/properties/Registers/items/properties/Fields/items",
"type": "object",
"propertyNames" : {
"enum" : [
"Name",
"SW Large"
]
},
"required": [
"Name"
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/Name",
"type": "string"
},
"SW Large": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/SW Large",
"type": "boolean"
}
}
}
}
},
"dependencies" : {
"Fields" : { "$ref" : "#/definitions/upward-dependency" }
},
}
I've checked Schema with online validator and added your object to "examples" section.
Full schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"propertyNames" : {
"enum" : [
"Registers"
]
},
"required": [
"Registers"
],
"examples" : [
{
"Registers": [
{
"Name": "device",
"Address": "100",
"SW Words": 2,
"Fields": [
{
"Name" : "Product",
"SW Large" : true
},
{
"Name": "Version"
}
]
}
]
}
],
"properties": {
"Registers": {
"$id": "#/properties/Registers",
"type": "array",
"items": {
"$id": "#/properties/Registers/items",
"type": "object",
"propertyNames" : {
"enum" : [
"Name",
"Address",
"SW Words",
"Fields"
]
},
"required": [
"Name",
"Address",
"Fields"
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Name",
"type": "string"
},
"Address": {
"$id": "#/properties/Registers/items/properties/Address",
"type": "string"
},
"SW Words": {
"$id": "#/properties/Sections/items/properties/Blocks/items/properties/SW Words",
"type": "integer",
"default": 2,
"minimum": 2
},
"Fields": {
"$id": "#/properties/Registers/items/properties/Fields",
"type": "array",
"items": {
"$id": "#/properties/Registers/items/properties/Fields/items",
"type": "object",
"propertyNames" : {
"enum" : [
"Name",
"SW Large"
]
},
"required": [
"Name"
],
"properties": {
"Name": {
"type": "string"
},
"SW Large": {
"type": "boolean"
}
}
}
}
},
"dependencies" : {
"Fields" : { "$ref" : "#/definitions/upward-dependency" }
},
}
}
},
"definitions" : {
"upward-dependency" : {
"anyOf" : [
{ "not" : {"$ref" : "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true"} },
{ "required" : ["SW Words"] }
]
},
"Fields-contains-at-least-1-element-with-SW-Large-true" : {
"properties": {
"Fields" : {
"contains" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
}
},
}
},
}
Some side notes:
Instead of "additionalProperties":false please use "propertyNames". It's been specifically introduced for purpose of making sure only demanded properties are present in JSON object validated aginst schema.
"propertyNames" : {
"enum" : [
"Registers"
]
}
See: https://json-schema.org/understanding-json-schema/reference/object.html#property-names
It is very important to properly shape schemas/sub-schemas depending on which level these should be applied. Please note how "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true" properly reflects schema at "#/Registers/items" nesting level (since the "dependencies" is applied to an object on "#/Registers/items" level and one can think of it as of ""#/Registers/items/dependencies"). See https://json-schema.org/understanding-json-schema/reference/object.html#dependencies .
If you intend to validate single item of "Fields" array separately or even single item of "Registers" array separately, you might consider reshaping your schema to separate sub-schemas like I did with "questionA" in here: https://stackoverflow.com/a/53309856/2811843 . Thus - if necessity is there - you could easily exclude sub-schemas from main schema and just properly reference to them using "$ref" keyword. Some reading on JSON Pointer and relative JSON Pointer might be also useful for structuring complex schemas.
Worth to start at: https://json-schema.org/understanding-json-schema/structuring.html
I hope it helped.
You're a wizard, was wrecking my head with this. Thanks for the additional tips, still learning the basics!
– Adam
Nov 15 '18 at 17:26
Thank you, but the wizardry credit is exaggerated. You already had proper idea on how to solve it @Adam . I've edited answer by adding more exceptions from main schema to illustrate solution approach + two more side notes. I strongly recommend reading through stackoverflow.com/questions/48015822/… - it presents how to translate Boolean expressions into JSON Schema and what actually sits behind "contains" keyword. Since the answer is accepted, could I suggest up-voting it by clicking up arrow?
– PsychoFish
Nov 15 '18 at 19:37
I have tried to upvote but i get the message "Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." - So youre saying I should split up my schema to those definitions? A side note I can indeed validate this with the online validator but the python tools Ive tried all fail in different ways - very sad I may indeed need to think about restructuring - thanks for your help
– Adam
Nov 16 '18 at 10:00
Votes - oh, rep thing, nevermind. Validator: check which draft version python tools do support. Are you using perhaps one of validators enlisted here? json-schema.org/implementations.html#validators Restructuring schema might help, but please note I used "contains" keywords, which was introduced only since draft-06 and you might face the neccessity to change it in a way explained in recommended article. Moreover, I do not know how well python tools do handle "dependencies" -> it might go down to phrasing that specific one otherwise (by a separate subschemas and logical operators)
– PsychoFish
Nov 16 '18 at 11:41
I am using python jsonschema so it should support up to draft-7 but it is expecting "SW Words" to exist all the time. Shame I would have loved to have this automated within our existing python tools
– Adam
Nov 16 '18 at 12:41
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317843%2fjson-schema-validation-dependencies-on-subschema%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Yuk, upward dependencies can be really unpleaseant. Does the model have to be shaped that way?
Solution
You are on the right track. What you are missing is checking correctly if at least one element of "Fields" array had that "SW Large" : true and then shape the proper dependency.
Since draft-06 it is solved with "contains" keyword. In order to not repeat content, I'd reccommend to read following:
JSON Schema: How to check that an array contains at least one object with a property with a given value?
Json Schema: Require a property only when a specific property is present in a deep nested object (very educational!)
https://json-schema.org/understanding-json-schema/reference/array.html
Your schema re-worked below.
See the "definitions" section
First of all it's adding "contains" : { schema } to "Fields" definition. I need it as separate schema in order to use it as a condition in logical implication.
"definitions" : {
"Fields-contains-at-least-1-element-with-SW-Large-true" : {
"properties": {
"Fields" : {
"contains" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
}
},
}
},
If you would add it permanently, it might look like:
"Fields" : {
"type" : "array",
"contains" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
"items": {...},
}
which translates to "at least one item of "Fields" array must contain object with said property name and said value". Every JSON with "Fields" array that contains no item with "SW Large" : true would fail validation against such schema. And if you reverse the "contains" schema definition like:
"Fields" : {
"type" : "array",
"contains" : {
"not" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
}
"items": {...},
}
it would translate to "no item of "Fields" array can contain object with said property name and said value". Every JSON with "Fields" array that contains at least one item with "SW Large" : true would fail validation against such schema.
I don't want any of above to happen. I want to link "Fields/contains" condition with requiring or not requiring the "SW Words" property one level above, hence getting schema excluded to "definitions" section and making a proper use of it.
The "upward-dependency" is defined using that check and proper logical implication with "anyOf" keyword
It doesn't contain at least 1 item with "SW Large" : true OR "SW Words" is required
"definitions" : {
"upward-dependency" : {
"anyOf" : [
{ "not" : {"$ref" : "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true"} },
{ "required" : ["SW Words"] }
]
},
},
At the level of single item of "Registers" array I've added the "dependencies". Whenever "Fields" item appear in one of "Registers" items, it's been checked if it contains the unfortunate "SW Large" : true and if it does - the "SW Words" becomes required. Voila!
"items" : {
"$id": "#/properties/Registers/items
"type" : "object",
"properties" : {
"Fields": {
"$id": "#/properties/Registers/items/properties/Fields",
"type": "array",
"items": {
"$id": "#/properties/Registers/items/properties/Fields/items",
"type": "object",
"propertyNames" : {
"enum" : [
"Name",
"SW Large"
]
},
"required": [
"Name"
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/Name",
"type": "string"
},
"SW Large": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/SW Large",
"type": "boolean"
}
}
}
}
},
"dependencies" : {
"Fields" : { "$ref" : "#/definitions/upward-dependency" }
},
}
I've checked Schema with online validator and added your object to "examples" section.
Full schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"propertyNames" : {
"enum" : [
"Registers"
]
},
"required": [
"Registers"
],
"examples" : [
{
"Registers": [
{
"Name": "device",
"Address": "100",
"SW Words": 2,
"Fields": [
{
"Name" : "Product",
"SW Large" : true
},
{
"Name": "Version"
}
]
}
]
}
],
"properties": {
"Registers": {
"$id": "#/properties/Registers",
"type": "array",
"items": {
"$id": "#/properties/Registers/items",
"type": "object",
"propertyNames" : {
"enum" : [
"Name",
"Address",
"SW Words",
"Fields"
]
},
"required": [
"Name",
"Address",
"Fields"
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Name",
"type": "string"
},
"Address": {
"$id": "#/properties/Registers/items/properties/Address",
"type": "string"
},
"SW Words": {
"$id": "#/properties/Sections/items/properties/Blocks/items/properties/SW Words",
"type": "integer",
"default": 2,
"minimum": 2
},
"Fields": {
"$id": "#/properties/Registers/items/properties/Fields",
"type": "array",
"items": {
"$id": "#/properties/Registers/items/properties/Fields/items",
"type": "object",
"propertyNames" : {
"enum" : [
"Name",
"SW Large"
]
},
"required": [
"Name"
],
"properties": {
"Name": {
"type": "string"
},
"SW Large": {
"type": "boolean"
}
}
}
}
},
"dependencies" : {
"Fields" : { "$ref" : "#/definitions/upward-dependency" }
},
}
}
},
"definitions" : {
"upward-dependency" : {
"anyOf" : [
{ "not" : {"$ref" : "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true"} },
{ "required" : ["SW Words"] }
]
},
"Fields-contains-at-least-1-element-with-SW-Large-true" : {
"properties": {
"Fields" : {
"contains" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
}
},
}
},
}
Some side notes:
Instead of "additionalProperties":false please use "propertyNames". It's been specifically introduced for purpose of making sure only demanded properties are present in JSON object validated aginst schema.
"propertyNames" : {
"enum" : [
"Registers"
]
}
See: https://json-schema.org/understanding-json-schema/reference/object.html#property-names
It is very important to properly shape schemas/sub-schemas depending on which level these should be applied. Please note how "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true" properly reflects schema at "#/Registers/items" nesting level (since the "dependencies" is applied to an object on "#/Registers/items" level and one can think of it as of ""#/Registers/items/dependencies"). See https://json-schema.org/understanding-json-schema/reference/object.html#dependencies .
If you intend to validate single item of "Fields" array separately or even single item of "Registers" array separately, you might consider reshaping your schema to separate sub-schemas like I did with "questionA" in here: https://stackoverflow.com/a/53309856/2811843 . Thus - if necessity is there - you could easily exclude sub-schemas from main schema and just properly reference to them using "$ref" keyword. Some reading on JSON Pointer and relative JSON Pointer might be also useful for structuring complex schemas.
Worth to start at: https://json-schema.org/understanding-json-schema/structuring.html
I hope it helped.
You're a wizard, was wrecking my head with this. Thanks for the additional tips, still learning the basics!
– Adam
Nov 15 '18 at 17:26
Thank you, but the wizardry credit is exaggerated. You already had proper idea on how to solve it @Adam . I've edited answer by adding more exceptions from main schema to illustrate solution approach + two more side notes. I strongly recommend reading through stackoverflow.com/questions/48015822/… - it presents how to translate Boolean expressions into JSON Schema and what actually sits behind "contains" keyword. Since the answer is accepted, could I suggest up-voting it by clicking up arrow?
– PsychoFish
Nov 15 '18 at 19:37
I have tried to upvote but i get the message "Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." - So youre saying I should split up my schema to those definitions? A side note I can indeed validate this with the online validator but the python tools Ive tried all fail in different ways - very sad I may indeed need to think about restructuring - thanks for your help
– Adam
Nov 16 '18 at 10:00
Votes - oh, rep thing, nevermind. Validator: check which draft version python tools do support. Are you using perhaps one of validators enlisted here? json-schema.org/implementations.html#validators Restructuring schema might help, but please note I used "contains" keywords, which was introduced only since draft-06 and you might face the neccessity to change it in a way explained in recommended article. Moreover, I do not know how well python tools do handle "dependencies" -> it might go down to phrasing that specific one otherwise (by a separate subschemas and logical operators)
– PsychoFish
Nov 16 '18 at 11:41
I am using python jsonschema so it should support up to draft-7 but it is expecting "SW Words" to exist all the time. Shame I would have loved to have this automated within our existing python tools
– Adam
Nov 16 '18 at 12:41
|
show 1 more comment
Yuk, upward dependencies can be really unpleaseant. Does the model have to be shaped that way?
Solution
You are on the right track. What you are missing is checking correctly if at least one element of "Fields" array had that "SW Large" : true and then shape the proper dependency.
Since draft-06 it is solved with "contains" keyword. In order to not repeat content, I'd reccommend to read following:
JSON Schema: How to check that an array contains at least one object with a property with a given value?
Json Schema: Require a property only when a specific property is present in a deep nested object (very educational!)
https://json-schema.org/understanding-json-schema/reference/array.html
Your schema re-worked below.
See the "definitions" section
First of all it's adding "contains" : { schema } to "Fields" definition. I need it as separate schema in order to use it as a condition in logical implication.
"definitions" : {
"Fields-contains-at-least-1-element-with-SW-Large-true" : {
"properties": {
"Fields" : {
"contains" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
}
},
}
},
If you would add it permanently, it might look like:
"Fields" : {
"type" : "array",
"contains" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
"items": {...},
}
which translates to "at least one item of "Fields" array must contain object with said property name and said value". Every JSON with "Fields" array that contains no item with "SW Large" : true would fail validation against such schema. And if you reverse the "contains" schema definition like:
"Fields" : {
"type" : "array",
"contains" : {
"not" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
}
"items": {...},
}
it would translate to "no item of "Fields" array can contain object with said property name and said value". Every JSON with "Fields" array that contains at least one item with "SW Large" : true would fail validation against such schema.
I don't want any of above to happen. I want to link "Fields/contains" condition with requiring or not requiring the "SW Words" property one level above, hence getting schema excluded to "definitions" section and making a proper use of it.
The "upward-dependency" is defined using that check and proper logical implication with "anyOf" keyword
It doesn't contain at least 1 item with "SW Large" : true OR "SW Words" is required
"definitions" : {
"upward-dependency" : {
"anyOf" : [
{ "not" : {"$ref" : "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true"} },
{ "required" : ["SW Words"] }
]
},
},
At the level of single item of "Registers" array I've added the "dependencies". Whenever "Fields" item appear in one of "Registers" items, it's been checked if it contains the unfortunate "SW Large" : true and if it does - the "SW Words" becomes required. Voila!
"items" : {
"$id": "#/properties/Registers/items
"type" : "object",
"properties" : {
"Fields": {
"$id": "#/properties/Registers/items/properties/Fields",
"type": "array",
"items": {
"$id": "#/properties/Registers/items/properties/Fields/items",
"type": "object",
"propertyNames" : {
"enum" : [
"Name",
"SW Large"
]
},
"required": [
"Name"
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/Name",
"type": "string"
},
"SW Large": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/SW Large",
"type": "boolean"
}
}
}
}
},
"dependencies" : {
"Fields" : { "$ref" : "#/definitions/upward-dependency" }
},
}
I've checked Schema with online validator and added your object to "examples" section.
Full schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"propertyNames" : {
"enum" : [
"Registers"
]
},
"required": [
"Registers"
],
"examples" : [
{
"Registers": [
{
"Name": "device",
"Address": "100",
"SW Words": 2,
"Fields": [
{
"Name" : "Product",
"SW Large" : true
},
{
"Name": "Version"
}
]
}
]
}
],
"properties": {
"Registers": {
"$id": "#/properties/Registers",
"type": "array",
"items": {
"$id": "#/properties/Registers/items",
"type": "object",
"propertyNames" : {
"enum" : [
"Name",
"Address",
"SW Words",
"Fields"
]
},
"required": [
"Name",
"Address",
"Fields"
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Name",
"type": "string"
},
"Address": {
"$id": "#/properties/Registers/items/properties/Address",
"type": "string"
},
"SW Words": {
"$id": "#/properties/Sections/items/properties/Blocks/items/properties/SW Words",
"type": "integer",
"default": 2,
"minimum": 2
},
"Fields": {
"$id": "#/properties/Registers/items/properties/Fields",
"type": "array",
"items": {
"$id": "#/properties/Registers/items/properties/Fields/items",
"type": "object",
"propertyNames" : {
"enum" : [
"Name",
"SW Large"
]
},
"required": [
"Name"
],
"properties": {
"Name": {
"type": "string"
},
"SW Large": {
"type": "boolean"
}
}
}
}
},
"dependencies" : {
"Fields" : { "$ref" : "#/definitions/upward-dependency" }
},
}
}
},
"definitions" : {
"upward-dependency" : {
"anyOf" : [
{ "not" : {"$ref" : "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true"} },
{ "required" : ["SW Words"] }
]
},
"Fields-contains-at-least-1-element-with-SW-Large-true" : {
"properties": {
"Fields" : {
"contains" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
}
},
}
},
}
Some side notes:
Instead of "additionalProperties":false please use "propertyNames". It's been specifically introduced for purpose of making sure only demanded properties are present in JSON object validated aginst schema.
"propertyNames" : {
"enum" : [
"Registers"
]
}
See: https://json-schema.org/understanding-json-schema/reference/object.html#property-names
It is very important to properly shape schemas/sub-schemas depending on which level these should be applied. Please note how "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true" properly reflects schema at "#/Registers/items" nesting level (since the "dependencies" is applied to an object on "#/Registers/items" level and one can think of it as of ""#/Registers/items/dependencies"). See https://json-schema.org/understanding-json-schema/reference/object.html#dependencies .
If you intend to validate single item of "Fields" array separately or even single item of "Registers" array separately, you might consider reshaping your schema to separate sub-schemas like I did with "questionA" in here: https://stackoverflow.com/a/53309856/2811843 . Thus - if necessity is there - you could easily exclude sub-schemas from main schema and just properly reference to them using "$ref" keyword. Some reading on JSON Pointer and relative JSON Pointer might be also useful for structuring complex schemas.
Worth to start at: https://json-schema.org/understanding-json-schema/structuring.html
I hope it helped.
You're a wizard, was wrecking my head with this. Thanks for the additional tips, still learning the basics!
– Adam
Nov 15 '18 at 17:26
Thank you, but the wizardry credit is exaggerated. You already had proper idea on how to solve it @Adam . I've edited answer by adding more exceptions from main schema to illustrate solution approach + two more side notes. I strongly recommend reading through stackoverflow.com/questions/48015822/… - it presents how to translate Boolean expressions into JSON Schema and what actually sits behind "contains" keyword. Since the answer is accepted, could I suggest up-voting it by clicking up arrow?
– PsychoFish
Nov 15 '18 at 19:37
I have tried to upvote but i get the message "Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." - So youre saying I should split up my schema to those definitions? A side note I can indeed validate this with the online validator but the python tools Ive tried all fail in different ways - very sad I may indeed need to think about restructuring - thanks for your help
– Adam
Nov 16 '18 at 10:00
Votes - oh, rep thing, nevermind. Validator: check which draft version python tools do support. Are you using perhaps one of validators enlisted here? json-schema.org/implementations.html#validators Restructuring schema might help, but please note I used "contains" keywords, which was introduced only since draft-06 and you might face the neccessity to change it in a way explained in recommended article. Moreover, I do not know how well python tools do handle "dependencies" -> it might go down to phrasing that specific one otherwise (by a separate subschemas and logical operators)
– PsychoFish
Nov 16 '18 at 11:41
I am using python jsonschema so it should support up to draft-7 but it is expecting "SW Words" to exist all the time. Shame I would have loved to have this automated within our existing python tools
– Adam
Nov 16 '18 at 12:41
|
show 1 more comment
Yuk, upward dependencies can be really unpleaseant. Does the model have to be shaped that way?
Solution
You are on the right track. What you are missing is checking correctly if at least one element of "Fields" array had that "SW Large" : true and then shape the proper dependency.
Since draft-06 it is solved with "contains" keyword. In order to not repeat content, I'd reccommend to read following:
JSON Schema: How to check that an array contains at least one object with a property with a given value?
Json Schema: Require a property only when a specific property is present in a deep nested object (very educational!)
https://json-schema.org/understanding-json-schema/reference/array.html
Your schema re-worked below.
See the "definitions" section
First of all it's adding "contains" : { schema } to "Fields" definition. I need it as separate schema in order to use it as a condition in logical implication.
"definitions" : {
"Fields-contains-at-least-1-element-with-SW-Large-true" : {
"properties": {
"Fields" : {
"contains" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
}
},
}
},
If you would add it permanently, it might look like:
"Fields" : {
"type" : "array",
"contains" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
"items": {...},
}
which translates to "at least one item of "Fields" array must contain object with said property name and said value". Every JSON with "Fields" array that contains no item with "SW Large" : true would fail validation against such schema. And if you reverse the "contains" schema definition like:
"Fields" : {
"type" : "array",
"contains" : {
"not" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
}
"items": {...},
}
it would translate to "no item of "Fields" array can contain object with said property name and said value". Every JSON with "Fields" array that contains at least one item with "SW Large" : true would fail validation against such schema.
I don't want any of above to happen. I want to link "Fields/contains" condition with requiring or not requiring the "SW Words" property one level above, hence getting schema excluded to "definitions" section and making a proper use of it.
The "upward-dependency" is defined using that check and proper logical implication with "anyOf" keyword
It doesn't contain at least 1 item with "SW Large" : true OR "SW Words" is required
"definitions" : {
"upward-dependency" : {
"anyOf" : [
{ "not" : {"$ref" : "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true"} },
{ "required" : ["SW Words"] }
]
},
},
At the level of single item of "Registers" array I've added the "dependencies". Whenever "Fields" item appear in one of "Registers" items, it's been checked if it contains the unfortunate "SW Large" : true and if it does - the "SW Words" becomes required. Voila!
"items" : {
"$id": "#/properties/Registers/items
"type" : "object",
"properties" : {
"Fields": {
"$id": "#/properties/Registers/items/properties/Fields",
"type": "array",
"items": {
"$id": "#/properties/Registers/items/properties/Fields/items",
"type": "object",
"propertyNames" : {
"enum" : [
"Name",
"SW Large"
]
},
"required": [
"Name"
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/Name",
"type": "string"
},
"SW Large": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/SW Large",
"type": "boolean"
}
}
}
}
},
"dependencies" : {
"Fields" : { "$ref" : "#/definitions/upward-dependency" }
},
}
I've checked Schema with online validator and added your object to "examples" section.
Full schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"propertyNames" : {
"enum" : [
"Registers"
]
},
"required": [
"Registers"
],
"examples" : [
{
"Registers": [
{
"Name": "device",
"Address": "100",
"SW Words": 2,
"Fields": [
{
"Name" : "Product",
"SW Large" : true
},
{
"Name": "Version"
}
]
}
]
}
],
"properties": {
"Registers": {
"$id": "#/properties/Registers",
"type": "array",
"items": {
"$id": "#/properties/Registers/items",
"type": "object",
"propertyNames" : {
"enum" : [
"Name",
"Address",
"SW Words",
"Fields"
]
},
"required": [
"Name",
"Address",
"Fields"
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Name",
"type": "string"
},
"Address": {
"$id": "#/properties/Registers/items/properties/Address",
"type": "string"
},
"SW Words": {
"$id": "#/properties/Sections/items/properties/Blocks/items/properties/SW Words",
"type": "integer",
"default": 2,
"minimum": 2
},
"Fields": {
"$id": "#/properties/Registers/items/properties/Fields",
"type": "array",
"items": {
"$id": "#/properties/Registers/items/properties/Fields/items",
"type": "object",
"propertyNames" : {
"enum" : [
"Name",
"SW Large"
]
},
"required": [
"Name"
],
"properties": {
"Name": {
"type": "string"
},
"SW Large": {
"type": "boolean"
}
}
}
}
},
"dependencies" : {
"Fields" : { "$ref" : "#/definitions/upward-dependency" }
},
}
}
},
"definitions" : {
"upward-dependency" : {
"anyOf" : [
{ "not" : {"$ref" : "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true"} },
{ "required" : ["SW Words"] }
]
},
"Fields-contains-at-least-1-element-with-SW-Large-true" : {
"properties": {
"Fields" : {
"contains" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
}
},
}
},
}
Some side notes:
Instead of "additionalProperties":false please use "propertyNames". It's been specifically introduced for purpose of making sure only demanded properties are present in JSON object validated aginst schema.
"propertyNames" : {
"enum" : [
"Registers"
]
}
See: https://json-schema.org/understanding-json-schema/reference/object.html#property-names
It is very important to properly shape schemas/sub-schemas depending on which level these should be applied. Please note how "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true" properly reflects schema at "#/Registers/items" nesting level (since the "dependencies" is applied to an object on "#/Registers/items" level and one can think of it as of ""#/Registers/items/dependencies"). See https://json-schema.org/understanding-json-schema/reference/object.html#dependencies .
If you intend to validate single item of "Fields" array separately or even single item of "Registers" array separately, you might consider reshaping your schema to separate sub-schemas like I did with "questionA" in here: https://stackoverflow.com/a/53309856/2811843 . Thus - if necessity is there - you could easily exclude sub-schemas from main schema and just properly reference to them using "$ref" keyword. Some reading on JSON Pointer and relative JSON Pointer might be also useful for structuring complex schemas.
Worth to start at: https://json-schema.org/understanding-json-schema/structuring.html
I hope it helped.
Yuk, upward dependencies can be really unpleaseant. Does the model have to be shaped that way?
Solution
You are on the right track. What you are missing is checking correctly if at least one element of "Fields" array had that "SW Large" : true and then shape the proper dependency.
Since draft-06 it is solved with "contains" keyword. In order to not repeat content, I'd reccommend to read following:
JSON Schema: How to check that an array contains at least one object with a property with a given value?
Json Schema: Require a property only when a specific property is present in a deep nested object (very educational!)
https://json-schema.org/understanding-json-schema/reference/array.html
Your schema re-worked below.
See the "definitions" section
First of all it's adding "contains" : { schema } to "Fields" definition. I need it as separate schema in order to use it as a condition in logical implication.
"definitions" : {
"Fields-contains-at-least-1-element-with-SW-Large-true" : {
"properties": {
"Fields" : {
"contains" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
}
},
}
},
If you would add it permanently, it might look like:
"Fields" : {
"type" : "array",
"contains" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
"items": {...},
}
which translates to "at least one item of "Fields" array must contain object with said property name and said value". Every JSON with "Fields" array that contains no item with "SW Large" : true would fail validation against such schema. And if you reverse the "contains" schema definition like:
"Fields" : {
"type" : "array",
"contains" : {
"not" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
}
"items": {...},
}
it would translate to "no item of "Fields" array can contain object with said property name and said value". Every JSON with "Fields" array that contains at least one item with "SW Large" : true would fail validation against such schema.
I don't want any of above to happen. I want to link "Fields/contains" condition with requiring or not requiring the "SW Words" property one level above, hence getting schema excluded to "definitions" section and making a proper use of it.
The "upward-dependency" is defined using that check and proper logical implication with "anyOf" keyword
It doesn't contain at least 1 item with "SW Large" : true OR "SW Words" is required
"definitions" : {
"upward-dependency" : {
"anyOf" : [
{ "not" : {"$ref" : "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true"} },
{ "required" : ["SW Words"] }
]
},
},
At the level of single item of "Registers" array I've added the "dependencies". Whenever "Fields" item appear in one of "Registers" items, it's been checked if it contains the unfortunate "SW Large" : true and if it does - the "SW Words" becomes required. Voila!
"items" : {
"$id": "#/properties/Registers/items
"type" : "object",
"properties" : {
"Fields": {
"$id": "#/properties/Registers/items/properties/Fields",
"type": "array",
"items": {
"$id": "#/properties/Registers/items/properties/Fields/items",
"type": "object",
"propertyNames" : {
"enum" : [
"Name",
"SW Large"
]
},
"required": [
"Name"
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/Name",
"type": "string"
},
"SW Large": {
"$id": "#/properties/Registers/items/properties/Fields/items/properties/SW Large",
"type": "boolean"
}
}
}
}
},
"dependencies" : {
"Fields" : { "$ref" : "#/definitions/upward-dependency" }
},
}
I've checked Schema with online validator and added your object to "examples" section.
Full schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"propertyNames" : {
"enum" : [
"Registers"
]
},
"required": [
"Registers"
],
"examples" : [
{
"Registers": [
{
"Name": "device",
"Address": "100",
"SW Words": 2,
"Fields": [
{
"Name" : "Product",
"SW Large" : true
},
{
"Name": "Version"
}
]
}
]
}
],
"properties": {
"Registers": {
"$id": "#/properties/Registers",
"type": "array",
"items": {
"$id": "#/properties/Registers/items",
"type": "object",
"propertyNames" : {
"enum" : [
"Name",
"Address",
"SW Words",
"Fields"
]
},
"required": [
"Name",
"Address",
"Fields"
],
"properties": {
"Name": {
"$id": "#/properties/Registers/items/properties/Name",
"type": "string"
},
"Address": {
"$id": "#/properties/Registers/items/properties/Address",
"type": "string"
},
"SW Words": {
"$id": "#/properties/Sections/items/properties/Blocks/items/properties/SW Words",
"type": "integer",
"default": 2,
"minimum": 2
},
"Fields": {
"$id": "#/properties/Registers/items/properties/Fields",
"type": "array",
"items": {
"$id": "#/properties/Registers/items/properties/Fields/items",
"type": "object",
"propertyNames" : {
"enum" : [
"Name",
"SW Large"
]
},
"required": [
"Name"
],
"properties": {
"Name": {
"type": "string"
},
"SW Large": {
"type": "boolean"
}
}
}
}
},
"dependencies" : {
"Fields" : { "$ref" : "#/definitions/upward-dependency" }
},
}
}
},
"definitions" : {
"upward-dependency" : {
"anyOf" : [
{ "not" : {"$ref" : "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true"} },
{ "required" : ["SW Words"] }
]
},
"Fields-contains-at-least-1-element-with-SW-Large-true" : {
"properties": {
"Fields" : {
"contains" : {
"properties": {
"SW Large": { "enum": [true] }
},
"required" : ["SW Large"]
}
}
},
}
},
}
Some side notes:
Instead of "additionalProperties":false please use "propertyNames". It's been specifically introduced for purpose of making sure only demanded properties are present in JSON object validated aginst schema.
"propertyNames" : {
"enum" : [
"Registers"
]
}
See: https://json-schema.org/understanding-json-schema/reference/object.html#property-names
It is very important to properly shape schemas/sub-schemas depending on which level these should be applied. Please note how "#/definitions/Fields-contains-at-least-1-element-with-SW-Large-true" properly reflects schema at "#/Registers/items" nesting level (since the "dependencies" is applied to an object on "#/Registers/items" level and one can think of it as of ""#/Registers/items/dependencies"). See https://json-schema.org/understanding-json-schema/reference/object.html#dependencies .
If you intend to validate single item of "Fields" array separately or even single item of "Registers" array separately, you might consider reshaping your schema to separate sub-schemas like I did with "questionA" in here: https://stackoverflow.com/a/53309856/2811843 . Thus - if necessity is there - you could easily exclude sub-schemas from main schema and just properly reference to them using "$ref" keyword. Some reading on JSON Pointer and relative JSON Pointer might be also useful for structuring complex schemas.
Worth to start at: https://json-schema.org/understanding-json-schema/structuring.html
I hope it helped.
edited Nov 15 '18 at 23:20
answered Nov 15 '18 at 14:34
PsychoFishPsychoFish
86699
86699
You're a wizard, was wrecking my head with this. Thanks for the additional tips, still learning the basics!
– Adam
Nov 15 '18 at 17:26
Thank you, but the wizardry credit is exaggerated. You already had proper idea on how to solve it @Adam . I've edited answer by adding more exceptions from main schema to illustrate solution approach + two more side notes. I strongly recommend reading through stackoverflow.com/questions/48015822/… - it presents how to translate Boolean expressions into JSON Schema and what actually sits behind "contains" keyword. Since the answer is accepted, could I suggest up-voting it by clicking up arrow?
– PsychoFish
Nov 15 '18 at 19:37
I have tried to upvote but i get the message "Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." - So youre saying I should split up my schema to those definitions? A side note I can indeed validate this with the online validator but the python tools Ive tried all fail in different ways - very sad I may indeed need to think about restructuring - thanks for your help
– Adam
Nov 16 '18 at 10:00
Votes - oh, rep thing, nevermind. Validator: check which draft version python tools do support. Are you using perhaps one of validators enlisted here? json-schema.org/implementations.html#validators Restructuring schema might help, but please note I used "contains" keywords, which was introduced only since draft-06 and you might face the neccessity to change it in a way explained in recommended article. Moreover, I do not know how well python tools do handle "dependencies" -> it might go down to phrasing that specific one otherwise (by a separate subschemas and logical operators)
– PsychoFish
Nov 16 '18 at 11:41
I am using python jsonschema so it should support up to draft-7 but it is expecting "SW Words" to exist all the time. Shame I would have loved to have this automated within our existing python tools
– Adam
Nov 16 '18 at 12:41
|
show 1 more comment
You're a wizard, was wrecking my head with this. Thanks for the additional tips, still learning the basics!
– Adam
Nov 15 '18 at 17:26
Thank you, but the wizardry credit is exaggerated. You already had proper idea on how to solve it @Adam . I've edited answer by adding more exceptions from main schema to illustrate solution approach + two more side notes. I strongly recommend reading through stackoverflow.com/questions/48015822/… - it presents how to translate Boolean expressions into JSON Schema and what actually sits behind "contains" keyword. Since the answer is accepted, could I suggest up-voting it by clicking up arrow?
– PsychoFish
Nov 15 '18 at 19:37
I have tried to upvote but i get the message "Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." - So youre saying I should split up my schema to those definitions? A side note I can indeed validate this with the online validator but the python tools Ive tried all fail in different ways - very sad I may indeed need to think about restructuring - thanks for your help
– Adam
Nov 16 '18 at 10:00
Votes - oh, rep thing, nevermind. Validator: check which draft version python tools do support. Are you using perhaps one of validators enlisted here? json-schema.org/implementations.html#validators Restructuring schema might help, but please note I used "contains" keywords, which was introduced only since draft-06 and you might face the neccessity to change it in a way explained in recommended article. Moreover, I do not know how well python tools do handle "dependencies" -> it might go down to phrasing that specific one otherwise (by a separate subschemas and logical operators)
– PsychoFish
Nov 16 '18 at 11:41
I am using python jsonschema so it should support up to draft-7 but it is expecting "SW Words" to exist all the time. Shame I would have loved to have this automated within our existing python tools
– Adam
Nov 16 '18 at 12:41
You're a wizard, was wrecking my head with this. Thanks for the additional tips, still learning the basics!
– Adam
Nov 15 '18 at 17:26
You're a wizard, was wrecking my head with this. Thanks for the additional tips, still learning the basics!
– Adam
Nov 15 '18 at 17:26
Thank you, but the wizardry credit is exaggerated. You already had proper idea on how to solve it @Adam . I've edited answer by adding more exceptions from main schema to illustrate solution approach + two more side notes. I strongly recommend reading through stackoverflow.com/questions/48015822/… - it presents how to translate Boolean expressions into JSON Schema and what actually sits behind "contains" keyword. Since the answer is accepted, could I suggest up-voting it by clicking up arrow?
– PsychoFish
Nov 15 '18 at 19:37
Thank you, but the wizardry credit is exaggerated. You already had proper idea on how to solve it @Adam . I've edited answer by adding more exceptions from main schema to illustrate solution approach + two more side notes. I strongly recommend reading through stackoverflow.com/questions/48015822/… - it presents how to translate Boolean expressions into JSON Schema and what actually sits behind "contains" keyword. Since the answer is accepted, could I suggest up-voting it by clicking up arrow?
– PsychoFish
Nov 15 '18 at 19:37
I have tried to upvote but i get the message "Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." - So youre saying I should split up my schema to those definitions? A side note I can indeed validate this with the online validator but the python tools Ive tried all fail in different ways - very sad I may indeed need to think about restructuring - thanks for your help
– Adam
Nov 16 '18 at 10:00
I have tried to upvote but i get the message "Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." - So youre saying I should split up my schema to those definitions? A side note I can indeed validate this with the online validator but the python tools Ive tried all fail in different ways - very sad I may indeed need to think about restructuring - thanks for your help
– Adam
Nov 16 '18 at 10:00
Votes - oh, rep thing, nevermind. Validator: check which draft version python tools do support. Are you using perhaps one of validators enlisted here? json-schema.org/implementations.html#validators Restructuring schema might help, but please note I used "contains" keywords, which was introduced only since draft-06 and you might face the neccessity to change it in a way explained in recommended article. Moreover, I do not know how well python tools do handle "dependencies" -> it might go down to phrasing that specific one otherwise (by a separate subschemas and logical operators)
– PsychoFish
Nov 16 '18 at 11:41
Votes - oh, rep thing, nevermind. Validator: check which draft version python tools do support. Are you using perhaps one of validators enlisted here? json-schema.org/implementations.html#validators Restructuring schema might help, but please note I used "contains" keywords, which was introduced only since draft-06 and you might face the neccessity to change it in a way explained in recommended article. Moreover, I do not know how well python tools do handle "dependencies" -> it might go down to phrasing that specific one otherwise (by a separate subschemas and logical operators)
– PsychoFish
Nov 16 '18 at 11:41
I am using python jsonschema so it should support up to draft-7 but it is expecting "SW Words" to exist all the time. Shame I would have loved to have this automated within our existing python tools
– Adam
Nov 16 '18 at 12:41
I am using python jsonschema so it should support up to draft-7 but it is expecting "SW Words" to exist all the time. Shame I would have loved to have this automated within our existing python tools
– Adam
Nov 16 '18 at 12:41
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317843%2fjson-schema-validation-dependencies-on-subschema%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown