sinclairzx81 / typebox

Json Schema Type Builder with Static Type Resolution for TypeScript
Other
4.56k stars 148 forks source link

Default values in nested objects are not generated #926

Open landrygeiger opened 5 days ago

landrygeiger commented 5 days ago

Hello, when I try to use Value.Default to generate the default values for a schema, properties on nested objects within the schema do not have their default values generated.

const TestSchema = Type.Object({
  a: Type.Object({
    b: Type.String({ default: null }),
  }),
  c: Type.String({ default: null }),
});

const T = Value.Default(TestSchema, { a: {} }); // { a: {}, c: null }

As you can see, the root level properties are generated, but not the nested ones.

Thanks!

kade-robertson commented 5 days ago

You can solve this by specifying an empty object as the default for the Type.Object components:

const TestSchema = Type.Object({
  a: Type.Object({
    b: Type.String({ default: null }),
  }, { default: {} }),
  c: Type.String({ default: null }),
}, { default: {} });

This comment might help explain why this is necessary: https://github.com/sinclairzx81/typebox/issues/714#issuecomment-1872640161