Incorrectly Applying 'if-then' Condition to Nested Definition #497
-
Hi, I have following basic json-schema: {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"version": {
"type": "integer"
},
"tools": {
"type": "array",
"items": {
"$ref": "#/definitions/tool"
}
}
},
"definitions": {
"tool": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"type": {
"type": "string"
}
},
"required": [
"type"
]
}
},
"required": [
"version",
"tools"
]
} I am trying to create a simple condition: I've tried multiple expansions of the schema like: "required": [
"version",
"tools"
],
"if": {
"properties": {
"version": {
"const": 1
}
}
},
"then": {
"properties": {
"name": {
"not": {}
}
}
}
} "required": [
"version",
"tools"
],
"if": {
"properties": {
"version": {
"const": 1
}
}
},
"then": {
"definitions": {
"tool": {
"properties": {
"name": {
"not": {}
}
}
}
}
}
} "required": [
"version",
"tools"
],
"if": {
"properties": {
"version": {
"const": 1
}
}
},
"then": {
"definitions": {
"tool": {
"not": {
"required": ["name"]
}
}
}
}
} Here are some examples of valid json:
And some of the invalid json: // A
{
"version": 1,
"tools": [
{
"name": "Tool1",
"type": "TypeA"
},
{
"type": "TypeB"
}
]
} The if clause works fine, and recognizes the change, but the else clause is either not being recognized or refers to I discovered that the condition works smoothly if the nested It may be my bad, in that case, please help me to figure this out. In other case take this as a reported bug. Thanks for your help, Marek |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
No problem, we got you. You got close on your second try. What you have to consider is you need to define Your
You can see it working here: https://jsonschema.dev/s/Z8UqI If you want to understand more about how to think with applicators, we have an article about that: https://json-schema.org/blog/posts/applicability-json-schema-fundamentals-part-1 |
Beta Was this translation helpful? Give feedback.
-
@Relequestual Thank you so much. After your explanation I understood how the tree structure works, and I have a working schema! If I may have a follow-up question. This is my working condition: "if":{
"properties":{
"version":{
"const":1
}
}
},
"then":{
"properties":{
"tools":{
"items":{
"properties":{
"versions":{
"items":{
"allOf":[
{
"properties":{
"InfoA":{
"not":{
"required":[
"name"
]
}
}
}
},
{
"properties":{
"InfoB":{
"not":{
"required":[
"name"
]
}
}
}
},
{
"properties":{
"InfoC":{
"not":{
"required":[
"name"
]
}
}
}
}
]
}
}
}
}
}
}
} But it is quite long. In real case, it has even more items in the To give you more context // Schema that references items that apply in "items" allOf
"versionInfo":{
"type":"object",
"properties":{
"name":{
"description":"Version ...",
"type":"string"
},
"InfoA":{
"$ref":"#/definitions/DownloadInfo"
},
"InfoB":{
"$ref":"#/definitions/DownloadInfo"
},
"InfoC":{
"$ref":"#/definitions/DownloadInfo"
}
}
}
// Schema that references in each Info(A,B,C)
// #/definitions/DownloadInfo
"DownloadInfo": {
"description": "Info about download ...",
"type": "object",
"properties": {
"type": {
"type": "string",
"description": "Download type"
},
"name": {
"type": "string",
"description": "Download name"
}
},
"required": [
"type"
]
} Thanks again, with helping me! |
Beta Was this translation helpful? Give feedback.
No problem, we got you.
This becomes easier to understand when you think about applicability and applicators.
Both
if
andthen
are applicator keywords, as isproperties
anditems
.You got close on your second try. What you have to consider is you need to define
if
andthen
at the level at which it can see down to all locations in the instance it needs to know about.Your
then
clause needs to look like this:You can see it working here: https://jsonschema.dev/s/Z8UqI
If you want to understand more about how to think with applicat…