Fixed #285: an issue with the enum drop down when having defined multiple enums in a JSON schema

This commit is contained in:
jos 2016-05-22 20:14:50 +02:00
parent 60b7b17511
commit b922aceb07
2 changed files with 53 additions and 45 deletions

View File

@ -3,6 +3,12 @@
https://github.com/josdejong/jsoneditor https://github.com/josdejong/jsoneditor
## 2016-05-22, version 5.5.4
- Fixed #285: an issue with the enum drop down when having defined multiple
enums in a JSON schema.
## 2016-05-22, version 5.5.3 ## 2016-05-22, version 5.5.3
- Fixed #299: reverted the fix of #268 by trimming text in fields and values. - Fixed #299: reverted the fix of #268 by trimming text in fields and values.

View File

@ -1277,10 +1277,10 @@ Node.prototype._updateDomValue = function () {
this.dom.select.appendChild(this.dom.select.option); this.dom.select.appendChild(this.dom.select.option);
//Iterate all enum values and add them as options //Iterate all enum values and add them as options
for(var i = 0; i < this.enum.enum.length; i++) { for(var i = 0; i < this.enum.length; i++) {
this.dom.select.option = document.createElement('option'); this.dom.select.option = document.createElement('option');
this.dom.select.option.value = this.enum.enum[i]; this.dom.select.option.value = this.enum[i];
this.dom.select.option.innerHTML = this.enum.enum[i]; this.dom.select.option.innerHTML = this.enum[i];
if(this.dom.select.option.value == this.value){ if(this.dom.select.option.value == this.value){
this.dom.select.option.selected = true; this.dom.select.option.selected = true;
} }
@ -1983,59 +1983,61 @@ Node.prototype.updateDom = function (options) {
Node.prototype._updateSchema = function () { Node.prototype._updateSchema = function () {
//Locating the schema of the node and checking for any enum type //Locating the schema of the node and checking for any enum type
if(this.editor && this.editor.options) { if(this.editor && this.editor.options) {
var field = (this.index != undefined) ? this.index : this.field; // find the part of the json schema matching this nodes path
this.schema = Node._findSchema(this.editor.options.schema, this.getPath());
//Search for the schema element of the current node and store it in the schema attribute. if (this.schema) {
//Hereafter, wherever you have access in the node you will have also access in its own schema. this.enum = Node._findEnum(this.schema);
this.schema = this._getJsonObject(this.editor.options.schema, 'name', field)[0];
if(!this.schema) {
this.schema = this._getJsonObject(this.editor.options.schema, field)[0];
} }
else {
//Search for any enumeration type in the schema of the current node.
//Enum types can be also be part of a composite type.
if(this.schema){
if(this.schema.hasOwnProperty('enum')){
this.enum = {};
this.enum.enum = this.schema.enum;
} else if(this.schema.hasOwnProperty('oneOf')){
this.enum = this._getJsonObject(this.schema.oneOf, 'enum')[0];
} else if(this.schema.hasOwnProperty('anyOf')){
this.enum = this._getJsonObject(this.schema.anyOf, 'enum')[0];
} else if(this.schema.hasOwnProperty('allOf')){
this.enum = this._getJsonObject(this.schema.allOf, 'enum')[0];
} else {
delete this.enum;
}
} else {
delete this.enum; delete this.enum;
} }
} }
}; };
/** /**
* Get all sub-elements of the given object with the specified key and value. * find an enum definition in a JSON schema, as property `enum` or inside
* one of the schemas composites (`oneOf`, `anyOf`, `allOf`)
* @param {Object} schema
* @return {Array | null} Returns the enum when found, null otherwise.
* @private * @private
*/ */
Node.prototype._getJsonObject = function (obj, key, val) { Node._findEnum = function (schema) {
var objects = []; if (schema.enum) {
for (var i in obj) { return schema.enum;
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
if(i === key && val === undefined){
if(Array.isArray(obj[i])) {
objects.push(obj);
} else {
objects.push(obj[i]);
} }
} else {
objects = objects.concat(this._getJsonObject(obj[i], key, val)); var composite = schema.oneOf || schema.anyOf || schema.allOf;
} if (composite) {
} else if (i == key && obj[key] == val) { var match = composite.filter(function (entry) {return entry.enum});
objects.push(obj); if (match.length > 0) {
return match[0].enum;
} }
} }
return objects;
return null
};
/**
* Return the part of a JSON schema matching given path.
* @param {Object} schema
* @param {Array.<string | number>} path
* @return {Object | null}
* @private
*/
Node._findSchema = function (schema, path) {
var childSchema = schema;
for (var i = 0; i < path.length && childSchema; i++) {
var key = path[i];
if (typeof key === 'string' && childSchema.properties) {
childSchema = childSchema.properties[key] || null
}
else if (typeof key === 'number' && childSchema.items) {
childSchema = childSchema.items
}
}
return childSchema
}; };
/** /**