Jsoneditor: how to add enum in templates , its not showing combo box for templates

Created on 6 Oct 2017  路  23Comments  路  Source: josdejong/jsoneditor

bug help wanted

All 23 comments

These enum dropdowns are a feature that you can use by providing a JSON Schema enumeration, so you could use it in combination with a template.

var schema = {
        "title": "Example Composite Schema",
        "oneOf": [
            {
            "type": "object",
            "properties": {
                "enumerable_prop": {
                    "type": "string",
                  "enum": ["first", "second", "third"]
              }
            }
          },
          {
            "type": "array"
          }
        ]
      };

var options = {
name: 'UconnectMessage',
schema: schema,
templates: [
    {
        text: 'text',
        title: 'Insert a Message',
        className: 'any',
        field: 'notify,
        value: {
            enumerable_prop:null
        }
    }

Can you give an example, I tried this , its not showing dropdown

Can json be an object instead?

yes. just play around with it and you will see

I tried and it didn't work

Keep trying :)

You will have to adjust both JSON and JSON schema, these need to be aligned with each other.

No luck yet
var schema = {
"type": "object",
"properties": {
"gender": {
"enum": ["male", "female"]
}
}
};

var json = {};


var templates = [
  {
    text: 'Employee',
    title: 'Insert a new employee',
    className: 'any',
    field: 'employee',
    value: {
      gender: '',
    }
  }
]

var options = {
  schema: schema,
  templates: templates
};

// create the editor
var container = document.getElementById('jsoneditor');
var editor = new JSONEditor(container, options, json);

I think the difficulty here is that the enum dropdown only works when the JSON matches the JSONSchema, whilst a template can be inserted everywhere. In your example code the enum for gender only works for a property in the root object, and not for nested objects. When inserting an employee via the template, you create a nested object - which is not matching the JSON Schema enum.

I didn't get you

The schema that you post:

var schema = {
  "type": "object",
    "properties": {
    "gender": {
      "enum": ["male", "female"]
    }
  }
};

will only create a dropdown for the field gender in the root of the JSON object like:

{
  "gender": "male"
}

It will NOT create a dropdown not for nested objects like here:

{
  "someNestedObject": {
    "gender": "male"
  }
}

if you need that, you will need to adjust your JSON Schema to have the definition of gender there where it will be created in your JSON object. If you want a dropdown for gender everywhere in the object (on all levels), you will have to define a JSON Schema with recursion like discussed here.

I have followed this conversation and tried everything you suggested, but I can not get a dropdown to show up anywhere but on the top level of the schema for an enum property like gender.

let demographicsSchema = {
    "type": "object",
        "properties": {
            "firstName": { "type": "string", "minLength" : 1 },
            //"lastName": { "type" : ["string","null"] },
            //"gender": { "enum": ["Male", "Female"] },
            "lastName": { "type" : "string" },
        "gender" : {"$ref" : "gender"  } ,
            "age": { "type": "number", "minimum" : 0, "default" : 40},
            "maritalStatus": { "type": "string", "enum" : ["M","S","DV","WD","MA","NM","SE"] },
            "cin": { "type": "string" },
            "sufficientInfoForCIN": { "type": "string" },
            "middleNameOrInitial": { "type": "string" },
            "nameSuffix": { "type": "string" },
            "nameVerified": { "type": "string" },
            "ssn": { "type": "string", minLength:9, maxLength:9 },
            "ssnStatus": { "type": "string", "default" : "VR" },
    },
    "required": ["firstName", "gender", "age"]
};

let genderSchema = {
    "type": "string",
        "enum": ["Male", "Female"] 
};

Do you have plans to make dropdown available on any level?
Also, what is the relation between jsoneditor and json-editor projects. I like jsoneditir, but json-editor does handle enums a sdropdowns on any level of the schema

@ortrud have you tried the example that I posted here? http://jsbin.com/ciqifub/edit?html,output

There is no relation between jsoneditor and json-editor except that they are both editors for JSON :)

If your json schema defines enums on any level, then JSONEditor will display them on any level - that's a matter of how you define your json schema.

screenshot_2018-08-22 js bin

I have the same issue, where the enums drop downs never seem to appear within objects in arrays.

Here's the screenshot of the example you posted on jsbin.

Hm, that's odd. Will look into it, thanks for reporting @SteveNeithardt.

I think this issue is fixed via #624, will close it now. If there is still an issue please let me know.

Hi @josdejong , I'm still running into this issue (or a similar one) in the current version (5.32.4) but only in the specific case where my schema has an object with additionalProperties having an enum.

Here's an example: https://jsbin.com/vuyabuyiji/edit?html,output

image
image

For the error to show up you have to add additional properties under the "testObj" object.

As you can see in this example, the validation works, telling me I have to choose one of the enum values, but the combobox/select does not appear for the addtional properties.

Thank you for looking into it 馃槃

@chriscarreau thanks for reporting (with a reproducible jsbin). Not sure, but I think the enum dropdowns don't (yet) reckon with additionalProperties.

Someone interested in looking into rendering an enum dropdown for additionalProperties?

I was wondering if dropdowns for schemas with additionalProperties have been addressed yet?
I don't get a dropdown for enum properties inside an additionalProperties in version 9.1.2.
My schema is as follows:

{
  "type": "object",
  "title": "",
  "description": "",
  "default": {},
  "additionalProperties": {
    "type": "object",
    "properties": {
      "day": {
        "type": "string",
        "enum": [
          "sunday",
          "monday",
          "tuesday"
        ]
      }
    }
  }
}

@yotamgod I'm not sure. I've just published v9.1.3 which contains a fix by @maufl which _may_ be related, see #1158 and #1161. Can you try out [email protected] to see whether your issue was accidentally fixed as a side effect?

Sadly it hasn't :(

Thanks for checking 馃憤 . So we still have to do some debugging here.

Was this page helpful?
0 / 5 - 0 ratings