Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default value does not work #975

Closed
KunWangV opened this issue Jul 12, 2018 · 11 comments
Closed

default value does not work #975

KunWangV opened this issue Jul 12, 2018 · 11 comments

Comments

@KunWangV
Copy link

{
  "title": "Person",
  "type": "object",
  "properties": {
    "Do you have any pets?": {
      "type": "string",
      "enum": [
        "No",
        "Yes: One",
        "Yes: More than one"
      ],
      "default": "No"
    }
  },
  "required": [
    "Do you have any pets?"
  ],
  "dependencies": {
    "Do you have any pets?": {
      "oneOf": [
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "No"
              ]
            }
          }
        },
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "Yes: One"
              ]
            },
            "How old is your pet?": {
              "type": "number"
            }
          },
          "required": [
            "How old is your pet?"
          ]
        },
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "Yes: More than one"
              ]
            },
            "Do you want to get rid of any?": {
              "type": "boolean",
              **"default": false;**
            }
          },
          "required": [
            "Do you want to get rid of any?"
          ]
        }
      ]
    }
  }
}

as shown in the code, the default value for "Do you want to get rid of any?" does not work!

@doncesarts
Copy link
Contributor

Hi @KunWangV , You have a typo in your schema definition the character ; in the "default" definition of "Do you want to get rid of any?" . Apart from that it is working the default value as expected.

{
  "title": "Person",
  "type": "object",
  "properties": {
    "Do you have any pets?": {
      "type": "string",
      "enum": [
        "No",
        "Yes: One",
        "Yes: More than one"
      ],
      "default": "No"
    }
  },
  "required": [
    "Do you have any pets?"
  ],
  "dependencies": {
    "Do you have any pets?": {
      "oneOf": [
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "No"
              ]
            }
          }
        },
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "Yes: One"
              ]
            },
            "How old is your pet?": {
              "type": "number"
            }
          },
          "required": [
            "How old is your pet?"
          ]
        },
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "Yes: More than one"
              ]
            },
            "Do you want to get rid of any?": {
              "type": "boolean",
              "default": false
            }
          },
          "required": [
            "Do you want to get rid of any?"
          ]
        }
      ]
    }
  }
}

Copy and paste that on https://mozilla-services.github.io/react-jsonschema-form/ , it works.

Regards

@KunWangV
Copy link
Author

I cannot get the expected result using your code, are you sure it is ok?

@cmawhorter
Copy link

my guess is that OP is running into the same problem i am. if the initial formData contains any value other than undefined for a property (like null), default will be ignored.

that's correct, but unexpected, especially when dealing with non-required fields.

IMO -- null for non-required fields should be stripped and not fail validation, since there is no way for an end-user to enter a value of null.

json schema supports multiple types e.g. something: { type: [ 'string', 'null' ] }. if this lib supported that, this would be a non-issue.

but as it stands right now, you need to preprocess data and strip all null values before handing it off to jsonschema-form or else it'll force your user to interact with a non-required field.

An example for https://mozilla-services.github.io/react-jsonschema-form/

{
  "type": "object",
  "properties": {
    "notrequired": {
      "type": "string",
      "title": "Not required",
      "minLength": 0,
      "default": "fdsa"
    }
  }
}

formData

{
  "notrequired": null
}

With that, there is no way to submit the form.

This uischema doesn't help matters either: { "notrequired": { "ui:emptyValue": "" } }

@jduncanRadBlue
Copy link

Any updates here? I have a complex form that starts with a single drop down box. The form is populated based off of what is selected using the dependencies. I have several default values that need to be pre-populated. Anyone know how to get the default value to work here?

@glasserc
Copy link
Contributor

I think this issue is not really coherent so I'm going to close it.

As mentioned by @doncesarts, when I fix the typo in the JSON schema, it seems to work OK (an empty formData becomes {"Do you have any pets": "No"}, and selecting Yes: More than one auto-fills "Do you want to get rid of any?": false).

@cmawhorter: default values aren't applied when existing values are present. Yes, that includes null, false, empty arrays, or any other JSON-allowed value. I don't really expect that behavior to change. rjsf generally doesn't try to "clean" data.

@jduncanRadBlue, if your issue is not addressed by the above remarks, maybe it would be better to open a new issue.

@cmawhorter
Copy link

@glasserc The change @doncesarts mentioned does not work. I copy the typo-less schema into the live editor and hit submit and result: {}. It should be: { "Do you have any pets?": "No" }, because No is the default.

I'm not talking about sanitizing the data as much as I'm talking about a limitation in this lib that can't be worked around.

Getting back to the pets schema. The default value for "Do you have any pets?" is pointless because it's impossible to ever use it (as far as rjsf is concerned).

@jduncanRadBlue
Copy link

jduncanRadBlue commented Nov 27, 2018

@glasserc The change @doncesarts mentioned does not work for me either. The part that I'm concerned with is the dependencies. I need to be able to specify default values for those fields. I am not able to get those default values to render.

@glasserc
Copy link
Contributor

@cmawhorter I just tried pasting the typo-less schema into the playground and replacing the formData with {} and it was immediately replaced with

{
  "Do you have any pets?": "No"
}

Clicking Submit caused it to log exactly the same object to the log. It sounds like you're seeing something else so please feel free to open another issue to describe what exactly you're doing and what exactly you're seeing.

@jduncanRadBlue Here is the same pet schema with a schema dependency on the field which has a default as well as providing a default.

{
  "title": "Person",
  "type": "object",
  "properties": {
    "Do you have any pets?": {
      "type": "string",
      "enum": [
        "No",
        "Yes: One",
        "Yes: More than one"
      ],
      "default": "Yes: One"
    }
  },
  "required": [
    "Do you have any pets?"
  ],
  "dependencies": {
    "Do you have any pets?": {
      "oneOf": [
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "No"
              ]
            }
          }
        },
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "Yes: One"
              ]
            },
            "How old is your pet?": {
              "type": "number",
              "default": 21
            }
          },
          "required": [
            "How old is your pet?"
          ]
        },
        {
          "properties": {
            "Do you have any pets?": {
              "enum": [
                "Yes: More than one"
              ]
            },
            "Do you want to get rid of any?": {
              "type": "boolean",
              "default": false
            }
          },
          "required": [
            "Do you want to get rid of any?"
          ]
        }
      ]
    }
  }
}

Replacing the formData with {} caused it to produce

{
  "Do you have any pets?": "Yes: One",
  "How old is your pet?": 21
}

As previously mentioned, since this issue doesn't really make sense, please feel free to open a new one.

@MatinF
Copy link

MatinF commented Feb 5, 2019

@glasserc I seem to have a similar example, but I'm unable to make the dependent properties show with the default values. Can you let me know if this is due to an issue with the react-jsonschema-form or with my schema?

Specifically, the below can be parsed in the PlayGround. Upon selecting Configuration Mode to be "Simple", I'd expect the default value of "Bit-rate standard" to be 250000 (i.e. 250K) - but it's simply the first entry in the available list. Further, no value is added to the formData until I manually select another value for "Bit-rate standard".

Any thoughts/inputs would be appreciated,
Martin

{
  "type": "object",
  "properties": {
    "can": {
      "type": "object",
      "properties": {
        "phy": {
          "title": "",
          "type": "object",
          "properties": {
            "bit_rate_cfg_mode": {
              "title": "Configuration mode",
              "type": "integer",
              "default": 1,
              "anyOf": [
                {
                  "title": "Auto",
                  "enum": [
                    1
                  ]
                },
                {
                  "title": "Simple",
                  "enum": [
                    2
                  ]
                }
              ]
            }
          },
          "dependencies": {
            "bit_rate_cfg_mode": {
              "oneOf": [
                {
                  "properties": {
                    "bit_rate_cfg_mode": {
                      "enum": [
                        1
                      ]
                    }
                  }
                },
                {
                  "properties": {
                    "bit_rate_cfg_mode": {
                      "enum": [
                        2
                      ]
                    },
                    "bit_rate_std": {
                      "title": "Bit-rate standard",
                      "type": "integer",
                      "default": 250000,
                      "anyOf": [
                        {
                          "title": "5K",
                          "enum": [
                            5000
                          ]
                        },
                        {
                          "title": "10K",
                          "enum": [
                            10000
                          ]
                        },
                        {
                          "title": "20K",
                          "enum": [
                            20000
                          ]
                        },
                        {
                          "title": "33.333K",
                          "enum": [
                            33333
                          ]
                        },
                        {
                          "title": "47.619K",
                          "enum": [
                            47619
                          ]
                        },
                        {
                          "title": "50K",
                          "enum": [
                            50000
                          ]
                        },
                        {
                          "title": "83.333K",
                          "enum": [
                            83333
                          ]
                        },
                        {
                          "title": "95.238K",
                          "enum": [
                            95238
                          ]
                        },
                        {
                          "title": "100K",
                          "enum": [
                            100000
                          ]
                        },
                        {
                          "title": "125K",
                          "enum": [
                            125000
                          ]
                        },
                        {
                          "title": "250K",
                          "enum": [
                            250000
                          ]
                        },
                        {
                          "title": "500K",
                          "enum": [
                            500000
                          ]
                        },
                        {
                          "title": "800K",
                          "enum": [
                            800000
                          ]
                        },
                        {
                          "title": "1M",
                          "enum": [
                            1000000
                          ]
                        }
                      ]
                    },
                    "bit_rate_fd": {
                      "title": "Bit-rate FD",
                      "type": "integer",
                      "default": 1,
                      "anyOf": [
                        {
                          "title": "Disable",
                          "enum": [
                            1
                          ]
                        },
                        {
                          "title": "1M",
                          "enum": [
                            1000000
                          ]
                        },
                        {
                          "title": "2M",
                          "enum": [
                            2000000
                          ]
                        },
                        {
                          "title": "5M",
                          "enum": [
                            4000000
                          ]
                        }
                      ]
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}

@epicfaace
Copy link
Member

@MatinF thanks for your report, it does seem to be an issue with using default and oneOf/anyOf. Can you please make a playground link (https://mozilla-services.github.io/react-jsonschema-form/) with your example and make another issue for this?

@MatinF
Copy link

MatinF commented May 22, 2019

@epicfaace I've now added this as a new issue with an example as requested:
#1293

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants