-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix: add 'hasOption' function to prevent 'null' or 'undefined' option values #1542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1542 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 102 103 +1
Lines 2029 2033 +4
Branches 457 458 +1
=========================================
+ Hits 2029 2033 +4
Continue to review full report at Codecov.
|
3351ff8
to
dad8af5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a little bit skeptical about this one. We don't do type checking on options for other validators, why should we for this one?
It could undoubtedly be extended to other validators too. |
As mentioned in my issue I was (still am) confused that the library seems to differentiate between passing
and
I expected both invocations to result in the same output as |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @fedeci
I'm trying to check old and stale PRs, can you please fix merge conflicts here?
I will in a couple of hours! |
dad8af5
to
a6181a6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @fedeci !
I added two suggestions, can you check them and give me a feedback/your opinion?
src/lib/isInt.js
Outdated
hasOption(options, 'allow_leading_zeroes') && !options.allow_leading_zeroes ? | ||
int : intLeadingZeroes | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is hasOption
needed here? Can't we just do this since both undefined
and null
will be converted to false?
let regex = options.allow_leading_zeroes ? intLeadingZeroes : int;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we can
src/lib/isFloat.js
Outdated
(!options.hasOwnProperty('max') || value <= options.max) && | ||
(!options.hasOwnProperty('lt') || value < options.lt) && | ||
(!options.hasOwnProperty('gt') || value > options.gt); | ||
(!hasOption(options, 'min') || value >= options.min) && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was thinking of an alternative here since this function will only be needed in isFloat
and isInt
, can't we just use nullish coalescing operator? It will return null
instead of undefined
and any value
> null will resolve to true
(!hasOption(options, 'min') || value >= options.min) && | |
value >= (options.min ?? null) && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose that back when I wrote this I didn't know about the nullish coalescing op😄. Definitely what we are searching, though.
a6181a6
to
68ecc1b
Compare
Oh, maybe |
I need to update some deps, including |
You probably don't want to update eslint right now, i'm already working on a PR to update dev deps including rollup and eslint and you will have issues with eslint (some rules have been changed between versions) The screenshot is |
I'll wait for that PR to mark this as ready for review, in the meanwhile I will work with my locally update deps. Remember to update |
153d641
to
3425af1
Compare
3425af1
to
d03e4bb
Compare
return false; | ||
} | ||
const value = parseFloat(str.replace(',', '.')); | ||
const value = parseFloat(str.replace(splittingChar, '.')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is another bug fixed. I don't know why it could not be detected before.
value >= (options.min ?? -Infinity) && | ||
value <= (options.max ?? Infinity) && | ||
value < (options.lt ?? Infinity) && | ||
value > (options.gt ?? -Infinity); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
null
returns true only for min
and max
. Otherwise is false
. I had to use this kind of identity instead.
Thank you ! Already did 😅 I will probably finish working on it this week end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for fixing this.
And sorry for taking long for the review. We're now closing in, for the Jan release. Thanks @fedeci ! 🎉
Check the failing tests and we should be good to go. |
This fixes #1532
Checklist