Skip to content

Commit c218264

Browse files
committed
fix: Don't skip required undefined properties on object
1 parent b14eb6b commit c218264

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/value/default/from-object.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ THE SOFTWARE.
2929
// deno-fmt-ignore-file
3030
// deno-lint-ignore-file
3131

32-
import type { TProperties, TObject } from '../../type/index.ts'
32+
import { type TProperties, type TObject, IsOptional } from '../../type/index.ts'
3333
import { Guard } from '../../guard/index.ts'
34-
import { FromDefault } from './from-default.ts'
3534
import { FromType } from './from-type.ts'
3635

3736
import { IsAdditionalProperties } from '../../schema/types/index.ts'
@@ -46,7 +45,7 @@ export function FromObject(context: TProperties, type: TObject, value: unknown):
4645
// yielded a non undefined result. Here we interpret an undefined result as
4746
// a non assignable property and continue.
4847
const propertyValue = FromType(context, type.properties[key], value[key])
49-
if (Guard.IsUndefined(propertyValue)) continue
48+
if (Guard.IsUndefined(propertyValue) && (IsOptional(type.properties[key]) || !('default' in type.properties[key]))) continue
5049
value[key] = FromType(context, type.properties[key], value[key])
5150
}
5251
// return if not additional properties

test/typebox/runtime/value/default/object.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,13 @@ Test('Should Default 22', () => {
260260
const R = Value.Default(X, { x: 2 })
261261
Assert.IsEqual(R, { x: 2 })
262262
})
263+
Test('Should Default 23', () => {
264+
const X = Type.Object({ x: Type.Undefined({ default: undefined }) })
265+
const R = Value.Default(X, {})
266+
Assert.IsEqual(R, { x: undefined })
267+
})
268+
Test('Should Default 24', () => {
269+
const X = Type.Object({ x: Type.Optional({ default: undefined }) })
270+
const R = Value.Default(X, {})
271+
Assert.IsEqual(R, {})
272+
})

0 commit comments

Comments
 (0)