Skip to content

Commit 5ff7410

Browse files
authored
Merge pull request #907 from crishoj/optional-boolean-string-decoding-issue
Optional `BooleanString` decoding issue
2 parents 0240b5a + 1869e8b commit 5ff7410

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/type-system.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ export const ElysiaType = {
409409
.Decode((value) => {
410410
if (typeof value === 'string') return value === 'true'
411411

412-
if (property && !Value.Check(schema, value))
412+
if (value !== undefined && !Value.Check(schema, value))
413413
throw new ValidationError('property', schema, value)
414414

415415
return value

test/validator/query.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,34 @@ describe('Query Validator', () => {
200200
expect(await res.json()).toEqual({ param1: true })
201201
})
202202

203+
it('parse optional boolean string with second parameter', async () => {
204+
const schema = t.Object({
205+
registered: t.Optional(t.Boolean()),
206+
other: t.String(),
207+
})
208+
const app = new Elysia().get('/', ({ query }) => query, {
209+
query: schema
210+
})
211+
const res = await app.handle(req('/?other=sucrose'))
212+
213+
expect(res.status).toBe(200)
214+
expect(await res.json()).toEqual({ other: 'sucrose' })
215+
})
216+
217+
it('parse optional boolean string with default value', async () => {
218+
const schema = t.Object({
219+
registered: t.Optional(t.Boolean({default: true})),
220+
other: t.String(),
221+
})
222+
const app = new Elysia().get('/', ({ query }) => query, {
223+
query: schema
224+
})
225+
const res = await app.handle(req('/?other=sucrose'))
226+
227+
expect(res.status).toBe(200)
228+
expect(await res.json()).toEqual({ other: 'sucrose', registered: true })
229+
})
230+
203231
it('validate optional object', async () => {
204232
const app = new Elysia().get(
205233
'/',

0 commit comments

Comments
 (0)