mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
@@ -115,8 +115,9 @@ RED.editor = (function() {
|
||||
var valid = validateNodeProperty(node, definition, prop, properties[prop]);
|
||||
if ((typeof valid) === "string") {
|
||||
result.push(valid);
|
||||
}
|
||||
else if(!valid) {
|
||||
} else if (Array.isArray(valid)) {
|
||||
result = result.concat(valid)
|
||||
} else if(!valid) {
|
||||
result.push(prop);
|
||||
}
|
||||
}
|
||||
@@ -165,7 +166,7 @@ RED.editor = (function() {
|
||||
// If the validator takes two arguments, it is a 3.x validator that
|
||||
// can return a String to mean 'invalid' and provide a reason
|
||||
if ((definition[property].validate.length === 2) &&
|
||||
((typeof valid) === "string")) {
|
||||
((typeof valid) === "string") || Array.isArray(valid)) {
|
||||
return valid;
|
||||
} else {
|
||||
// Otherwise, a 2.x returns a truth-like/false-like value that
|
||||
|
@@ -917,6 +917,51 @@ RED.utils = (function() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks a typed property is valid according to the type.
|
||||
* Returns true if valid.
|
||||
* Return String error message if invalid
|
||||
* @param {*} propertyType
|
||||
* @param {*} propertyValue
|
||||
* @returns true if valid, String if invalid
|
||||
*/
|
||||
function validateTypedProperty(propertyValue, propertyType, opt) {
|
||||
|
||||
let error
|
||||
if (propertyType === 'json') {
|
||||
try {
|
||||
JSON.parse(propertyValue);
|
||||
} catch(err) {
|
||||
error = RED._("validator.errors.invalid-json", {
|
||||
error: err.message
|
||||
})
|
||||
}
|
||||
} else if (propertyType === 'msg' || propertyType === 'flow' || propertyType === 'global' ) {
|
||||
if (!RED.utils.validatePropertyExpression(propertyValue)) {
|
||||
error = RED._("validator.errors.invalid-prop")
|
||||
}
|
||||
} else if (propertyType === 'num') {
|
||||
if (!/^[+-]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)?$/.test(propertyValue)) {
|
||||
error = RED._("validator.errors.invalid-num")
|
||||
}
|
||||
} else if (propertyType === 'jsonata') {
|
||||
try {
|
||||
jsonata(propertyValue)
|
||||
} catch(err) {
|
||||
error = RED._("validator.errors.invalid-expr", {
|
||||
error: err.message
|
||||
})
|
||||
}
|
||||
}
|
||||
if (error) {
|
||||
if (opt && opt.label) {
|
||||
return opt.label+': '+error
|
||||
}
|
||||
return error
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
function getMessageProperty(msg,expr) {
|
||||
var result = null;
|
||||
var msgPropParts;
|
||||
@@ -1451,6 +1496,7 @@ RED.utils = (function() {
|
||||
getDarkerColor: getDarkerColor,
|
||||
parseModuleList: parseModuleList,
|
||||
checkModuleAllowed: checkModuleAllowed,
|
||||
getBrowserInfo: getBrowserInfo
|
||||
getBrowserInfo: getBrowserInfo,
|
||||
validateTypedProperty: validateTypedProperty
|
||||
}
|
||||
})();
|
||||
|
@@ -13,6 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
try {
|
||||
RED.validators = {
|
||||
number: function(blankAllowed,mopt){
|
||||
return function(v, opt) {
|
||||
@@ -43,43 +44,17 @@ RED.validators = {
|
||||
typedInput: function(ptypeName,isConfig,mopt) {
|
||||
return function(v, opt) {
|
||||
var ptype = $("#node-"+(isConfig?"config-":"")+"input-"+ptypeName).val() || this[ptypeName];
|
||||
if (ptype === 'json') {
|
||||
try {
|
||||
JSON.parse(v);
|
||||
return true;
|
||||
} catch(err) {
|
||||
if (opt && opt.label) {
|
||||
return RED._("validator.errors.invalid-json-prop", {
|
||||
error: err.message,
|
||||
prop: opt.label,
|
||||
});
|
||||
}
|
||||
return opt ? RED._("validator.errors.invalid-json", {
|
||||
error: err.message
|
||||
}) : false;
|
||||
}
|
||||
} else if (ptype === 'msg' || ptype === 'flow' || ptype === 'global' ) {
|
||||
if (RED.utils.validatePropertyExpression(v)) {
|
||||
return true;
|
||||
}
|
||||
if (opt && opt.label) {
|
||||
return RED._("validator.errors.invalid-prop-prop", {
|
||||
prop: opt.label
|
||||
});
|
||||
}
|
||||
return opt ? RED._("validator.errors.invalid-prop") : false;
|
||||
} else if (ptype === 'num') {
|
||||
if (/^[+-]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)?$/.test(v)) {
|
||||
return true;
|
||||
}
|
||||
if (opt && opt.label) {
|
||||
return RED._("validator.errors.invalid-num-prop", {
|
||||
prop: opt.label
|
||||
});
|
||||
}
|
||||
return opt ? RED._("validator.errors.invalid-num") : false;
|
||||
const result = RED.utils.validateTypedProperty(v, ptype, opt)
|
||||
if (result === true || opt) {
|
||||
// Valid, or opt provided - return result as-is
|
||||
return result
|
||||
}
|
||||
return true;
|
||||
};
|
||||
// No opt - need to return false for backwards compatibilty
|
||||
return false
|
||||
}
|
||||
}
|
||||
};
|
||||
} catch(err) {
|
||||
console.log(err)
|
||||
debugger;
|
||||
}
|
Reference in New Issue
Block a user