Revision 0.33.6 (#963)

* Traverse Instanced Object Values Inside Default

* Version
This commit is contained in:
sinclairzx81
2024-08-15 05:15:01 +09:00
committed by GitHub
parent cc5e6c7aca
commit 6da8b3fbc4
4 changed files with 30 additions and 8 deletions
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "@sinclair/typebox",
"version": "0.33.5",
"version": "0.33.6",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@sinclair/typebox",
"version": "0.33.5",
"version": "0.33.6",
"license": "MIT",
"devDependencies": {
"@arethetypeswrong/cli": "^0.13.2",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.33.5",
"version": "0.33.6",
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
+8 -5
View File
@@ -82,19 +82,22 @@ function FromIntersect(schema: TIntersect, references: TSchema[], value: unknown
function FromObject(schema: TObject, references: TSchema[], value: unknown): any {
const defaulted = ValueOrDefault(schema, value)
if (!IsObject(defaulted)) return defaulted
const additionalPropertiesSchema = schema.additionalProperties as TSchema
const knownPropertyKeys = Object.getOwnPropertyNames(schema.properties)
// properties
for (const key of knownPropertyKeys) {
if (!HasDefaultProperty(schema.properties[key])) continue
defaulted[key] = Visit(schema.properties[key], references, defaulted[key])
// note: we need to traverse into the object and test if the return value
// yielded a non undefined result. Here we interpret an undefined result as
// a non assignable property and continue.
const propertyValue = Visit(schema.properties[key], references, defaulted[key])
if (IsUndefined(propertyValue)) continue
defaulted[key] = propertyValue
}
// return if not additional properties
if (!HasDefaultProperty(additionalPropertiesSchema)) return defaulted
if (!HasDefaultProperty(schema.additionalProperties)) return defaulted
// additional properties
for (const key of Object.getOwnPropertyNames(defaulted)) {
if (knownPropertyKeys.includes(key)) continue
defaulted[key] = Visit(additionalPropertiesSchema, references, defaulted[key])
defaulted[key] = Visit(schema.additionalProperties, references, defaulted[key])
}
return defaulted
}
+19
View File
@@ -209,4 +209,23 @@ describe('value/default/Object', () => {
Value.Default(A, {})
Assert.IsEqual(A, B)
})
// ----------------------------------------------------------------
// Traveral: https://github.com/sinclairzx81/typebox/issues/962
// ----------------------------------------------------------------
it('Should traverse into an object 1 (initialize)', () => {
const Child = Type.Object({ a: Type.String({ default: 'x' }) })
const Parent = Type.Object({ child: Child })
Assert.IsEqual(Value.Default(Child, {}), { a: 'x' })
Assert.IsEqual(Value.Default(Parent, { child: {} }), { child: { a: 'x' } })
})
it('Should traverse into an object 2 (retain)', () => {
const Child = Type.Object({ a: Type.String({ default: 'x' }) })
const Parent = Type.Object({ child: Child })
Assert.IsEqual(Value.Default(Parent, { child: { a: 1 } }), { child: { a: 1 } })
})
it('Should traverse into an object 3 (ignore on undefined)', () => {
const Child = Type.Object({ a: Type.String({ default: 'x' }) })
const Parent = Type.Object({ child: Child })
Assert.IsEqual(Value.Default(Parent, { child: undefined }), { child: undefined })
})
})