Skip to content

Commit

Permalink
Merge pull request #907 from crishoj/optional-boolean-string-decoding…
Browse files Browse the repository at this point in the history
…-issue

Optional `BooleanString` decoding issue
  • Loading branch information
SaltyAom authored Dec 4, 2024
2 parents 0240b5a + 1869e8b commit 5ff7410
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/type-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ export const ElysiaType = {
.Decode((value) => {
if (typeof value === 'string') return value === 'true'

if (property && !Value.Check(schema, value))
if (value !== undefined && !Value.Check(schema, value))
throw new ValidationError('property', schema, value)

return value
Expand Down
28 changes: 28 additions & 0 deletions test/validator/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,34 @@ describe('Query Validator', () => {
expect(await res.json()).toEqual({ param1: true })
})

it('parse optional boolean string with second parameter', async () => {
const schema = t.Object({
registered: t.Optional(t.Boolean()),
other: t.String(),
})
const app = new Elysia().get('/', ({ query }) => query, {
query: schema
})
const res = await app.handle(req('/?other=sucrose'))

expect(res.status).toBe(200)
expect(await res.json()).toEqual({ other: 'sucrose' })
})

it('parse optional boolean string with default value', async () => {
const schema = t.Object({
registered: t.Optional(t.Boolean({default: true})),
other: t.String(),
})
const app = new Elysia().get('/', ({ query }) => query, {
query: schema
})
const res = await app.handle(req('/?other=sucrose'))

expect(res.status).toBe(200)
expect(await res.json()).toEqual({ other: 'sucrose', registered: true })
})

it('validate optional object', async () => {
const app = new Elysia().get(
'/',
Expand Down

0 comments on commit 5ff7410

Please sign in to comment.