jdorn / json-editor

JSON Schema Based Editor
MIT License
5.81k stars 1.08k forks source link

Unexpected behavior with different types in array #327

Open tgeorges opened 9 years ago

tgeorges commented 9 years ago

Hi Jeremy !

First of all, thank you for your brilliant work, it's great.

I'm trying to use your editor to provide different type of objects in the same array (the objects are all based on the same base class)

To do this I have defined a schema using oneOf keyword, like that :

{
  "type": "array",
  "items": {
    "oneOf": [
      {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "BUILD",
              "REASSIGN"
            ]
          }
        }
      },
      {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "BREAK"
            ]
          },
            "property2" :{"type" : "string"}

        }
      }
    ]
  }
}

It is working at load and for the first change of object type. After that, in memory json seem to be "corrupted".

The object attributes are retained and appears in the UI even if they are not defined in the schema.

With that exemple :

You can try it using that link

I hope you have an idea, Thank you for your help,

Tony

jdorn commented 9 years ago

I just added a new option keep_oneof_values. It defaults to true to keep the current behavior, but you can set it to false to do what you want.

Set globally:

JSONEditor.defaults.options.keep_oneof_values = false;

Set per JSONEditor instance:

var editor = new JSONEditor(el, {
  schema: {...},
  keep_oneof_values: false
});

Set for a single oneOf instance:

{
  "options": {
    "keep_oneof_values": false
  },
  "oneOf": []
}
tgeorges commented 9 years ago

Hi and thank you Jeremy, I will try it this week.

TrevorPage commented 9 years ago

I've just tried out @tgeorges example with the additional keep_oneof_values property, and it seems to solve the problem.

@tgeorges example with modification added: link

Therefore I reckon this could be closed off!

Thank you for the fantastic library.