Skip to content

Commit

Permalink
Improve isValidReferral fn
Browse files Browse the repository at this point in the history
Accept only numbers in string w/o leading zeros.
  • Loading branch information
r-czajkowski committed Nov 25, 2024
1 parent e361813 commit f2d48ad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 1 addition & 3 deletions dapp/src/router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ export const router = createBrowserRouter([
// TODO: display the error page/modal when the referral is invalid.
const referralCodeFromUrl = referralProgram.getReferralFromURL()

const referralCode = referralProgram.isValidReferral(
Number(referralCodeFromUrl),
)
const referralCode = referralProgram.isValidReferral(referralCodeFromUrl)
? referralCodeFromUrl!
: env.REFERRAL

Expand Down
20 changes: 17 additions & 3 deletions dapp/src/utils/referralProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,23 @@ const EMBEDDED_APP_TO_REFERRAL: Record<EmbedApp, number> = {
"ledger-live": 2083,
}

const isValidReferral = (value: number) => {
const isInteger = Number.isInteger(value)
return isInteger && value >= 0 && value <= MAX_UINT16
const isValidReferral = (value: unknown) => {
let valueAsNumber: number | undefined

if (typeof value === "string") {
// Only digits w/o leading zeros.
const isNumber = /^(?:[1-9][0-9]*|0)$/.test(value)
valueAsNumber = isNumber ? Number(value) : undefined
} else if (typeof value === "number") {
valueAsNumber = value
}

return (
!!valueAsNumber &&
Number.isInteger(valueAsNumber) &&
valueAsNumber >= 0 &&
valueAsNumber <= MAX_UINT16
)
}

const getReferralFromURL = () =>
Expand Down

0 comments on commit f2d48ad

Please sign in to comment.