Commit b9cb557d authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_web: expand json schema before use it for validation.

parent 640ed520
...@@ -132,7 +132,7 @@ ...@@ -132,7 +132,7 @@
<fieldset id="parameter-main"> </fieldset>\n <fieldset id="parameter-main"> </fieldset>\n
<fieldset id="parameter-optional"> </fieldset>\n <fieldset id="parameter-optional"> </fieldset>\n
<fieldset id="parameter-xml"> \n <fieldset id="parameter-xml"> \n
<textarea name="your_parameter_xml" class="parameter_xml_output"></textarea>\n <input type=hidden name="field_your_instance_xml" class="parameter_xml_output"></input>\n
</fieldset>\n </fieldset>\n
\n \n
</div>\n </div>\n
...@@ -278,7 +278,7 @@ ...@@ -278,7 +278,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>941.38608.48931.35464</string> </value> <value> <string>941.46746.34379.16640</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -296,7 +296,7 @@ ...@@ -296,7 +296,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1426523723.07</float> <float>1426586570.59</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -102,8 +102,8 @@ ...@@ -102,8 +102,8 @@
<value> <string encoding="cdata"><![CDATA[ <value> <string encoding="cdata"><![CDATA[
/*jslint nomen: true, maxlen: 200, indent: 2*/\n /*jslint nomen: true, maxlen: 200, indent: 2*/\n
/*global window, rJS, console, RSVP, jQuery, jIO, tv4, JSON, $ */\n /*global window, rJS, console, RSVP, jQuery, jIO, tv4, URI, JSON, $ */\n
(function (window, rJS, $, RSVP) {\n (function (window, rJS, $, URI, RSVP) {\n
"use strict";\n "use strict";\n
\n \n
var gk = rJS(window);\n var gk = rJS(window);\n
...@@ -131,7 +131,7 @@ ...@@ -131,7 +131,7 @@
return schema;\n return schema;\n
}\n }\n
\n \n
function resolveReference(partial_schema, schema) {\n function resolveReference(partial_schema, schema, base_url) {\n
var parts,\n var parts,\n
external_schema,\n external_schema,\n
ref = partial_schema.$ref;\n ref = partial_schema.$ref;\n
...@@ -149,6 +149,11 @@ ...@@ -149,6 +149,11 @@
}\n }\n
\n \n
return RSVP.Queue().push(function () {\n return RSVP.Queue().push(function () {\n
if (URI(ref).protocol() === "") {\n
if (base_url !== undefined) {\n
ref = base_url + "/" + ref;\n
}\n
}\n
return getJSON(ref);\n return getJSON(ref);\n
})\n })\n
.push(function (json) {\n .push(function (json) {\n
...@@ -164,7 +169,7 @@ ...@@ -164,7 +169,7 @@
}\n }\n
\n \n
// Inspired from https://github.com/nexedi/dream/blob/master/dream/platform/src/jsplumb/jsplumb.js#L398\n // Inspired from https://github.com/nexedi/dream/blob/master/dream/platform/src/jsplumb/jsplumb.js#L398\n
function expandSchema(json_schema, full_schema) {\n function expandSchema(json_schema, full_schema, base_url) {\n
var i,\n var i,\n
expanded_json_schema = clone(json_schema) || {};\n expanded_json_schema = clone(json_schema) || {};\n
\n \n
...@@ -176,12 +181,14 @@ ...@@ -176,12 +181,14 @@
if (json_schema.$ref) {\n if (json_schema.$ref) {\n
return resolveReference(\n return resolveReference(\n
json_schema,\n json_schema,\n
full_schema\n full_schema,\n
base_url\n
)\n )\n
.push(function (remote_schema) {\n .push(function (remote_schema) {\n
return expandSchema(\n return expandSchema(\n
remote_schema,\n remote_schema,\n
full_schema\n full_schema,\n
base_url\n
);\n );\n
}).push(function (referencedx) {\n }).push(function (referencedx) {\n
$.extend(expanded_json_schema, referencedx);\n $.extend(expanded_json_schema, referencedx);\n
...@@ -197,12 +204,14 @@ ...@@ -197,12 +204,14 @@
function wrapperResolveReference(p) {\n function wrapperResolveReference(p) {\n
return resolveReference(\n return resolveReference(\n
json_schema.properties[p],\n json_schema.properties[p],\n
full_schema\n full_schema,\n
base_url\n
).push(function (external_schema) {\n ).push(function (external_schema) {\n
// console.log(p);\n // console.log(p);\n
return expandSchema(\n return expandSchema(\n
external_schema,\n external_schema,\n
full_schema\n full_schema,\n
base_url\n
)\n )\n
.push(function (referencedx) {\n .push(function (referencedx) {\n
$.extend(expanded_json_schema.properties[p], referencedx);\n $.extend(expanded_json_schema.properties[p], referencedx);\n
...@@ -231,7 +240,8 @@ ...@@ -231,7 +240,8 @@
function wrapperExpandSchema(p) {\n function wrapperExpandSchema(p) {\n
return expandSchema(\n return expandSchema(\n
json_schema.allOf[p],\n json_schema.allOf[p],\n
full_schema\n full_schema,\n
base_url\n
).push(function (referencedx) {\n ).push(function (referencedx) {\n
if (referencedx.properties) {\n if (referencedx.properties) {\n
$.extend(\n $.extend(\n
...@@ -266,7 +276,7 @@ ...@@ -266,7 +276,7 @@
return getJSON("../../renderjs/slapos_load_meta_schema.json");\n return getJSON("../../renderjs/slapos_load_meta_schema.json");\n
}\n }\n
\n \n
function validateJSONSchema(json) {\n function validateJSONSchema(json, base_url) {\n
return getMetaJSONSchema()\n return getMetaJSONSchema()\n
.then(function (meta_schema) {\n .then(function (meta_schema) {\n
if (!tv4.validate(json, meta_schema)) {\n if (!tv4.validate(json, meta_schema)) {\n
...@@ -275,7 +285,7 @@ ...@@ -275,7 +285,7 @@
return JSON.parse(json);\n return JSON.parse(json);\n
})\n })\n
.then(function (schema) {\n .then(function (schema) {\n
return expandSchema(schema, schema);\n return expandSchema(schema, schema, base_url);\n
});\n });\n
}\n }\n
\n \n
...@@ -283,7 +293,14 @@ ...@@ -283,7 +293,14 @@
.declareMethod("loadJSONSchema", function (url) {\n .declareMethod("loadJSONSchema", function (url) {\n
return getJSON(url)\n return getJSON(url)\n
.then(function (json) {\n .then(function (json) {\n
return validateJSONSchema(json);\n var base_url, url_uri;\n
\n
url_uri = URI(url);\n
base_url = url_uri.path().split("/");\n
base_url.pop();\n
base_url = url.split(url_uri.path())[0] + base_url.join("/");\n
\n
return validateJSONSchema(json, base_url);\n
});\n });\n
})\n })\n
\n \n
...@@ -317,11 +334,22 @@ ...@@ -317,11 +334,22 @@
.declareMethod("validateJSON", function (schema_url, generated_json) {\n .declareMethod("validateJSON", function (schema_url, generated_json) {\n
return getJSON(schema_url)\n return getJSON(schema_url)\n
.then(function (json) {\n .then(function (json) {\n
return tv4.validateMultiple(generated_json, JSON.parse(json));\n var base_url,\n
url_uri = URI(schema_url),\n
schema = JSON.parse(json);\n
\n
base_url = url_uri.path().split("/");\n
base_url.pop();\n
base_url = schema_url.split(url_uri.path())[0] + base_url.join("/");\n
\n
return expandSchema(schema, schema, base_url)\n
.push(function (loaded_schema) {\n
return tv4.validateMultiple(generated_json, loaded_schema);\n
});\n
});\n });\n
});\n });\n
\n \n
}(window, rJS, $, RSVP)); }(window, rJS, $, RSVP, URI));
]]></string> </value> ]]></string> </value>
</item> </item>
...@@ -458,7 +486,7 @@ ...@@ -458,7 +486,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>941.38337.40987.563</string> </value> <value> <string>941.46927.41646.58504</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -476,7 +504,7 @@ ...@@ -476,7 +504,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1426092715.19</float> <float>1426597461.76</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -246,7 +246,7 @@ ...@@ -246,7 +246,7 @@
return g.validateJSON(json_url, json_dict)\n return g.validateJSON(json_url, json_dict)\n
.push(function (validation) {\n .push(function (validation) {\n
var error_index,\n var error_index,\n
parameter_textarea = g.props.element.querySelectorAll(\'.parameter_xml_output\')[0],\n parameter_input = g.props.element.querySelectorAll(\'.parameter_xml_output\')[0],\n
field_name,\n field_name,\n
div,\n div,\n
divm,\n divm,\n
...@@ -262,7 +262,7 @@ ...@@ -262,7 +262,7 @@
});\n });\n
\n \n
if (validation.valid) {\n if (validation.valid) {\n
parameter_textarea.innerText = jsonDictToParameterXML(json_dict);\n parameter_input.value = jsonDictToParameterXML(json_dict);\n
} else {\n } else {\n
for (error_index in validation.errors) {\n for (error_index in validation.errors) {\n
if (validation.errors.hasOwnProperty(error_index)) {\n if (validation.errors.hasOwnProperty(error_index)) {\n
...@@ -281,7 +281,7 @@ ...@@ -281,7 +281,7 @@
divm.querySelector("span.error").textContent = validation.missing[missing_index].message;\n divm.querySelector("span.error").textContent = validation.missing[missing_index].message;\n
}\n }\n
}\n }\n
parameter_textarea.innerText = "ERROR";\n parameter_input.value = "ERROR";\n
}\n }\n
console.log(jsonDictToParameterXML(json_dict));\n console.log(jsonDictToParameterXML(json_dict));\n
return false;\n return false;\n
...@@ -472,6 +472,7 @@ ...@@ -472,6 +472,7 @@
);\n );\n
})\n })\n
.fail(function (error) {\n .fail(function (error) {\n
console.error(error.stack);\n
return g.renderFailoverTextArea(\'\', error.toString());\n return g.renderFailoverTextArea(\'\', error.toString());\n
});\n });\n
});\n });\n
...@@ -613,7 +614,7 @@ ...@@ -613,7 +614,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>941.45697.33456.18551</string> </value> <value> <string>941.46904.21655.48418</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -631,7 +632,7 @@ ...@@ -631,7 +632,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1426523636.72</float> <float>1426596317.01</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