parameter icon indicating copy to clipboard operation
parameter copied to clipboard

{required: false} rule does not skip the validation when the value is undefined but the key exists in the object

Open manuel-di-iorio opened this issue 7 years ago • 2 comments

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;
}

manuel-di-iorio avatar Aug 29 '17 22:08 manuel-di-iorio