Skip to content

Commit

Permalink
Fix sending empty query parameters being parsed as empty strings inst…
Browse files Browse the repository at this point in the history
…ead of null (#104)
  • Loading branch information
G4brym authored Nov 21, 2023
1 parent e344ba3 commit 53e7782
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,13 @@ export function extractQueryParameters(
}

const params: Record<string, any> = {}
for (const [key, value] of searchParams.entries()) {
for (let [key, value] of searchParams.entries()) {
// Query parameters can be empty strings, that should equal to null as nothing was provided
if (value === '') {
// @ts-ignore
value = null
}

if (params[key] === undefined) {
params[key] = value
} else if (!Array.isArray(params[key])) {
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ describe('queryParametersValidation', () => {
expect(findError(resp.errors, 'p_string')).toBeUndefined()
})

test('checkStringInvalidEmpty', async () => {
const qs = '?p_string='
const request = await todoRouter.handle(
buildRequest({ method: 'GET', path: `/todos${qs}` })
)
const resp = await request.json()

expect(findError(resp.errors, 'p_string')).toEqual(
'Expected string, received null'
)
})

test('checkBooleanInvalid', async () => {
const qs = '?p_boolean=asd'
const request = await todoRouter.handle(
Expand Down

0 comments on commit 53e7782

Please sign in to comment.