Skip to content

Commit 78e2115

Browse files
committed
🧪 test: verify standaloneValidator preservation
1 parent 648aef2 commit 78e2115

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

‎test/core/flattened-routes.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,64 @@ describe('getFlattenedRoutes', () => {
105105
expect(mixedRoute?.hooks.body.type).toBe('object')
106106
})
107107

108+
it('preserves standaloneValidator array when flattening', () => {
109+
const app = new Elysia().guard(
110+
{
111+
headers: t.Object({
112+
authorization: t.String()
113+
}),
114+
body: t.Object({
115+
data: t.String()
116+
}),
117+
response: {
118+
401: t.Object({ error: t.String() }),
119+
500: t.Object({ message: t.String() })
120+
}
121+
},
122+
(app) =>
123+
app.post('/protected', ({ body }) => body, {
124+
response: t.Object({
125+
success: t.Boolean(),
126+
data: t.String()
127+
})
128+
})
129+
)
130+
131+
// @ts-expect-error - accessing protected method for testing
132+
const flatRoutes = app.getFlattenedRoutes()
133+
134+
const protectedRoute = flatRoutes.find((r) => r.path === '/protected')
135+
136+
expect(protectedRoute).toBeDefined()
137+
138+
// The standaloneValidator array should still exist after flattening
139+
expect(protectedRoute?.hooks.standaloneValidator).toBeDefined()
140+
expect(Array.isArray(protectedRoute?.hooks.standaloneValidator)).toBe(
141+
true
142+
)
143+
144+
// Should have at least one validator from the guard
145+
expect(protectedRoute?.hooks.standaloneValidator?.length).toBeGreaterThan(
146+
0
147+
)
148+
149+
// Verify the standaloneValidator contains the guard schemas
150+
const validator = protectedRoute?.hooks.standaloneValidator?.[0]
151+
expect(validator).toBeDefined()
152+
expect(validator?.headers).toBeDefined()
153+
expect(validator?.body).toBeDefined()
154+
expect(validator?.response).toBeDefined()
155+
156+
// Verify schemas were also flattened into direct properties
157+
expect(protectedRoute?.hooks.headers).toBeDefined()
158+
expect(protectedRoute?.hooks.headers.type).toBe('object')
159+
expect(protectedRoute?.hooks.body).toBeDefined()
160+
expect(protectedRoute?.hooks.body.type).toBe('object')
161+
expect(protectedRoute?.hooks.response).toBeDefined()
162+
expect(protectedRoute?.hooks.response[401]).toBeDefined()
163+
expect(protectedRoute?.hooks.response[500]).toBeDefined()
164+
})
165+
108166
it('handles query and params schemas from guard', () => {
109167
const app = new Elysia().guard(
110168
{

0 commit comments

Comments
 (0)