react-jsonschema-form icon indicating copy to clipboard operation
react-jsonschema-form copied to clipboard

Nested dependencies not updated correctly

Open sunkeysun opened this issue 2 years ago • 17 comments

Prerequisites

What theme are you using?

core

What is your question?

Playground

https://codesandbox.io/s/black-forest-bgkbeo?file=/src/App.js As example, conditinal schema property value not update as expected. when "animal" change to "Cat", "food" should be "meat".

schema.json

{
  "type": "object",
  "properties": {
    "animal": {
      "enum": ["Cat", "Fish"],
      "default": "Cat"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "animal": {
            "const": "Cat"
          }
        }
      },
      "then": {
        "properties": {
          "food": {
            "type": "string",
            "enum": ["meat", "grass", "fish"],
            "default": "meat"
          }
        },
        "required": ["food"]
      }
    },
    {
      "if": {
        "properties": {
          "animal": {
            "const": "Fish"
          }
        }
      },
      "then": {
        "properties": {
          "food": {
            "type": "string",
            "enum": ["insect", "worms"],
            "default": "insect"
          },
          "water": {
            "type": "string",
            "enum": ["lake", "sea"],
            "default": "lake"
          }
        },
        "required": ["food", "water"]
      }
    },
    {
      "required": ["animal"]
    }
  ]
}
image

sunkeysun avatar Feb 21 '23 07:02 sunkeysun

@sunkeysun We don't have great support for if/then/else in allOf. It is definitely a bug in how we populate form data. Are you willing to try to create a fix?

heath-freenome avatar Feb 24 '23 16:02 heath-freenome

@heath-freenome I can take care of the bug, if nobody working on the issue.

sarpere avatar Mar 14 '23 06:03 sarpere

@sarpere That would be great!

heath-freenome avatar Mar 14 '23 19:03 heath-freenome

any updates?

furkan-akinci avatar Mar 30 '23 06:03 furkan-akinci

@heath-freenome I can take care of the bug, if nobody working on the issue.

any updates?

sunkeysun avatar Mar 30 '23 07:03 sunkeysun

@sunkeysun I am awaiting a PR from @sarpere

heath-freenome avatar Apr 12 '23 16:04 heath-freenome

@heath-freenome I could not have a time to fix it. if someone can to fix it, It would be better.

sarpere avatar Apr 14 '23 01:04 sarpere

@heath-freenome Any updates?

I can work on it

mahendra790 avatar Feb 22 '24 08:02 mahendra790

@mahendra790 Nothing has happened, so please do feel free to work on a fix

heath-freenome avatar Feb 22 '24 20:02 heath-freenome

@heath-freenome do we have any potential solution or a way in which we can fix this? Because from my initial understanding I think we somehow have to remove or add properties to form data when a field changes then the defaults can be applied

mahendra790 avatar Feb 23 '24 06:02 mahendra790

@heath-freenome There are multiple problems I am facing with allOf with if then else

  1. Defaults are not working correctly it just works for the first time, then the correct default is not set for the field.
  2. It also does not sanitize the form data to remove the fields which are not present in new schema.
  3. It also does not handle the case where the current value in the field is not valid according to new schema I think it should first see if the current value is also valid in new schema, if yes fine otherwise try to use default if present otherwise just reset it.

If I call sanitizeFormData on schema changes then 1 and 2 works fine if we have defaults set for fields otherwise if there is common field incorrect value is not cleared https://github.com/mahendra790/react-jsonschema-form/pull/1/files

I am using this syntax in one of my project and need to get this fixed, can you please guide me how we should approach this problem, may be if we can jump on a call it would be very helpful. I am ready to fix this

mahendra790 avatar Feb 23 '24 14:02 mahendra790

@heath-freenome can you please check once?

mahendra790 avatar Mar 01 '24 17:03 mahendra790

@mahendra790 Sorry, I missed your request for a call. I am free March 8th from 3-5pm Pacific Time. If that works, we can jump on discord.

heath-freenome avatar Mar 01 '24 21:03 heath-freenome

@mahendra790 Sorry, I missed your request for a call. I am free March 8th from 3-5pm Pacific Time. If that works, we can jump on discord.

@heath-freenome sorry, I was busy in some work, can we connect in this week?

mahendra790 avatar Mar 11 '24 07:03 mahendra790

@mahendra790 You are welcome to join the weekly meeting as well. We have the next one tomorrow at 3pm Eastern and noon Pacific

heath-freenome avatar Mar 14 '24 17:03 heath-freenome

@mahendra790 You are welcome to join the weekly meeting as well. We have the next one tomorrow at 3pm Eastern and noon Pacific

@heath-freenome how can I get the link for the meeting?

mahendra790 avatar Mar 14 '24 17:03 mahendra790

It's in the pinned issue on the top of the issues page

heath-freenome avatar Mar 15 '24 20:03 heath-freenome