Skip to content

Commit 4084a6e

Browse files
committed
🔧 fix: handle optional property with special character
1 parent edfa808 commit 4084a6e

File tree

5 files changed

+32
-18
lines changed

5 files changed

+32
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.1.6 - 18 Aug 2025
2+
Bug fix:
3+
- handle optional property with special character
4+
15
# 0.1.5 - 6 Aug 2025
26
Bug fix:
37
- [#23](https://github.com/elysiajs/exact-mirror/pull/23) optional properties not deleted

example/index.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,17 @@ import { t } from 'elysia'
22
import { createMirror } from '../src/index'
33
import { TypeCompiler } from '@sinclair/typebox/compiler'
44

5-
const shape = t.Object(
6-
{
7-
keys: t.Array(t.Object({ a: t.Number() }))
8-
},
9-
{
10-
additionalProperties: true
11-
}
12-
)
5+
const shape = t.Object({
6+
'is-admin': t.Union([
7+
t.Boolean(),
8+
t.String({
9+
format: 'boolean'
10+
})
11+
])
12+
})
1313

1414
const value = {
15-
keys: [
16-
{
17-
a: 1,
18-
// @ts-expect-error
19-
b: 2
20-
}
21-
],
22-
extra: true
15+
'is-admin': true
2316
} satisfies typeof shape.static
2417

2518
const mirror = createMirror(shape, {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "exact-mirror",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "Mirror exact value to TypeBox/OpenAPI model",
55
"license": "MIT",
66
"scripts": {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ const mirror = (
495495
// 63 is '?'
496496
const shouldQuestion =
497497
prop.charCodeAt(0) !== 63 && schema.type !== 'array'
498-
v += `)delete x${shouldQuestion ? '?' : ''}${prop}\n`
498+
v += `)delete x${shouldQuestion ? (prop.charCodeAt(0) === 91 ? '?.' : '?') : ''}${prop}\n`
499499
}
500500

501501
return `${v}return x`

test/index.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,21 @@ describe('Core', () => {
269269
extra: true
270270
})
271271
})
272+
273+
it('handle optional property with special character', () => {
274+
const shape = t.Object({
275+
'is-admin': t.Union([
276+
t.Boolean(),
277+
t.String({
278+
format: 'boolean'
279+
})
280+
])
281+
})
282+
283+
const value = {
284+
'is-admin': true
285+
} satisfies typeof shape.static
286+
287+
isEqual(shape, value)
288+
})
272289
})

0 commit comments

Comments
 (0)