remoteoss / json-schema-form

https://json-schema-form.vercel.app
MIT License
85 stars 6 forks source link

Conditionals attributes are not removed after the condition is unmatched #56

Closed sandrina-p closed 11 months ago

sandrina-p commented 11 months ago

Problem

The following example is about description, but this happens with any conditional attribute (title, minimum, etc).

Given a form that asks for two fields is_full_time (yes/no) and hours (number). If yes is selected, then it shows a description warning about the maximum hours.

The JSON Schema is the following:

{
  "properties": {
    "is_full_time": {
      "type": "string",
      "title": "Is full time",
      "oneOf": [{ "const": "yes", "title": "Yes" }, { "const": "no", "title": "No" }]
    },
    "hours": { "type": "number", "title": "Hours per day" }
  },
  "allOf": [
    {
      "if": {
        "properties": { "is_full_time": { "const": "yes" } },
        "required": ["is_full_time"]
      },
      "then": {
        "properties": {
          "hours": {
            "description": "We recommend no more than 8 hours."
          }
        }
      }
    }
  ]
}

Steps to reproduce:

  1. In the playground add the schema above.
  2. Select Yes, the description shows up.
  3. Select No. The description should disappear but it remains 🐛
image

For this scenario, a workaround could be to undo the value by set description: "" in the else statement, like this:

"else": {
  "properties": {
    "hours": {
      "description": ""
     }
   }
}

But this ain't possible for every attribute. For example, if you set const: 10, there's no way to "undo" it. Same for any other type of validation.