Commit 76541c25 authored by Boris Kocherov's avatar Boris Kocherov

partial add ignore_incorrect option.

currently realized only for enum and object
parent ee725769
......@@ -995,7 +995,8 @@
.push(function (value) {
return gadget.rerender({
schema: opt.schema,
value: value
value: value,
ignore_incorrect: opt.ignore_incorrect
})
.push(function () {
return gadget.reValidate(value, opt.schema);
......
......@@ -125,20 +125,21 @@
return schema;
}
function render_enum(schema, json_document) {
function render_enum(g, schema, json_document) {
var input = document.createElement("select"),
option,
i,
ser_value,
selected = false,
empty_option,
enum_arr = schema['enum'];
input.size = 1;
option = document.createElement("option");
option.value = "";
empty_option = document.createElement("option");
empty_option.value = "";
if (json_document === undefined) {
option.selected = true;
empty_option.selected = true;
}
input.appendChild(option);
input.appendChild(empty_option);
for (i = 0; i < enum_arr.length; i += 1) {
option = document.createElement("option");
ser_value = JSON.stringify(enum_arr[i]);
......@@ -155,6 +156,10 @@
input.appendChild(option);
}
if (json_document !== undefined && !selected) {
if (g.props.ignore_incorrect) {
empty_option.selected = true;
g.props.changed = true;
} else {
// save original json_document even if it
// not support with schema
// XXX element should be removed on first user interact
......@@ -169,11 +174,13 @@
option.selected = true;
input.appendChild(option);
}
}
return input;
}
function render_enum_with_title(schema_arr, json_document, selected_schema) {
function render_enum_with_title(g, schema_arr, json_document, selected_schema) {
var input = document.createElement("select"),
empty_option,
option,
i,
ser_value,
......@@ -182,12 +189,12 @@
if (json_document === undefined && selected_schema !== undefined) {
json_document = selected_schema.schema.const;
}
option = document.createElement("option");
option.value = "";
empty_option = document.createElement("option");
empty_option.value = "";
if (json_document === undefined) {
option.selected = true;
empty_option.selected = true;
}
input.appendChild(option);
input.appendChild(empty_option);
for (i = 0; i < schema_arr.length; i += 1) {
option = document.createElement("option");
// XXX use number id for speedup
......@@ -207,6 +214,10 @@
input.appendChild(option);
}
if (json_document !== undefined && !selected) {
if (g.props.ignore_incorrect) {
empty_option.selected = true;
g.props.changed = true;
} else {
// save original json_document even if it
// not support with schema
// XXX element should be removed on first user interact
......@@ -221,10 +232,11 @@
option.selected = true;
input.appendChild(option);
}
}
return input;
}
function render_boolean(json_document) {
function render_boolean(g, json_document) {
var input,
schema_for_selection = {
type: "boolean",
......@@ -237,7 +249,7 @@
if (json_document === "false") {
json_document = false;
}
input = render_enum(schema_for_selection, json_document);
input = render_enum(g, schema_for_selection, json_document);
input.setAttribute('data-json-type', "boolean");
return input;
}
......@@ -333,6 +345,7 @@
document: options.json_document,
display_label: options.parent_type !== "array",
saveOrigValue: g.props.saveOrigValue,
ignore_incorrect: g.props.ignore_incorrect,
scope: scope
})
.push(function () {
......@@ -994,13 +1007,13 @@
// render input begin
if (!input && schema_arr[0].is_arr_of_const && schema_arr.length > 1) {
input = render_enum_with_title(schema_arr, json_document, options.selected_schema);
input = render_enum_with_title(gadget, schema_arr, json_document, options.selected_schema);
}
if (!input && schema.const !== undefined) {
input = render_const(gadget, schema, json_document);
}
if (!input && schema.enum !== undefined) {
input = render_enum(schema, json_document);
input = render_enum(gadget, schema, json_document);
// XXX take in account existing type with enum
type_changed = false;
}
......@@ -1010,7 +1023,7 @@
}
if (!input && type === "boolean") {
input = render_boolean(json_document);
input = render_boolean(gadget, json_document);
}
if (!input && ["string", "integer", "number", "null"].indexOf(type) >= 0) {
......@@ -1560,6 +1573,10 @@
return queue;
})
.push(function () {
if (g.props.ignore_incorrect) {
g.props.changed = true;
return;
}
var key,
queue = RSVP.Queue();
for (key in json_document) {
......@@ -2058,6 +2075,8 @@
}
g.props.property_name = property_name;
g.props.schema_arr = options.schema_arr;
// XXX realized only for enum and object
g.props.ignore_incorrect = options.ignore_incorrect;
g.props.render_opt = {
type: options.type,
selected_schema: options.selected_schema,
......@@ -2072,6 +2091,9 @@
var g = this,
for_delete,
root = g.element.querySelector('[data-json-path="/"]');
if (opt.ignore_incorrect !== undefined) {
g.props.ignore_incorrect = opt.ignore_incorrect;
}
g.props.changed = false;
g.props.inputs = [];
g.props.add_buttons = [];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment