-
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: