import * as vi from "vitest"
import { zx } from "@traversable/zod"
// Using `zx.deepPartial.writeable` here to make it easier to visualize `zx.deepPartial`'s behavior:
vi.expect.soft(zx.deepPartial.writeable(
z.object({
a: z.number(),
b: z.string(),
c: z.object({
d: z.array(z.object({
e: z.number().max(1),
f: z.boolean()
})).length(10)
})
})
)).toMatchInlineSnapshot
(`
"z.object({
a: z.number().optional(),
b: z.string().optional(),
c: z.object({
d: z.array(z.object({
e: z.number().max(1).optional(),
f: z.boolean().optional()
})).length(10).optional()
}).optional()
})"
`)
zx.deepPartial
Converts an arbitrary zod schema into its "deeply-partial" form.
That just means that the schema will be traversed, and wrap all z.object
z.object
properties with z.optionalz.optional
.Options
zx.deepPartial
's behavior is configurable at the typelevel via the defaults.typeleveloptions.typelevel
property:defaults.typelevel
"semantic"
(default): leave the schema untouched, but wrap it in a no-op interface (zx.deepPartial.Semantic
) to make things explicitdefaults.typelevel
"applyToTypesOnly"
: apply the transformation to the schema's output type and wrap it in z.ZodTypez.ZodType
defaults.typelevel
"preserveSchemaType"
:zx.deepPartial
will return what it got, type untouchedNote:
Make sure you understand the semantics of
deepPartial
before 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.