Skip to content

Commit c54a0f8

Browse files
authored
Revision 0.34.39 (#1296)
* Guard Array in Object Conversion * Guard Array in Record Conversion * Version | Changelog * TypeScript 5.9.2
1 parent 60fdafe commit c54a0f8

File tree

6 files changed

+38
-14
lines changed

6 files changed

+38
-14
lines changed

changelog/0.34.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
---
44

55
### Revision Updates
6+
- [Revision 0.34.39](https://github.com/sinclairzx81/typebox/pull/1296)
7+
- Guard for Array in Object and Record conversion
68
- [Revision 0.34.38](https://github.com/sinclairzx81/typebox/pull/1282)
79
- Preserve exact type matches in Union conversion
810
- [Revision 0.34.37](https://github.com/sinclairzx81/typebox/pull/1278)

hammer.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export async function test_typescript() {
4040
'5.2.2', '5.3.2', '5.3.3', '5.4.3',
4141
'5.4.5', '5.5.2', '5.5.3', '5.5.4',
4242
'5.6.2', '5.6.3', '5.7.2', '5.7.3',
43-
'5.8.2', 'next', 'latest'
43+
'5.8.2', '5.8.3', 'next', 'latest'
4444
]) {
4545
await shell(`npm install typescript@${version} --no-save`)
4646
await test_static()

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sinclair/typebox",
3-
"version": "0.34.38",
3+
"version": "0.34.39",
44
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
55
"keywords": [
66
"typescript",
@@ -38,6 +38,6 @@
3838
"ajv-formats": "^2.1.1",
3939
"mocha": "^11.1.0",
4040
"prettier": "^2.7.1",
41-
"typescript": "^5.8.3"
41+
"typescript": "^5.9.2"
4242
}
4343
}

src/value/convert/convert.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,15 @@ function FromNumber(schema: TNumber, references: TSchema[], value: any): unknown
200200
}
201201
// prettier-ignore
202202
function FromObject(schema: TObject, references: TSchema[], value: any): unknown {
203-
if(!IsObject(value)) return value
203+
if(!IsObject(value) || IsArray(value)) return value
204204
for(const propertyKey of Object.getOwnPropertyNames(schema.properties)) {
205205
if(!HasPropertyKey(value, propertyKey)) continue
206206
value[propertyKey] = Visit(schema.properties[propertyKey], references, value[propertyKey])
207207
}
208208
return value
209209
}
210210
function FromRecord(schema: TRecord, references: TSchema[], value: any): unknown {
211-
const isConvertable = IsObject(value)
211+
const isConvertable = IsObject(value) && !IsArray(value)
212212
if (!isConvertable) return value
213213
const propertyKey = Object.getOwnPropertyNames(schema.patternProperties)[0]
214214
const property = schema.patternProperties[propertyKey]

test/runtime/value/convert/union.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,26 @@ describe('value/convert/Union', () => {
7777
Assert.IsEqual((A as any).data[0].key2, 42)
7878
Assert.IsEqual((B as any).data[0].key2, 42)
7979
})
80+
// ----------------------------------------------------------------
81+
// https://github.com/sinclairzx81/typebox/issues/1295
82+
// ----------------------------------------------------------------
83+
it('Should guard against Array conversion in Object', () => {
84+
const T = Type.Union([
85+
Type.Object({
86+
type: Type.Literal('A'),
87+
values: Type.Union([Type.String(), Type.Number()]),
88+
}),
89+
Type.Object({
90+
type: Type.Literal('B'),
91+
values: Type.String(),
92+
}),
93+
])
94+
const converted = Value.Convert(T, [{ type: 'A', values: 1 }])
95+
Assert.IsEqual(converted, [{ type: 'A', values: 1 }])
96+
})
97+
it('Should guard against Array conversion in Record', () => {
98+
const T = Type.Union([Type.Record(Type.String({ pattern: '^values$' }), Type.Union([Type.String(), Type.Number()])), Type.Record(Type.String({ pattern: '^type$' }), Type.Union([Type.String(), Type.Number()]))])
99+
const converted = Value.Convert(T, [{ type: 'A', values: 1 }])
100+
Assert.IsEqual(converted, [{ type: 'A', values: 1 }])
101+
})
80102
})

0 commit comments

Comments
 (0)