Skip to content

Commit

Permalink
Validate amount is greater than 0
Browse files Browse the repository at this point in the history
  • Loading branch information
harryttd committed Aug 17, 2023
1 parent 731bcd1 commit f39995e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "dist/src/api.js",
"scripts": {
"build": "npx tsc",
"dev": "nodemon -e ts -w src --exec \"npx tsc && npm run start\"",
"dev": "nodemon -e ts -w src -w profiles.json --exec \"npx tsc && npm run start\"",
"start": "node --enable-source-maps dist/src/api.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
25 changes: 12 additions & 13 deletions src/profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ const validateProperty = (
property: keyof ProfileConfig
): number => {
const value = Number(profile[property])

if (isNaN(value)) {
throw new Error(`Profile '${property}' must be a number`)
}

// If the property is 'amount' or if challenges are enabled, then the value of
// the property must be greater than 0. If it's not, an error is thrown.
if ((property === "amount" || !DISABLE_CHALLENGES) && value <= 0) {
throw new Error(`Profile '${property}' must be greater than 0`)
}

return value
}

Expand All @@ -39,21 +47,12 @@ const validateProfile = (profile: ProfileConfig): ProfileConfig => {
}, {} as ProfileConfig)
}

const validatedProfiles = new Proxy(
Object.entries(profiles).reduce((acc, [key, profile]) => {
const validatedProfiles = Object.entries(profiles).reduce(
(acc, [key, profile]) => {
acc[key.toUpperCase()] = validateProfile(profile)
return acc
}, {} as Record<Profile, ProfileConfig>),
{
get: (target, prop) => {
const key = String(prop)
if (key in target) {
return target[key]
} else {
throw new Error(`Profile '${key}' does not exist`)
}
},
}
},
{} as Record<Profile, ProfileConfig>
)

export default validatedProfiles

0 comments on commit f39995e

Please sign in to comment.