Skip to content

Commit 86ec566

Browse files
committed
🔧 fix: transform schema inside query
1 parent ef122b0 commit 86ec566

File tree

7 files changed

+60
-34
lines changed

7 files changed

+60
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# 1.2.20 - 22 Feb 2025
22
Bug fix:
3+
- [#671](https://github.com/elysiajs/elysia/issues/671#issuecomment-2675777040) Transform query schema check fails
34
- model type
45

56
# 1.2.19 - 22 Feb 2025

example/a.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
import { Elysia, t } from '../src'
22
import { req } from '../test/utils'
33

4-
const p1 = new Elysia().model({
5-
a: t.String()
6-
})
4+
const app = new Elysia().get(
5+
'/test',
6+
({ query: { id } }) => ({
7+
id,
8+
type: typeof id
9+
}),
10+
{
11+
query: t.Object({
12+
id: t
13+
.Transform(t.Array(t.UnionEnum(['test', 'foo'])))
14+
.Decode((id) => ({ value: id }))
15+
.Encode((id) => id.value)
16+
})
17+
}
18+
)
719

8-
const p2 = new Elysia().model({
9-
b: t.Number()
10-
})
11-
12-
const app = new Elysia()
13-
.use([p1, p2])
14-
.model({
15-
c: t.String()
16-
})
17-
18-
console.log(app.models)
20+
app.handle(req('/test?id=test'))
21+
.then((x) =>
22+
x.json().then((v) =>
23+
console.dir(v, {
24+
depth: null
25+
})
26+
)
27+
)

src/compose.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import {
2121
redirect,
2222
signCookie,
2323
isNotEmpty,
24-
encodePath,
25-
replaceSchemaType
24+
encodePath
2625
} from './utils'
2726
import { ParseError, error } from './error'
2827

@@ -46,7 +45,7 @@ import type {
4645
LifeCycleStore,
4746
SchemaValidator
4847
} from './types'
49-
import { t, type TypeCheck } from './type-system'
48+
import { type TypeCheck } from './type-system'
5049

5150
const TypeBoxSymbol = {
5251
optional: Symbol.for('TypeBox.Optional'),

src/index.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -633,20 +633,6 @@ export default class Elysia<
633633
createQuery() {
634634
if (this.query) return this.query
635635

636-
// console.dir(
637-
// getSchemaValidator(cloned.query, {
638-
// modules,
639-
// dynamic,
640-
// models,
641-
// coerce: true,
642-
// additionalCoerce:
643-
// stringToStructureCoercions()
644-
// }).schema,
645-
// {
646-
// depth: null
647-
// }
648-
// )
649-
650636
return (this.query = getSchemaValidator(
651637
cloned.query,
652638
{

src/type-system.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { fullFormats } from './formats'
3030
import type { CookieOptions } from './cookies'
3131
import { ValidationError } from './error'
3232
import type { MaybeArray } from './types'
33+
import { hasTransform } from './compose'
3334

3435
const isISO8601 =
3536
/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/
@@ -619,7 +620,8 @@ export const ElysiaType = {
619620

620621
if (typeof value === 'string') return decode(value)
621622

622-
throw new ValidationError('property', schema, value)
623+
// Is probably transformed, unable to check schema
624+
return value
623625
})
624626
.Encode((value) => {
625627
if (typeof value === 'string')

src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ const _replaceSchemaType = (
366366
const { anyOf, oneOf, allOf, not, properties, items, ...rest } = schema
367367
const to = options.to(rest)
368368

369-
if(!to) return schema
369+
if (!to) return schema
370370

371371
// If t.Transform is used, we need to re-calculate Encode, Decode
372372
let transform
@@ -513,7 +513,7 @@ const _replaceSchemaType = (
513513
const { anyOf, oneOf, allOf, not, type, ...rest } = value
514514
const to = options.to(rest)
515515

516-
if(!to) return schema
516+
if (!to) return schema
517517

518518
if (to.anyOf)
519519
for (let i = 0; i < to.anyOf.length; i++)

test/validator/query.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,4 +919,33 @@ describe('Query Validator', () => {
919919
{ status: ['a', 'b'] }
920920
])
921921
})
922+
923+
it('handle Transform query', async () => {
924+
const app = new Elysia().get(
925+
'/test',
926+
({ query: { id } }) => ({
927+
id,
928+
type: typeof id
929+
}),
930+
{
931+
query: t.Object({
932+
id: t
933+
.Transform(t.Array(t.UnionEnum(['test', 'foo'])))
934+
.Decode((id) => ({ value: id }))
935+
.Encode((id) => id.value)
936+
})
937+
}
938+
)
939+
940+
const response = await app
941+
.handle(req('/test?id=test'))
942+
.then((x) => x.json())
943+
944+
expect(response).toEqual({
945+
id: {
946+
value: ['test']
947+
},
948+
type: 'object'
949+
})
950+
})
922951
})

0 commit comments

Comments
 (0)