Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to add enum in templates , its not showing combo box for templates #473

Closed
sumancholleti opened this issue Oct 6, 2017 · 24 comments
Closed

Comments

@sumancholleti
Copy link

No description provided.

@josdejong
Copy link
Owner

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.

@sumancholleti
Copy link
Author

sumancholleti commented Oct 8, 2017

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

@josdejong
Copy link
Owner

Here you go:

http://jsbin.com/ciqifub/edit?html,output

@sumancholleti
Copy link
Author

Can json be an object instead?

@josdejong
Copy link
Owner

yes. just play around with it and you will see

@sumancholleti
Copy link
Author

I tried and it didn't work

@josdejong
Copy link
Owner

Keep trying :)

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

@sumancholleti
Copy link
Author

sumancholleti commented Oct 10, 2017

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);

@josdejong
Copy link
Owner

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.

@sumancholleti
Copy link
Author

I didn't get you

@josdejong
Copy link
Owner

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.

@ortrud
Copy link

ortrud commented Jan 17, 2018

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

@josdejong
Copy link
Owner

@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.

@SteveNeithardt
Copy link

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.

@josdejong
Copy link
Owner

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

@josdejong
Copy link
Owner

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

@chriscarreau
Copy link

chriscarreau commented Apr 11, 2019

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 😄

@josdejong
Copy link
Owner

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

@josdejong
Copy link
Owner

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

@yotamgod
Copy link

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"
        ]
      }
    }
  }
}

@josdejong
Copy link
Owner

@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 jsoneditor@9.1.3 to see whether your issue was accidentally fixed as a side effect?

@yotamgod
Copy link

Sadly it hasn't :(

@josdejong
Copy link
Owner

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

mpccolorado added a commit to mpccolorado/jsoneditor that referenced this issue Jul 23, 2021
josdejong pushed a commit that referenced this issue Jul 24, 2021
* Fixed issue #473. Enum works properly now

* Fixed issues with lint rules
@josdejong
Copy link
Owner

This should be fixed now in v9.5.3, see #1355.

If not fixed, please re-open this issue again.

andyquinterom pushed a commit to proyais/jsoneditor that referenced this issue Dec 7, 2021
* Fixed issue josdejong#473. Enum works properly now

* Fixed issues with lint rules
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants