When validating against an empty object I get an error:
\nMessage:\n Required properties are missing from object: otherIncomeSourceOtherComment.\nSchema path:\n #/then/required\n
If I set otherIncomeSource
as required, and include it in the data as an empty array, then the if/else
validates as expected.
What am I doing wrong?
\nI want otherIncomeSource
to be optional, and if it is present and it has an item with the value OTHER
, then I want the field otherIncomeSourceOtherComment
to be required.
You can find an example here:
\nhttps://www.jsonschemavalidator.net/s/bx5E08xw
Hey @andlbrei ,
\nThe problem is on your if
. Your check there is essentially saying that \"if (if otherIncomeSource
is defined and matches the given schema...)\", but doesn't enforce otherIncomeSource
to be present. Hence, an empty schema passes the if
and you get into then
, which forces otherIncomeSourceOtherComment
to be required. Instead, you probably want to add required
to your if
as well, so that the empty instance doesn't pass:
{\n \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n \"type\": \"object\",\n \"properties\": {\n \"otherIncomeSource\": {\n \"type\": \"array\",\n \"items\": {\n \"type\": \"string\"\n }\n },\n \"otherIncomeSourceOtherComment\": {\n \"type\": \"string\"\n }\n },\n \"if\": {\n \"required\": [ \"otherIncomeSource\" ],\n \"properties\": {\n \"otherIncomeSource\": {\n \"type\": \"array\",\n \"contains\": {\n \"const\": \"OTHER\"\n }\n }\n }\n },\n \"then\": {\n \"required\": [\n \"otherIncomeSourceOtherComment\"\n ]\n }\n}
Schemas can quickly get complicated. If it helps, what I typically do is use https://github.com/sourcemeta/jsonschema/blob/main/docs/test.markdown to write little unit tests for all the schemas I write.
","upvoteCount":1,"url":"https://github.com/orgs/json-schema-org/discussions/822#discussioncomment-11142301"}}}-
Hello! This is a my schema {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"otherIncomeSource": {
"type": "array",
"items": {
"type": "string"
}
},
"otherIncomeSourceOtherComment": {
"type": "string"
}
},
"if": {
"properties": {
"otherIncomeSource": {
"type": "array",
"contains": {
"const": "OTHER"
}
}
}
},
"then": {
"required": [
"otherIncomeSourceOtherComment"
]
}
} When validating against an empty object I get an error:
If I set What am I doing wrong? I want You can find an example here: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @andlbrei , The problem is on your {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"otherIncomeSource": {
"type": "array",
"items": {
"type": "string"
}
},
"otherIncomeSourceOtherComment": {
"type": "string"
}
},
"if": {
"required": [ "otherIncomeSource" ],
"properties": {
"otherIncomeSource": {
"type": "array",
"contains": {
"const": "OTHER"
}
}
}
},
"then": {
"required": [
"otherIncomeSourceOtherComment"
]
}
} Schemas can quickly get complicated. If it helps, what I typically do is use https://github.com/sourcemeta/jsonschema/blob/main/docs/test.markdown to write little unit tests for all the schemas I write. |
Beta Was this translation helpful? Give feedback.
Hey @andlbrei ,
The problem is on your
if
. Your check there is essentially saying that "if (ifotherIncomeSource
is defined and matches the given schema...)", but doesn't enforceotherIncomeSource
to be present. Hence, an empty schema passes theif
and you get intothen
, which forcesotherIncomeSourceOtherComment
to be required. Instead, you probably want to addrequired
to yourif
as well, so that the empty instance doesn't pass: