elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
9.13k stars 194 forks source link

Query parameters do not work with number type from Typebox #340

Open JohnnyRacer opened 7 months ago

JohnnyRacer commented 7 months ago

Hello I am trying to use query params in my routes but seems to have trouble with the number type. The following route when accessed via the URL gives this error : `Invalid query, \'num\' Expected : {} Found : { "num": "3" }'

The URL I am fetching from is : '/num?number=3`

import { Static, Type as t } from "@sinclair/typebox"

app.get('/num', ({query}) => {
return {number : query.num}
}, {
query : t.Object({
     number : t.Number()
})
});

However if I change the type to {number : t.String()} it works as expected.

JohnnyRacer commented 7 months ago

The same error occurs if I import { t } from 'elysia.

JohnnyRacer commented 7 months ago

Seems like the problem is t.Number(), using t.Numeric() fixes this issue.

MatthewAry commented 7 months ago

While most people are comfortable with dealing with a flat, Record like object for handling their query parameters, I think that Eden Treaty and Eden Fetch should allow for serialization of objects into the query string using a tool like https://www.npmjs.com/package/qs I think support for this could be added by modifying Eden to have a lifecycle hook specifically for handling how the $query object gets converted to a query string. Elysia might need a hook for the deserialization step for all incoming requests.