Skip to content
\n

When validating against an empty object I get an error:

\n
Message:\n    Required properties are missing from object: otherIncomeSourceOtherComment.\nSchema path:\n    #/then/required\n
\n

If I set otherIncomeSource as required, and include it in the data as an empty array, then the if/else validates as expected.

\n

What am I doing wrong?

\n

I 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.

\n

You can find an example here:
\nhttps://www.jsonschemavalidator.net/s/bx5E08xw

","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

Hey @andlbrei ,

\n

The 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
{\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}
\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"}}}

Issue with conditional require of field if array contains a const #822

Answered by jviotti
andlbrei asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @andlbrei ,

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

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "otherIncomeSource": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "otherIncomeSourceOtherComment": {
      "type": "s…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@andlbrei
Comment options

Answer selected by andlbrei
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants