Commit d3ad8698 authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_jio: Use appropriate schema when validate the form

In a scenario with multiple forms for the same software type it is better store which schema was used to render the form.
parent 4d5a8610
...@@ -197,17 +197,22 @@ ...@@ -197,17 +197,22 @@
} }
gk gk
.declareMethod("getBaseUrl", function (url) {
var base_url, url_uri = URI(url);
base_url = url_uri.path().split("/");
base_url.pop();
base_url = url.split(url_uri.path())[0] + base_url.join("/");
return base_url;
})
.declareMethod("loadJSONSchema", function (url) { .declareMethod("loadJSONSchema", function (url) {
var gadget = this;
return getJSON(url) return getJSON(url)
.push(function (json) { .push(function (json) {
var base_url, url_uri; return gadget.getBaseUrl(url)
.push(function (base_url) {
url_uri = URI(url); return validateJSONSchema(json, base_url);
base_url = url_uri.path().split("/"); });
base_url.pop();
base_url = url.split(url_uri.path())[0] + base_url.join("/");
return validateJSONSchema(json, base_url);
}); });
}) })
...@@ -218,62 +223,22 @@ ...@@ -218,62 +223,22 @@
}); });
}) })
.declareMethod("validateJSONForSoftwareType", function (schema_url, software_type, generated_json) { .declareMethod("validateJSON", function (base_url, schema_url, generated_json) {
return getJSON(schema_url) var parameter_schema_url = schema_url;
.push(function (json) {
return JSON.parse(json);
})
.push(function (json_object) {
var parameter_schema_url,
st,
base_url,
url_uri = URI(schema_url);
for (st in json_object["software-type"]) {
if (json_object["software-type"].hasOwnProperty(st)) {
if (st === software_type) {
parameter_schema_url = json_object["software-type"][st].request;
}
}
}
if (URI(parameter_schema_url).protocol() === "") {
base_url = url_uri.path().split("/");
base_url.pop();
base_url = schema_url.split(url_uri.path())[0] + base_url.join("/");
if (base_url !== undefined) {
parameter_schema_url = base_url + "/" + parameter_schema_url;
}
}
return getJSON(parameter_schema_url)
.push(function (json) {
var schema = JSON.parse(json);
return expandSchema(schema, schema, base_url) if (URI(parameter_schema_url).protocol() === "") {
.push(function (loaded_json) { if (base_url !== undefined) {
return tv4.validateMultiple(generated_json, loaded_json); parameter_schema_url = base_url + "/" + parameter_schema_url;
}); }
}); }
});
})
.declareMethod("validateJSON", function (schema_url, generated_json) { return getJSON(parameter_schema_url)
return getJSON(schema_url)
.push(function (json) { .push(function (json) {
var base_url, var schema = JSON.parse(json);
url_uri = URI(schema_url),
schema = JSON.parse(json);
base_url = url_uri.path().split("/");
base_url.pop();
base_url = schema_url.split(url_uri.path())[0] + base_url.join("/");
return expandSchema(schema, schema, base_url) return expandSchema(schema, schema, base_url)
.push(function (loaded_schema) { .push(function (loaded_json) {
return tv4.validateMultiple(generated_json, loaded_schema); return tv4.validateMultiple(generated_json, loaded_json);
}); });
}); });
}); });
}(window, rJS, $, RSVP)); }(window, rJS, $, RSVP));
\ No newline at end of file
...@@ -230,7 +230,7 @@ ...@@ -230,7 +230,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>963.13311.58473.5341</string> </value> <value> <string>971.50940.46169.22476</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -248,7 +248,7 @@ ...@@ -248,7 +248,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1509706800.83</float> <float>1542815972.88</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -25,10 +25,9 @@ ...@@ -25,10 +25,9 @@
</div> </div>
<div class="field" title="serialisation_type"> <div class="field" title="serialisation_type">
<div class="input"> <div class="input">
<input type=hidden name="serialisation_type" class="slapos-serialisation-type"></input> <input type=hidden name="serialisation_type" class="slapos-serialisation-type">
</div> </div>
</div> </div>
</div>
<div class="field" title="hide_show_button"> <div class="field" title="hide_show_button">
<div class="input"> <div class="input">
<button type="button" class="slapos-show-form hidden-button"> Show Parameter Form </button> <button type="button" class="slapos-show-form hidden-button"> Show Parameter Form </button>
...@@ -39,8 +38,9 @@ ...@@ -39,8 +38,9 @@
<fieldset id="parameter-main"> </fieldset> <fieldset id="parameter-main"> </fieldset>
<fieldset id="parameter-optional"> </fieldset> <fieldset id="parameter-optional"> </fieldset>
<fieldset id="parameter-xml"> <fieldset id="parameter-xml">
<input type=hidden name="parameter_hash" class="parameter_hash_output"></input> <input type=hidden name="parameter_hash" class="parameter_hash_output">
<input type=hidden name="shared" class="parameter_shared"></input> <input type=hidden name="shared" class="parameter_shared">
<input type=hidden name="schema_url" class="parameter_schema_url">
</fieldset> </fieldset>
</div> </div>
<div class="loadschema" <div class="loadschema"
......
...@@ -232,7 +232,7 @@ ...@@ -232,7 +232,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>963.13401.52011.27084</string> </value> <value> <string>971.50899.9801.15428</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -250,7 +250,7 @@ ...@@ -250,7 +250,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1512998042.09</float> <float>1542813409.18</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -475,6 +475,16 @@ ...@@ -475,6 +475,16 @@
return ""; return "";
} }
function getSchemaUrlFromForm(element) {
var input = element.querySelector(".parameter_schema_url");
if (input !== undefined && input !== null) {
return input.value;
}
return "";
}
gk.declareMethod("loadJSONSchema", function (url) { gk.declareMethod("loadJSONSchema", function (url) {
return this.getDeclaredGadget('loadschema') return this.getDeclaredGadget('loadschema')
.push(function (gadget) { .push(function (gadget) {
...@@ -482,13 +492,19 @@ ...@@ -482,13 +492,19 @@
}); });
}) })
.declareMethod("validateJSONForSoftwareType", function (schema_url, softwaretype, generated_json) { .declareMethod("validateJSON", function (base_url, schema_url, generated_json) {
return this.getDeclaredGadget('loadschema') return this.getDeclaredGadget('loadschema')
.push(function (gadget) { .push(function (gadget) {
return gadget.validateJSONForSoftwareType(schema_url, softwaretype, generated_json); return gadget.validateJSON(base_url, schema_url, generated_json);
}); });
}) })
.declareMethod("getBaseUrl", function (url) {
return this.getDeclaredGadget('loadschema')
.push(function (gadget) {
return gadget.getBaseUrl(url);
});
})
.declareMethod("loadSoftwareJSON", function (url) { .declareMethod("loadSoftwareJSON", function (url) {
return this.getDeclaredGadget('loadschema') return this.getDeclaredGadget('loadschema')
.push(function (gadget) { .push(function (gadget) {
...@@ -500,7 +516,9 @@ ...@@ -500,7 +516,9 @@
var g = this, var g = this,
software_type = getSoftwareTypeFromForm(g.element), software_type = getSoftwareTypeFromForm(g.element),
json_dict = getFormValuesAsJSONDict(g.element), json_dict = getFormValuesAsJSONDict(g.element),
schema_url = getSchemaUrlFromForm(g.element),
serialisation_type = getSerialisationTypeFromForm(g.element); serialisation_type = getSerialisationTypeFromForm(g.element);
if (software_type === "") { if (software_type === "") {
if (g.options.value.parameter.shared) { if (g.options.value.parameter.shared) {
throw new Error("The software type is not part of the json (" + software_type + " as slave)"); throw new Error("The software type is not part of the json (" + software_type + " as slave)");
...@@ -508,7 +526,10 @@ ...@@ -508,7 +526,10 @@
throw new Error("The software type is not part of the json (" + software_type + ")"); throw new Error("The software type is not part of the json (" + software_type + ")");
} }
return g.validateJSONForSoftwareType(json_url, software_type, json_dict) return g.getBaseUrl(json_url)
.push(function (base_url) {
return g.validateJSON(base_url, json_url, json_dict);
})
.push(function (validation) { .push(function (validation) {
var error_index, var error_index,
parameter_hash_input = g.element.querySelectorAll('.parameter_hash_output')[0], parameter_hash_input = g.element.querySelectorAll('.parameter_hash_output')[0],
...@@ -693,6 +714,7 @@ ...@@ -693,6 +714,7 @@
simplified_only = options.value.parameter.simplified_only, simplified_only = options.value.parameter.simplified_only,
input = gadget.element.querySelector('select.slapos-software-type'), input = gadget.element.querySelector('select.slapos-software-type'),
parameter_shared = gadget.element.querySelector('input.parameter_shared'), parameter_shared = gadget.element.querySelector('input.parameter_shared'),
parameter_schema_url = gadget.element.querySelector('input.parameter_schema_url'),
s_input = gadget.element.querySelector('input.slapos-serialisation-type'), s_input = gadget.element.querySelector('input.slapos-serialisation-type'),
selection_option_list = []; selection_option_list = [];
...@@ -796,7 +818,10 @@ ...@@ -796,7 +818,10 @@
options.serialisation = json.serialisation; options.serialisation = json.serialisation;
} }
return json['software-type'][option_selected_index].request; // Save current schema on the field
parameter_schema_url.value = json['software-type'][option_selected_index].request;
return parameter_schema_url.value;
}) })
.push(function (parameter_json_schema_url) { .push(function (parameter_json_schema_url) {
var parameter_dict = {}, json_url_uri, prefix, parameter_entry, var parameter_dict = {}, json_url_uri, prefix, parameter_entry,
......
...@@ -274,7 +274,7 @@ ...@@ -274,7 +274,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>971.49870.55173.61764</string> </value> <value> <string>971.50926.27080.61815</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -292,7 +292,7 @@ ...@@ -292,7 +292,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1542753152.17</float> <float>1542815548.03</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
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