Skip to content

Commit e19cfd3

Browse files
committed
🎉 release: 1.2.22
1 parent 009da8c commit e19cfd3

File tree

5 files changed

+39
-81
lines changed

5 files changed

+39
-81
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# 1.2.22 - 23 Feb 2025
1+
# 1.2.22 - 24 Feb 2025
22
Bug fix
3+
- [#1074](https://github.com/elysiajs/elysia/pull/1074) hasTransform doesn't detect Transform on root
34
- [#873](https://github.com/elysiajs/elysia/issues/873#issuecomment-2676628776) encode before type check
45

56
# 1.2.21 - 22 Feb 2025

example/a.ts

Lines changed: 27 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,33 @@
11
import { Elysia, t } from '../src'
2-
import { hasRef, hasTransform } from '../src/compose'
3-
import { TypeCompiler } from '../src/type-system'
4-
import { req } from '../test/utils'
2+
import { hasTransform } from '../src/compose'
3+
import { post } from '../test/utils'
54

6-
// use a simple string-number transform to demo
7-
const encodedNumberSchema = t
8-
.Transform(t.String())
9-
.Decode((value) => parseFloat(value))
10-
.Encode((value) => value.toString())
5+
console.log(
6+
hasTransform(
7+
t
8+
.Transform(
9+
t.Object({
10+
name: t.String()
11+
})
12+
)
13+
.Decode((x) => x.name)
14+
.Encode((x) => ({ name: x }))
15+
)
16+
)
1117

12-
// put the transform type in object for easily checking.
13-
const dataTransferObject = t.Object({
14-
value: encodedNumberSchema
18+
const app = new Elysia().post('/', ({ body }) => body, {
19+
body: t
20+
.Transform(
21+
t.Object({
22+
name: t.String()
23+
})
24+
)
25+
.Decode((x) => x.name)
26+
.Encode((x) => ({ name: x })),
27+
response: t.String()
1528
})
1629

17-
const a = TypeCompiler.Compile(encodedNumberSchema)
18-
19-
const elysia = new Elysia({
20-
experimental: {
21-
encodeSchema: true //open the flag!
22-
}
23-
}).post(
24-
'/',
25-
({ body }) => {
26-
// body.value is number!
27-
console.log(typeof body.value) // number here! like my expectation
28-
return body
29-
},
30-
{
31-
body: dataTransferObject, //in swagger, request body.value should be a string
32-
response: dataTransferObject //in swagger, reponse body.value should be a string
33-
}
34-
)
35-
36-
await elysia
37-
.handle(
38-
new Request('http://localhost:3000/', {
39-
method: 'POST',
40-
headers: {
41-
'Content-Type': 'application/json'
42-
},
43-
body: JSON.stringify({ value: '1.1' })
44-
})
45-
)
46-
.then((res) => res.json())
47-
.then(console.log) //still error here, it says:
30+
const res = await app.handle(post('/', { name: 'difhel' }))
4831

49-
/*
50-
{
51-
type: "validation",
52-
on: "response",
53-
summary: "Expected property 'value' to be string but found: 1.1",
54-
property: "/value",
55-
message: "Expected string",
56-
expected: {
57-
value: "",
58-
},
59-
found: {
60-
value: 1.1,
61-
},
62-
errors: [
63-
{
64-
type: 54,
65-
schema: [Object ...],
66-
path: "/value",
67-
value: 1.1,
68-
message: "Expected string",
69-
errors: [],
70-
summary: "Expected property 'value' to be string but found: 1.1",
71-
}
72-
],
73-
}
74-
*/
32+
console.log(await res.text()) //.toBe('difhel')
33+
console.log(res.status) //.toBe(200)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "elysia",
33
"description": "Ergonomic Framework for Human",
4-
"version": "1.2.21",
4+
"version": "1.2.22",
55
"author": {
66
"name": "saltyAom",
77
"url": "https://github.com/SaltyAom",

src/compose.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,6 @@ export const hasRef = (schema: TAnySchema): boolean => {
395395
)
396396
return true
397397
}
398-
399-
return false
400398
}
401399

402400
if (schema.type === 'array' && schema.items && hasRef(schema.items))
@@ -408,8 +406,13 @@ export const hasRef = (schema: TAnySchema): boolean => {
408406
export const hasTransform = (schema: TAnySchema): boolean => {
409407
if (!schema) return false
410408

411-
if (schema.$ref && schema.$defs && schema.$ref in schema.$defs)
412-
return hasTransform(schema.$defs[schema.$ref])
409+
if (
410+
schema.$ref &&
411+
schema.$defs &&
412+
schema.$ref in schema.$defs &&
413+
hasTransform(schema.$defs[schema.$ref])
414+
)
415+
return true
413416

414417
if (schema.oneOf)
415418
for (let i = 0; i < schema.oneOf.length; i++)
@@ -441,17 +444,12 @@ export const hasTransform = (schema: TAnySchema): boolean => {
441444
)
442445
return true
443446
}
444-
445-
return false
446447
}
447448

448449
if (schema.type === 'array' && schema.items && hasTransform(schema.items))
449450
return true
450451

451-
return (
452-
TransformSymbol in schema ||
453-
(!!schema.properties && TransformSymbol in schema.properties)
454-
)
452+
return TransformSymbol in schema
455453
}
456454

457455
/**

test/units/has-transform.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe('has transform', () => {
137137
expect(
138138
hasTransform(
139139
t
140-
.Transform(t.Object({ id: t.Integer() }))
140+
.Transform(t.Object({ id: t.String() }))
141141
.Decode((value) => value.id)
142142
.Encode((value) => ({
143143
id: value

0 commit comments

Comments
 (0)