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.

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 issue

Expected 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: image

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: image

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 the properties map a property of type array, where type 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 of , true, or false)
  • 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:closed
  • Created 5 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
germanbisurgicommented, Aug 14, 2018

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)

0reactions
theBullcommented, Feb 14, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set a default value on a Boolean in a Code First model?
The json formatter will ignore default values by default and then your database is setting the boolean to it's default value, false. See...
Read more >
Working with Datatypes | Using JSON | InterSystems IRIS Data ...
Resolving JSON Null and Boolean Values​​ In JSON syntax, the values true, false, and null are distinct from values 1, 0, and ""...
Read more >
JSONObject - Android Developers
In particular, calling put(name, null) removes the named entry from the object but put(name, JSONObject.NULL) stores an entry whose value is JSONObject.
Read more >
Understanding null safety - Dart programming language
If the type is List , you can call .add() or [] on it. If it's int , you can call + ....
Read more >
FormArray - Angular
Set the updateOn property for all controls in a form arraylink ... The options object is used to set a default value for...
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