Skip to content
This repository has been archived by the owner on Dec 19, 2022. It is now read-only.

Bug in s.number matcher with value of min or max is 0 #20

Closed
mehdivk opened this issue Mar 18, 2015 · 5 comments
Closed

Bug in s.number matcher with value of min or max is 0 #20

mehdivk opened this issue Mar 18, 2015 · 5 comments

Comments

@mehdivk
Copy link
Contributor

mehdivk commented Mar 18, 2015

Hi,
There is a bug in s.number matchers when min or max is 0. An example:

var s = require('strummer');
var ageValidator = s.number({min: 0});
var validate = s({age: ageValidator});
var catchMeIfYouCan = {age: 0};

console.log(validate(catchMeIfYouCan));

Issue is with if (opts.min && opts.max && (value < opts.min || value > opts.max)) {.
0, false and empty string can't pass if(some_var).

A helper needed here to just check value of min against being null or undefined. Something like

//utils.js

var hasValue = function(val) {
  return (typeof val !== 'undefined') && (val !== null);
}

module.exports = {
   hasValue: hasValue
}

This bug is available in some other matchers as well.

@mehdivk
Copy link
Contributor Author

mehdivk commented Mar 18, 2015

@nguyenchr @rprieto If you guys are happy with the hasValue helper solution I can apply it else happy to hear about ur solution.

@nguyenchr
Copy link
Contributor

good pickup, I guess we havn't run into this before as you don't generally set min or max values to be 0

hasValue looks like a good solution

@mehdivk
Copy link
Contributor Author

mehdivk commented Mar 18, 2015

Cool, I will comeback with a PR

@srushti
Copy link
Contributor

srushti commented Mar 27, 2015

Presumably this can be closed, now that the PR has been merged.

@srushti srushti closed this as completed Mar 27, 2015
@mehdivk
Copy link
Contributor Author

mehdivk commented Mar 29, 2015

@srushti 👍

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

No branches or pull requests

3 participants