array element of type boolean with false value is defaulting to null when loading json into the editor via the setValue() method
See original GitHub issueExpected behavior
When I specify a property of "type": "array"
in my schema, the editor correctly renders HTML controls to add items to that array. If my array allows various types (boolean, number, string, null), a type
selection dropdown appears adjacent to the input field of each element in my array. If I want to create an array of booleans, I select boolean
from the rendered type selection dropdown of each added element, and a select dropdown (enum) of possible values I am able to select from are rendered as
, true
, and false
. If I choose false
and save the value of the editor JSON (say, to localStorage), upon refreshing the page or reloading the editor, the editor should correctly select the boolean
type and false
value that I specified in my JSON. In particuar, loading the saved JSON back into the editor after page reload via the editor.setValue(savedJson)
method should render the controls correctly, such that a boolean
array element in my JSON with a value of false
, should render as such in the editor HTML. Note that this functionality occurs as expected if the value I specify is true
.
Actual behavior
When reloading the JSON via editor.setValue(...)
, the rendered type dropdown for the element in my array that should be boolean
instead renders as null
, and instead of displaying an enum drop down for
, true
, and false
, no value selection box appears (this appears to be the intended functionality when type is null
, but in this case, the type should be boolean
). As mentioned, the functionality renders the controls correctly when my boolean value is true
, however, the seemingly incorrect behavior happens when I specify a boolean value of false
. It also appears that selecting the empty value from the boolean dropdown list defaults to a value of true
after reloading the editor.
What I am selecting in the editor:
What the json looks like before refresh:
{"values": [true,false]}
(JSON correctly includes false
entry in my array)
What the editor displays after reloading the page/editor:
What the json looks like after the save:
{"values": [true, null]}
(JSON incorrectly includes a null
entry in my array instead of false
)
Steps to reproduce the behavior
NOTE: My schema makes extensive use of the $ref
property to separate various nested schemas into their own files, for sanity purposes. The schema I have provided here is a subset of my overall schema; I can’t see this being a cause of this issue, but I could be wrong; if I need to provide more of my schema, LMK.
- Create a simple schema of type
object
and specify an entry in theproperties
map a property of typearray
, wheretype
is one of a possible list of types. - In the editor, add an element to this array via the UI controls
- select
boolean
from the rendered type select dropdown - select
false
from the value input dropdown (should be an enum list oftrue
, orfalse
) - store the json programmatically somehow (such as in localStorage) - if you repro with localStorage, you’ll need to stringify the JSON.
- refresh the page
- set the value of the editor programmatically
editor.setValue(json)
after page refresh. If you repro with localStorage, you’ll need to parse the stringified JSON before passing it to this method.
{
"title": "Output",
"type": "object",
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"values": {
"type": "array",
"items": {
"type": ["boolean", "string", "null", "number"]
}
}
}
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:8
Top GitHub Comments
Hi theBull.
I tested your scenario and you are right. The json-editor dont recognizes the type of the values very well when not explicitly specified. Its a feature that we could add or fix (i don know at the moment).
But i think i find a hack for this. Use the “setValue” 2 times. Try it and Tell me if that fixes the problem for you (at least temp)
Hey everyone. I did a little more digging and couldn’t pinpoint the issue either. The workaround mentioned above worked to get me unblocked, so for what it’s worth, if anyone stumbles across this while the cause is being investigated, give that a shot.