2013-09-05 16:02:48 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2013-09-05 16:02:48 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
RED.validators = {
|
2022-02-14 02:40:49 +01:00
|
|
|
number: function(blankAllowed,mopt){
|
|
|
|
return function(v, opt) {
|
|
|
|
if ((blankAllowed&&(v===''||v===undefined)) || (v!=='' && !isNaN(v))) {
|
2017-01-06 23:20:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
2022-04-12 01:09:46 +02:00
|
|
|
if (opt && opt.label) {
|
|
|
|
return RED._("validator.errors.invalid-num-prop", {
|
|
|
|
prop: opt.label
|
|
|
|
});
|
2022-02-14 02:40:49 +01:00
|
|
|
}
|
|
|
|
return opt ? RED._("validator.errors.invalid-num") : false;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
regex: function(re, mopt) {
|
|
|
|
return function(v, opt) {
|
|
|
|
if (re.test(v)) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-04-12 01:09:46 +02:00
|
|
|
if (opt && opt.label) {
|
|
|
|
return RED._("validator.errors.invalid-regex-prop", {
|
|
|
|
prop: opt.label
|
|
|
|
});
|
2022-02-14 02:40:49 +01:00
|
|
|
}
|
|
|
|
return opt ? RED._("validator.errors.invalid-regexp") : false;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
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) {
|
2022-04-12 01:09:46 +02:00
|
|
|
if (opt && opt.label) {
|
|
|
|
return RED._("validator.errors.invalid-json-prop", {
|
2022-02-14 02:40:49 +01:00
|
|
|
error: err.message,
|
2022-04-12 01:09:46 +02:00
|
|
|
prop: opt.label,
|
|
|
|
});
|
2022-02-14 02:40:49 +01:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2022-04-12 01:09:46 +02:00
|
|
|
if (opt && opt.label) {
|
|
|
|
return RED._("validator.errors.invalid-prop-prop", {
|
|
|
|
prop: opt.label
|
|
|
|
});
|
2022-02-14 02:40:49 +01:00
|
|
|
}
|
|
|
|
return opt ? RED._("validator.errors.invalid-prop") : false;
|
|
|
|
} else if (ptype === 'num') {
|
|
|
|
if (/^[+-]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)?$/.test(v)) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-04-12 01:09:46 +02:00
|
|
|
if (opt && opt.label) {
|
|
|
|
return RED._("validator.errors.invalid-num-prop", {
|
|
|
|
prop: opt.label
|
|
|
|
});
|
2022-02-14 02:40:49 +01:00
|
|
|
}
|
|
|
|
return opt ? RED._("validator.errors.invalid-num") : false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
};
|