Schema and Optional Properties with Defaults

How can I express in effect schema that if all object properties are optional, the object itself should be optional?

Here's an example zod schema:

z.object({
    options: z.object({
        a: z.number().default(1),
        b: z.string().default("hello")
    }).default({})
})


Since both a and b are optional, zod allows me to set the default value for options to {}, but somehow I can't do that with schema.

S.Struct({
    options: S.Struct({
        a: S.Number.pipe(S.optional({ default: () => 1 })),
        b: S.String.pipe(S.optional({ default: () => "hello" }))
    }).pipe(S.optional({ default: () => ({ a: 1, b: "hello" }) }))
})


This is what schema forces me to do. Is there a way describe this behavior like in zod?
Was this page helpful?