1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Validation of credentials inputs.

This commit is contained in:
Antoine Aflalo 2014-05-16 10:34:23 +03:00
parent f822827454
commit 364f44451f

View File

@ -15,18 +15,24 @@
**/ **/
RED.editor = function() { RED.editor = function() {
var editing_node = null; var editing_node = null;
// TODO: should IMPORT/EXPORT get their own dialogs? // TODO: should IMPORT/EXPORT get their own dialogs?
function validateNodeProperties(node, definition, properties) {
var isValid = true;
for (var prop in definition) {
if (!validateNodeProperty(node, definition, prop, properties[prop])) {
isValid = false;
}
}
return isValid;
}
function validateNode(node) { function validateNode(node) {
var oldValue = node.valid; var oldValue = node.valid;
node.valid = true; node.valid = validateNodeProperties(node, node._def.defaults, node);
for (var d in node._def.defaults) { if (node._def._creds) {
if (!validateNodeProperty(node,d,node[d])) { node.valid = node.valid && validateNodeProperties(node, node._def.credentials, node._def._creds);
node.valid = false;
}
} }
if (node.valid != oldValue) { if (oldValue != node.valid) {
node.dirty = true; node.dirty = true;
} }
} }
@ -35,6 +41,7 @@ RED.editor = function() {
var dashedType = nodeType.replace(/\s+/g, '-'); var dashedType = nodeType.replace(/\s+/g, '-');
return 'credentials/' + dashedType + "/" + nodeID; return 'credentials/' + dashedType + "/" + nodeID;
} }
function sendCredentials(node,credDefinition,prefix) { function sendCredentials(node,credDefinition,prefix) {
var credentials = {}; var credentials = {};
for (var cred in credDefinition) { for (var cred in credDefinition) {
@ -45,6 +52,7 @@ RED.editor = function() {
} }
credentials[cred] = value; credentials[cred] = value;
} }
node._def._creds = credentials;
$.ajax({ $.ajax({
url: getCredentialsURL(node.type, node.id), url: getCredentialsURL(node.type, node.id),
type: 'POST', type: 'POST',
@ -52,17 +60,18 @@ RED.editor = function() {
success: function (result) { success: function (result) {
} }
}); });
} }
function validateNodeProperty(node,property,value) { function validateNodeProperty(node,definition,property,value) {
var valid = true; var valid = true;
if ("required" in node._def.defaults[property] && node._def.defaults[property].required) { if ("required" in definition[property] && definition[property].required) {
valid = value !== ""; valid = value !== "";
} }
if (valid && "validate" in node._def.defaults[property]) { if (valid && "validate" in definition[property]) {
valid = node._def.defaults[property].validate.call(node,value); valid = definition[property].validate.call(node,value);
} }
if (valid && node._def.defaults[property].type && RED.nodes.getType(node._def.defaults[property].type) && !("validate" in node._def.defaults[property])) { if (valid && definition[property].type && RED.nodes.getType(definition[property].type) && !("validate" in definition[property])) {
if (!value || value == "_ADD_") { if (!value || value == "_ADD_") {
valid = false; valid = false;
} else { } else {
@ -304,12 +313,13 @@ RED.editor = function() {
/** /**
* Add an on-change handler to revalidate a node field * Add an on-change handler to revalidate a node field
* @param node - the node being edited * @param node - the node being edited
* @param definition - the definition of the node
* @param property - the name of the field * @param property - the name of the field
* @param prefix - the prefix to use in the input element ids (node-input|node-config-input) * @param prefix - the prefix to use in the input element ids (node-input|node-config-input)
*/ */
function attachPropertyChangeHandler(node,property,prefix) { function attachPropertyChangeHandler(node,definition,property,prefix) {
$("#"+prefix+"-"+property).change(function() { $("#"+prefix+"-"+property).change(function() {
if (!validateNodeProperty(node, property,this.value)) { if (!validateNodeProperty(node, definition, property,this.value)) {
$(this).addClass("input-error"); $(this).addClass("input-error");
} else { } else {
$(this).removeClass("input-error"); $(this).removeClass("input-error");
@ -330,10 +340,9 @@ RED.editor = function() {
} else { } else {
preparePropertyEditor(node,d,prefix); preparePropertyEditor(node,d,prefix);
} }
attachPropertyChangeHandler(node,d,prefix); attachPropertyChangeHandler(node,definition.defaults,d,prefix);
} }
if (definition.credentials) { if (definition.credentials) {
// TODO: Validate credentials fields
$.getJSON(getCredentialsURL(node.type, node.id), function (data) { $.getJSON(getCredentialsURL(node.type, node.id), function (data) {
for (var cred in definition.credentials) { for (var cred in definition.credentials) {
@ -347,6 +356,10 @@ RED.editor = function() {
} else { } else {
preparePropertyEditor(data, cred, prefix); preparePropertyEditor(data, cred, prefix);
} }
attachPropertyChangeHandler(node, definition.credentials, cred, prefix);
for (var cred in definition.credentials) {
$("#" + prefix + "-" + cred).change();
}
} }
}); });
} }
@ -509,9 +522,10 @@ RED.editor = function() {
configTypeDef.oneditsave.call(RED.nodes.node(configId)); configTypeDef.oneditsave.call(RED.nodes.node(configId));
} }
validateNode(configNode); validateNode(configNode);
RED.view.dirty(true);
$( this ).dialog( "close" ); RED.view.dirty(true);
$(this).dialog("close");
} }
}, },
{ {