Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
manuel-di-iorio opened this issue Aug 29, 2017 · 2 comments

Comments

@manuel-di-iorio
Copy link

manuel-di-iorio commented Aug 29, 2017

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;
}
@gaochengyidlmu
Copy link

I also have the problem.

const rule = { transferDate: {type: 'string', required: false, allowEmpty: true}

let someDate = {transferDate: '2018-07-02'}

I want to set the transferDate to null.
but it throw the error.

{ message: 'should be a string', code: 'invalid', field: 'transferDate' }

@konglingwen94
Copy link

同样的问题+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants