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