parameter
parameter copied to clipboard
{required: false} rule does not skip the validation when the value is undefined but the key exists in the object
Related issue: #25 (in chinese).
Use case:
const obj = {test: null};
parameter.validate({
test: {
type: "integer",
required: false
}
}, obj);
The above code will give the error should be an integer
even if the required: false rule is defined.
Where is the problem: https://github.com/node-modules/parameter/blob/master/index.js, row 61 to 63
var has = obj.hasOwnProperty(key);
if (!has) {
This condition should be replaced with:
var rule = formatRule(rules[key]);
var value = obj[key];
if (value == undefined) {
if (rule.required) {
/* push required error */
}
continue;
}