rjsf-team / react-jsonschema-form

A React component for building Web forms from JSON Schema.
https://rjsf-team.github.io/react-jsonschema-form/
Apache License 2.0
14.11k stars 2.18k forks source link

Nested dependencies not updated correctly #3450

Open sunkeysun opened 1 year ago

sunkeysun commented 1 year ago

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
heath-freenome commented 1 year ago

@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?

sarpere commented 1 year ago

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

heath-freenome commented 1 year ago

@sarpere That would be great!

furkan-akinci commented 1 year ago

any updates?

sunkeysun commented 1 year ago

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

any updates?

heath-freenome commented 1 year ago

@sunkeysun I am awaiting a PR from @sarpere

sarpere commented 1 year ago

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

mahendra790 commented 6 months ago

@heath-freenome Any updates?

I can work on it

heath-freenome commented 6 months ago

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

mahendra790 commented 6 months ago

@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 commented 6 months ago

@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 commented 6 months ago

@heath-freenome can you please check once?

heath-freenome commented 6 months ago

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

mahendra790 commented 6 months ago

@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?

heath-freenome commented 6 months ago

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

mahendra790 commented 6 months ago

@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?

heath-freenome commented 6 months ago

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