Skip to content

in rule false positive with zero integer value #429

Open

Description

@ddegr

I've bumped to the odd behavior of the in rule. It throws ok when it should have not to.

My validating object must have status field. The status value must be in the array of possible values [1, 2, 3, 4].
So validation rule is defined like that: status: 'required|in:1,2,3,4'.

When status is 0 validator says everything is fine but mustn't as there is no 0 int the giving possible values.

I also tried to rewrite rule. Somehow like that status: [ 'required', { in: [1, 2, 3, 4] }] and got the same wrong result.

Then I cloned validatorjs and added test case to the spec/in-rule.js file.

  it("should fail when 0 (zero) is not in giving arra", function() {
    const validator = new Validator(
      {
        quantity: 0
      },
      {
        quantity: "in:1,2"
      }
    );
    expect(validator.passes()).to.be.false;
    expect(validator.fails()).to.be.true;
  });

So my test case doesn't pass. Am I doing something wrong? Or this is bug?

Activity

  1. chantouchsek commented on Jul 6, 2022

    @chantouchsek

    @ddegr check it out here, it was forked to fix.
    chantouchsek/validatorjs#227

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions