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

Allow enum with blank value as valid output by schema #648

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

pulges
Copy link

@pulges pulges commented Feb 6, 2019

Adds an option to configure blank value for enum lists in shema.
Adds support for enum lists containing the blank value. (Currently double blank options where rendered in list)

var container = document.getElementById('jsoneditor');

var schema = {
  "title": "Example Schema",
  "type": "object",
  "properties": {
    "academicDegree": {
      "enum": [null, "B.S.", "M.S.", "Ph.D."]
    }
  }
};

var options = {
  mode: 'tree',
  schema: schema,
  enumDefaultValue: null
};

var json = {
  "academicDegree": null
};

var editor = new JSONEditor(container, options, json);

@josdejong
Copy link
Owner

Thanks for your PR @pulges .

Just thinking: shouldn't we simply remove the current behavior which always adds a blank value (and makes an empty string in that case)? So "enum": [null, "B.S.", "M.S.", "Ph.D."] will render a dropdown where the first one is displayed as "--" or blank, and "enum": ["B.S.", "M.S.", "Ph.D."] would render a dropdown with three options and no option to change a value to null?

@pulges
Copy link
Author

pulges commented Feb 18, 2019

I thought about that, but the logic would lack then an option to display invalid/null value already present in json. From UX perspective it might work, if invalid option would be still rendered with '--', but with the validation not passing. If user picks another value '--' disappears from options. Thus user can not pick back invalid value.

@josdejong
Copy link
Owner

That's a good point. Maybe when the current value is invalid, we could let the enum add an extra entry for this with a clear indication that this current value is invalid. Suppose the current value is "foo" and is invalid, the dropdown could render "foo (INVALID)" as first entry, and as you suggest, remove this entry as soon as the user picks a valid value.

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

Successfully merging this pull request may close these issues.

None yet

2 participants