elysia
elysia copied to clipboard
Merge group schema with route schema
What is the problem this feature would solve?
Currently, values set in a group guard over overwritten if also defined in a route. For example:
new Elysia()
.group(
'/something'
{
response: {
default: t.Object({message: t.String()})
}
},
(app) => app
.get('/hello', () => 'hello', {response: {200: t.String()})
)
The response object for the "/hello" route will only contain the entry for the 200 response. If instead the response object was merged together, it'd be useful for adding default responses to a group. This would be especially useful for the "default" response which is really handy for handling errors.
What is the feature you are proposing to solve the problem?
Merge together the group guard response with the route defined response.
What alternatives have you considered?
No response
It should have been fixed by https://github.com/elysiajs/elysia/pull/502 (more specifically by SaltyAom's commit https://github.com/elysiajs/elysia/commit/da0fa83eacf00527e183f1dc410a699298657b9d) but it seems like it's broken.
Unable to replicate on 1.1.7 with the following code:
import { Elysia, t } from 'elysia'
new Elysia()
.group(
'/something',
{
response: {
400: t.Object({ message: t.String() })
}
},
(app) =>
app
.get('/200', ({ error }) => 'a', {
response: { 200: t.String() }
})
.get('/400', ({ error }) => error(400, { message: 'a' }), {
response: { 200: t.String() }
})
)
.listen(3000)
Both endpoint validation work correctly on my end.
Could you provide more specific reproduction step on how to replicate the error?
In the example above, the 400 response will be included for both routes. But if you use either "default" or "error", that won't work.
import { Elysia, t } from 'elysia'
new Elysia()
.group(
'/something',
{
response: {
error: t.Object({ message: t.String() })
}
},
(app) =>
app
.get('/200', ({ error }) => 'a', {
response: { 200: t.String() }
})
.get('/400', ({ error }) => error(400, { message: 'a' }), {
response: { 200: t.String() }
})
)
.listen(3000)
In Elyisa.js 1.2.25 this bug is still present. If you set default or error in group/guard, then it overwrites whatever is defined in the schema.
@SaltyAom
We never have support for response.default, and response.error so it seems to be a problem with a type issue (will look into it on my end)
But other than that, this seems to be fixed by Standalone validator
So, I'm closing as it has the original issue is about response.default
If you have a problem similar to this, please open a new one