JSON Schema nested dependencies

Viewed 434

I want to conditionally render an input field depending on a checkbox checked.

This checkbox is nested and I dont know how to access it.

I got this to work:

{
  "type": "object",
  "properties": {
    "firstName": {
      "type": "boolean"
    }
  },
  "dependencies": {
    "firstName": {
      "oneOf": [
        {
          "properties": {
            "firstName": {
              "enum": [true]
            },
            "lastName": {
              "type": "string"
            }
          }
        }
      ]
    }
  }
}

example here

Now what if the dependency is nested like so?:

{
  "type": "object",
  "properties": {
    "test": {
      "type": "object",
      "properties": {
        "enabled": {
          "type": "boolean"
        }
      }
    }
  },
  "dependencies": {
    "test": {
      "enabled": {
        "oneOf": [
          {
            "properties": {
              "enabled": {
                "enum": [true]
              },
              "lastName": {
                "type": "string"
              }
            }
          }
        ]
      }
    }
  }
}

as you can see: I have tried to access it but it is not recognized correctly. How would I approach this problem? Is this even possible?

JSON Scheme Validator says its valid

2 Answers

It looks like you have to have the dependencies in the same schema object as the property... like this

{
  "type": "object",
  "properties": {
    "test": {
      "type": "object",
      "properties": {
        "enabled": {
          "type": "boolean"
        }
      },
      "dependencies": {
        "enabled": {
          "oneOf": [
            {
              "properties": {
                "enabled": {
                  "const": true
                },
                "lastName": {
                  "type": "string"
                }
              }
            }
          ]
        }
      }
    }
  }
}

This works, but I don't know if the resulting data is what you would expect.

I'm an expert with JSON Schema, but I've never used the form generation tooling.

Related