import * as vi from "vitest"
import { zx } from "@traversable/zod"
// Using `zx.deepRequired.writeable` here to make it easier to visualize `zx.deepRequired`'s behavior:
vi.expect.soft(zx.deepRequired.writeable(
z.object({
a: z.optional(z.number()),
b: z.optional(z.string()),
c: z.object({
d: z.optional(
z.array(
z.optional(
z.object({
e: z.number().max(1),
f: z.optional(z.optional(z.boolean()))
})
)
)
)
})
})
)).toMatchInlineSnapshot(
`"z.object({
a: z.number(),
b: z.string(),
c: z.object({
d: z.array(
z.object({
e: z.number().max(1),
f: z.boolean()
})
)
})
})"`
)
zx.deepRequiredConverts an arbitrary zod schema into its "deeply-required" form.
That just means that the schema will be traversed, and any z.optional
z.optionalnodes will be unwrapped.Options
zx.deepRequired's behavior is configurable at the typelevel via the defaults.typeleveloptions.typelevelproperty:defaults.typelevel
"semantic"(default): leave the schema untouched, but wrap it in a no-op interface (zx.deepRequired.Semantic) to make things explicitdefaults.typelevel
"applyToTypesOnly": apply the transformation to the schema's output type and wrap it in z.ZodTypez.ZodTypedefaults.typelevel
"preserveSchemaType":zx.deepRequiredwill return what it got, type untouchedNote:
Make sure you understand the semantics of
deepRequiredbefore you use this in production. There's a reason it was removed from zod proper. This library trusts users to do their due diligence to make sure they understand the tradeoffs.