Skip to content

Commit

Permalink
Only accept fully valid ints in checkInt
Browse files Browse the repository at this point in the history
We now reject if the passed in string contains any non-int characters
to ensure that it was indeed an int that was passed to us and not just
something that could be converted to one even though it is not actually
an int

Change-type: patch
  • Loading branch information
Page- committed Feb 15, 2025
1 parent f89ca08 commit 481ad11
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export const checkInt = (num?: string): number | false => {
if (num == null) {
return false;
}
// If the string contains non-integer characters then it's not a valid integer, even if `parseInt` might turn it into one
if (!/^-?[0-9]+$/.test(num)) {
return false;
}
const n = parseInt(num, 10);
if (Number.isNaN(n)) {
return false;
Expand Down

0 comments on commit 481ad11

Please sign in to comment.