2

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

InsOp
  • 2,425
  • 3
  • 27
  • 42

2 Answers2

1

You are missing a "properties" under "dependencies" -> "test". Everything under that point is not recognized as a schema.

  "dependencies": {
    "test": {
      "properties": {
        "enabled": {
          ...

"dependencies" is documented here: https://json-schema.org/understanding-json-schema/reference/conditionals.html#id5.

Ether
  • 53,118
  • 13
  • 86
  • 159
  • thank you I got a rendering. But sadly it does not behave as expected. it should render the text-input if the checkbox is checked. would you say that this is ensured by the given schema? (because if yes, then the react library would be to blame) is there a method to achieve this goal? – InsOp Nov 18 '21 at 16:49
  • In terms of schema correctness, yes, but that won't do what's expected in the form, I think. I'm not sure exactly what they want, and what I've cooked up looks odd data wise. – Relequestual Nov 18 '21 at 16:49
1

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.

Relequestual
  • 11,631
  • 6
  • 47
  • 83