jdorn / json-editor

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

Setting JSONSchema v3 or v4 as schema gives strange results #234

Open mtrycz opened 10 years ago

mtrycz commented 10 years ago

Hello, I was giving json-editor a try, the demo looks very nice. I have two different requirements, displaying predefined views, for which json-editor looks like the way to go, and secondly constructing an editor for defining (those) views. It would be precious if we could use the same technology for both.

For the second I thought I'd be able to make an instance of json-editor loaded with the JSONSchema, so that users could define valid schemas/views. I'd expect there will be some customizations to do, but thought this would be a good starting point. Unfortunately this approach isn't working. JSONSchema v3 just won't load, while v4 loads but is having problems with multiple fields.

Or maybe there is a different, separate schema for json-editor? Or is the schema support not complete yet? If we end up using json-editor, I could probably contribute code toward this functionality.

Reproducibility: Open the demo.html and paste the schema (v3 or v4) in the schema section; or create a new schema with the http://json-schema.org/draft-0X/schema referenced.

Thanks in advance, Martin

mtrycz commented 10 years ago

I have been fiddling a bit with my use case. I defined a simplified "meta-form" schema. With this schema I can define simple forms. It's a proof of concept, really. It is represented here. I wanted to have a two-pane page with the form-editor on one side and the render of the form in it's current form on the other side, but haven't had time to do that yet (should be trivial, tho).

The "meta-form" schema isn't actually valid, because the "properties" keyword represents an array of objects, instead of an object with key/value pairs. I could not find a way of achieving this in JSONSchema. I convert this non-standard output into standard JSON with a simple function:

      function convert(json) {
        var properties_object = {};
        json.properties.forEach(function(el, index) {
          properties_object[el.title] = el;
        });
        json.properties = properties_object;
        return json;
      }

There are obviously many hard edges (like, many), but it's as a proof of concept it seems to work. To my surprise, the json still seems to work even if it's not converted to standard JSONSchema (ie. if properties are left as an array).

I was wondering if there is aready a functionality in json-editor to show/hide form fields based on what is already put in (like showing the "format" field when "type" is "string", or having "properties" only for "object" types). Clearly the "dependencies" are not enough.

I'll keep updating this issue, if anyone's interested. I'll probably make a pull request for a demo page with this functionality when it's ready. Cheers

robertlevy commented 10 years ago

also very interested in this same use case

jdorn commented 10 years ago

You can do this with JSON Schema in a limited fashion using oneOf and enum, but it gets complicated quickly and doesn't handle super complex use cases. Here's an example:

{
  "oneOf": [
    {
      "title": "object",
      "$ref": "#/definitions/schema-type",
      "required": ["properties"],
      "properties": {
        "type": {"enum": ["object"]},
        "properties": {"type": "object"}
      }
    },
    {
      "title": "string",
      "$ref": "#/definitions/schema-type",
      "properties": {
        "type": {"enum": ["string"]},
        "format": {"type": "string"}
      }
    },
    {
      "title": "number",
      "$ref": "#/definitions/schema-type",
      "properties": {
        "type": {"enum": ["number"]}
      }
    }
  ],
  "definitions": {
    "schema-type": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type"],
      "properties": {
        "title": {"type": "string"},
        "description": {"type": "string", "format": "textarea"},
        "type": {"type": "string", "options": {"hidden": true}}
      }
    }
  }
}

Due to bug #183 this schema won't work in JSON Editor. I plan to fix this soon, but in the mean time if you use a property name other than "type", it should work fine.

jdorn commented 10 years ago

Bug #183 is fixed, so the above example should work.

The reason that the draft schemas don't work is they rely on the anyOf keyword, which is not supported in JSON Editor. I couldn't think of a good UI for anyOf, but I'm open to suggestions. This is the only example of one I could find and it's pretty clunky - http://jsonary.com/documentation/json-schema/?section=keywords/General%20keywords#keywords/General%20keywords/04%20-%20anyOf

oleics commented 9 years ago

A schema to create schemas. If anyone else needs this, I can share updates to this via a gist.

http://goo.gl/cnPUiC

EDIT: Its nicer with bootstrap3 http://goo.gl/58UEJg

marctc commented 7 years ago

@oleics links are dead. Can you share again?