Skip to content

Conversation

fedeci
Copy link
Contributor

@fedeci fedeci commented Nov 30, 2020

This fixes #1532

Checklist

  • PR contains only changes related; no stray files, etc.
  • README updated (where applicable)
  • Tests written (where applicable)

@codecov
Copy link

codecov bot commented Dec 1, 2020

Codecov Report

Merging #1542 (68ecc1b) into master (13651ea) will not change coverage.
The diff coverage is 100.00%.

❗ Current head 68ecc1b differs from pull request most recent head d03e4bb. Consider uploading reports for the commit d03e4bb to get more accurate results
Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
src/lib/isFloat.js 100.00% <100.00%> (ø)
src/lib/isInt.js 100.00% <100.00%> (ø)
src/lib/util/hasOption.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 13651ea...d03e4bb. Read the comment docs.

Copy link
Member

@tux-tn tux-tn left a 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?

@fedeci
Copy link
Contributor Author

fedeci commented Dec 19, 2020

It could undoubtedly be extended to other validators too.
I think @timo92 could provide more details than me on his use case of isInt

@timo92
Copy link

timo92 commented Dec 20, 2020

As mentioned in my issue I was (still am) confused that the library seems to differentiate between passing

validator.isInt("2", {})

and

validator.isInt("2", { max: undefined })

I expected both invocations to result in the same output as options.max === undefined in both cases and the typescript types from @types/validator mark min and max as optional (i.e. not declared or undefined).
Now to me the types don't match the current implementation and one can argue which of the two implemenations/definitions is desired. To me as an end user of the library I can easily omit non required options from the argument but would have expected this handling to be performed within the library. I don't know what the initial intention behind using hasOwnProperty vs == null or the like was though.
The two possible solutions that would satisfy me personally would be to either allow explicit passing of undefined as the value of an option within validator.js or to change the typescript types to explicitly state that e.g. min must be a number or not present on the options object.

Copy link
Member

@tux-tn tux-tn left a 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?

@fedeci
Copy link
Contributor Author

fedeci commented Oct 2, 2021

I will in a couple of hours!

@fedeci fedeci force-pushed the option-availability branch from dad8af5 to a6181a6 Compare October 2, 2021 11:52
Copy link
Member

@tux-tn tux-tn left a 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
Comment on lines 14 to 16
hasOption(options, 'allow_leading_zeroes') && !options.allow_leading_zeroes ?
int : intLeadingZeroes
);
Copy link
Member

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;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we can

(!options.hasOwnProperty('max') || value <= options.max) &&
(!options.hasOwnProperty('lt') || value < options.lt) &&
(!options.hasOwnProperty('gt') || value > options.gt);
(!hasOption(options, 'min') || value >= options.min) &&
Copy link
Member

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

Suggested change
(!hasOption(options, 'min') || value >= options.min) &&
value >= (options.min ?? null) &&

Copy link
Contributor Author

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.

@fedeci fedeci force-pushed the option-availability branch from a6181a6 to 68ecc1b Compare October 6, 2021 10:45
@fedeci fedeci marked this pull request as draft October 6, 2021 10:46
@fedeci
Copy link
Contributor Author

fedeci commented Oct 6, 2021

Oh, maybe ?? is not supported by eslint? Probably I should fix babel-parser config🤔

@fedeci
Copy link
Contributor Author

fedeci commented Oct 6, 2021

I need to update some deps, including eslint itself reference. Do you prefer a new PR?

@tux-tn
Copy link
Member

tux-tn commented Oct 6, 2021

I need to update some deps, including eslint itself reference. Do you prefer a new PR?

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)
Capture d’écran 2021-10-06 à 12 51 45 PM

The screenshot is yarn lint result with updated eslint dependencies and without changing the eslint config 😅

@fedeci
Copy link
Contributor Author

fedeci commented Oct 6, 2021

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 babel-eslint to @babel/eslint-parser too.

@fedeci fedeci force-pushed the option-availability branch from 153d641 to 3425af1 Compare October 6, 2021 13:27
@fedeci fedeci force-pushed the option-availability branch from 3425af1 to d03e4bb Compare October 6, 2021 13:51
return false;
}
const value = parseFloat(str.replace(',', '.'));
const value = parseFloat(str.replace(splittingChar, '.'));
Copy link
Contributor Author

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.

Comment on lines +14 to +17
value >= (options.min ?? -Infinity) &&
value <= (options.max ?? Infinity) &&
value < (options.lt ?? Infinity) &&
value > (options.gt ?? -Infinity);
Copy link
Contributor Author

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.

@fedeci fedeci marked this pull request as ready for review October 6, 2021 13:53
@tux-tn
Copy link
Member

tux-tn commented Oct 6, 2021

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 babel-eslint to @babel/eslint-parser too.

Thank you ! Already did 😅 I will probably finish working on it this week end

@tux-tn tux-tn mentioned this pull request Nov 16, 2021
3 tasks
Copy link
Member

@profnandaa profnandaa left a 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 ! 🎉

@profnandaa profnandaa added ready-to-land For PRs that are reviewed and ready to be landed and removed 🕑 to-be-reviewed labels Dec 12, 2022
@profnandaa
Copy link
Member

Check the failing tests and we should be good to go.

@profnandaa profnandaa added 🧹 needs-update For PRs that need to be updated before landing and removed ready-to-land For PRs that are reviewed and ready to be landed labels Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-more-review 🧹 needs-update For PRs that need to be updated before landing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect validation result when passing explicit undefined in options to isInt

4 participants