Numeric
Sometime you might found yourself in need of numeric string like extracting path parameter, but it's typed as string and need to be convert to number.
Using Elysia's transform
life-cycle, you can manually parse number to string and reuse the transform function in other handler as well.
ts
app.get('/id/:id', ({ params: { id } }) => id, {
params: t.Object({
id: t.Number()
}),
transform({ params }) {
const id = +params.id
if(!Number.isNan(id))
params.id = id
}
})
app.get('/id/:id', ({ params: { id } }) => id, {
params: t.Object({
id: t.Number()
}),
transform({ params }) {
const id = +params.id
if(!Number.isNan(id))
params.id = id
}
})
However, it's a little bit redundant and require a boilerplate, so Elysia has a custom type to speed up the situation with t.Numeric
ts
app.get('/id/:id', ({ params: { id } }) => id, {
params: t.Object({
id: t.Numeric()
})
})
app.get('/id/:id', ({ params: { id } }) => id, {
params: t.Object({
id: t.Numeric()
})
})
This allows us to convert valid numeric string to number automatically.
If string is not a valid numeric string, it will throw an error in the runtime and prevent and execution inside the handler.
You can use numeric type on any property that support schema typing, including:
- params
- query
- headers
- body
- response