Skip to content

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

Open

Description

@manuel-di-iorio

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

Activity

  1. gaochengyidlmu commented on Jul 2, 2018

    @gaochengyidlmu

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

  2. konglingwen94 commented on Dec 10, 2020

    @konglingwen94

    同样的问题+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions