question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

default value does not work

See original GitHub issue
{
  "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!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
cmawhortercommented, Aug 8, 2018

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": "" } }

0reactions
MatinFcommented, May 22, 2019

@epicfaace I’ve now added this as a new issue with an example as requested: https://github.com/mozilla-services/react-jsonschema-form/issues/1293

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - DefaultValue attribute is not working with my Auto Property
The DefaultValue attribute is only used to tell the Visual Studio Designers (for example when designing a form) what the default value of...
Read more >
Solved: Default Value is not working well with Variables
Solved: I a Number field named "ScoreGreenPoint" + a Button named "+Green".. the scenario is that when i click on the.
Read more >
Default Values Not Working in Forms - WordPress.org
Hello. How does default values work in UM? As they doesn't seem to work as in any other non-UM forms, or they're not...
Read more >
SWITCH default value not working - Microsoft Power BI ...
Solved: Hi everyone, I've been using SWITCH for my report but my issue is that its default value was not showing at all...
Read more >
Set default value business rule not working
Use Set Field Value instead of Set Default Value. That should fix the problem. Sam. Reply.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found