Skip to content

Commit e345168

Browse files
authored
Merge pull request #1017 from and-rose/tighten-elysia-integer-type
fix: replace t.Number() for Type.Integer()
2 parents 10a3df7 + e714651 commit e345168

File tree

3 files changed

+64
-17
lines changed

3 files changed

+64
-17
lines changed

src/type-system.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ export const ElysiaType = {
376376
format: 'integer',
377377
default: 0
378378
}),
379-
t.Number(property)
379+
Type.Integer(property)
380380
],
381381
property
382382
)

test/validator/body.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,40 @@ describe('Body Validator', () => {
194194
expect(res.status).toBe(200)
195195
})
196196

197+
it('rejects malformed integer from array object', async () => {
198+
const app = new Elysia().post('/', ({ body }) => body, {
199+
body: t.Array(
200+
t.Object({
201+
name: t.String(),
202+
job: t.String(),
203+
trait: t.Optional(t.String()),
204+
age: t.Integer(),
205+
rank: t.Integer()
206+
})
207+
)
208+
})
209+
const res = await app.handle(
210+
post('/', [
211+
{
212+
name: 'sucrose',
213+
job: 'alchemist',
214+
age: 16.4,
215+
rank: 4
216+
}
217+
])
218+
)
219+
220+
expect(res.status).toBe(422)
221+
})
222+
223+
it('rejects malformed integer directly in array', async () => {
224+
const app = new Elysia().post('/', ({ body }) => body, {
225+
body: t.Array(t.Integer())
226+
})
227+
const res = await app.handle(post('/', [1, 2, 3, 4.2]))
228+
229+
expect(res.status).toBe(422)
230+
})
197231
it('validate empty body', async () => {
198232
const app = new Elysia().post('/', ({ body }) => body, {
199233
body: t.Union([

test/validator/params.test.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,40 @@ describe('Params Validator', () => {
110110
})
111111

112112
const res = await app.handle(req('/id/617.1234'))
113-
expect(await res.json()).toEqual({
113+
expect(await res.json()).toMatchObject({
114+
type: 'validation',
115+
on: 'params',
116+
summary: "Property 'id' should be one of: 'integer', 'integer'",
117+
property: '/id',
118+
message: 'Expected union value',
119+
expected: {
120+
id: 0
121+
},
122+
found: {
123+
id: '617.1234'
124+
},
114125
errors: [
115126
{
116-
errors: [],
117-
message: 'Expected integer',
118-
path: '',
127+
type: 62,
119128
schema: {
120-
type: 'integer'
129+
anyOf: [
130+
{
131+
format: 'integer',
132+
default: 0,
133+
type: 'string'
134+
},
135+
{
136+
type: 'integer'
137+
}
138+
]
121139
},
122-
summary: 'Expected integer',
123-
type: 27,
124-
value: 617.1234
140+
path: '/id',
141+
value: '617.1234',
142+
message: 'Expected union value',
143+
summary:
144+
"Property 'id' should be one of: 'integer', 'integer'"
125145
}
126-
],
127-
expected: 0,
128-
found: 617.1234,
129-
message: 'Expected integer',
130-
on: 'property',
131-
property: 'root',
132-
summary: 'Expected integer',
133-
type: 'validation'
146+
]
134147
})
135148
expect(res.status).toBe(422)
136149
})

0 commit comments

Comments
 (0)