Skip to content

Commit

Permalink
Invalid numbers route param parse as undefined (#897)
Browse files Browse the repository at this point in the history
* invalid number route params marshal into undefined

* linter

* treat all undefineds as undefined

* changeset
  • Loading branch information
AlecAivazis authored Feb 4, 2023
1 parent 2e10adc commit 1298fef
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-lobsters-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'houdini-svelte': patch
---

Invalid integer route paramters get unmarshaled as undefined
12 changes: 12 additions & 0 deletions packages/houdini/src/runtime/lib/scalars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,18 @@ describe('parseScalar', function () {
value: '1',
expected: 1,
},
{
title: 'invalid Int',
type: 'Int',
value: '',
expected: undefined,
},
{
title: 'invalid Float',
type: 'Float',
value: '',
expected: undefined,
},
{
title: 'Boolean',
type: 'Boolean',
Expand Down
20 changes: 16 additions & 4 deletions packages/houdini/src/runtime/lib/scalars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,12 @@ export function isScalar(config: ConfigFile, type: string) {
export function parseScalar(
config: ConfigFile,
type: string,
value: string
): string | number | boolean {
value?: string
): string | number | boolean | undefined {
if (typeof value === 'undefined') {
return undefined
}

if (type === 'Boolean') {
return value === 'true'
}
Expand All @@ -211,10 +215,18 @@ export function parseScalar(
return value
}
if (type === 'Int') {
return parseInt(value, 10)
const result = parseInt(value, 10)
if (Number.isNaN(result)) {
return undefined
}
return result
}
if (type === 'Float') {
return parseFloat(value)
const result = parseFloat(value)
if (Number.isNaN(result)) {
return undefined
}
return result
}

// if we have a special parse function, use it
Expand Down

0 comments on commit 1298fef

Please sign in to comment.