Validator.js: Incorrect validation result when passing explicit undefined in options to isInt

Created on 27 Nov 2020  路  7Comments  路  Source: validatorjs/validator.js

Describe the bug
Due to my use case I'm currently wrapping the isInt validator and passing the options object as required. In conformance with the typescript type definitions I'm explicitly passing undefined for options.max. Because isInt uses hasOwnProperty to check for the presence of the option it compares the value with undefined and the validation fails.

As a workaround I changed my wrapper to pass only defined options, but would have expected isInt to handle this case.
I noticed that hasOwnProperty is used to check for options in other validators as well if no default option exists. So if addressed this issue would span over multiple validators.

Examples

import validator from "validator";

validator.isInt("2", { min: 1, max: undefined }) // yields false

Additional context
Validator.js version: 13.1.17

馃悰 bug

All 7 comments

This also checks for truthy values:

let minCheckPassed = (!options.hasOwnProperty('min') || !options.min || str >= options.min);

@profnandaa what do you think? Should I submit a PR?

That would however fail for
isInt("-2", { min: 0 })
as 0 is falsy.

A parseInt or typeof may be sufficient.

Right, I haven't thought about that. However parseInt would only work for integers, so it is not applicable to strings or whatever other type the option is. I excluded typeof because it is unable to check for null, but a strict equality can.

const minCheckPassed = (
  options.min === null ||
  typeof options.min === 'undefined' ||
  str >= options.min
)

@fedeci are you still willing to open a PR for this use-case/fix?

Sure!

Go ahead and raise a Pr for this fix and tag the issue.
I will be checking on a similar issue on #1531

@profnandaa, @tux-tn, and @rubiin what do you think?

Go ahead and raise a Pr for this fix and tag the issue.
I will be checking on a similar issue on #1531

@profnandaa, @tux-tn, and @rubiin what do you think?

I think this should be fixed as well

Was this page helpful?
0 / 5 - 0 ratings

Related issues

smohiuddin picture smohiuddin  路  4Comments

malkhuzayyim picture malkhuzayyim  路  4Comments

karladler picture karladler  路  3Comments

frontendmonster picture frontendmonster  路  4Comments

zilahir picture zilahir  路  3Comments