Zod adapter
@xndrjs/domain-zod integrates Zod 4 with @xndrjs/domain.
It provides:
- an adapted
domainwhose top-levelshape/primitive/proofaccept Zod schemas;domain.core.*still takes aValidator zodToValidator(schema)when you need an explicitValidatorzodFromKit(kit)to embed domain kits inside Zod schemas- full re-export of
@xndrjs/domainfor single-entry imports (this package’sdomainshadows core)
Install
Section titled “Install”pnpm add @xndrjs/domain-zod zodMinimal example
Section titled “Minimal example”import { domain } from "@xndrjs/domain-zod";import { z } from "zod";
const Email = domain.primitive( "Email", z.email().transform((value) => value.toLowerCase()));
const email = Email.create("ADA@EXAMPLE.COM");// ada@example.comZod handles parsing and transformation. The domain primitive materializes the trusted value.
Passing schemas to domain
Section titled “Passing schemas to domain”Prefer passing Zod schemas directly:
const User = domain.shape( "User", z.object({ id: z.string(), age: z.number().int().nonnegative(), }));Use zodToValidator when you need a standalone Validator (shared compose, core @xndrjs/domain, or documenting the adapter mapping):
const userValidator = zodToValidator( z.object({ id: z.string(), age: z.number().int().nonnegative(), }));
const User = domain.core.shape("User", userValidator);Failure mapping (via the adapter):
engine: "zod"- one domain issue per Zod issue
- raw issue payload available in metadata
zodFromKit
Section titled “zodFromKit”zodFromKit(kit) turns a primitive or shape kit into a Zod field. Validation and transforms run only through the kit’s validator; the parent schema does not add a second parsing path for that field.
const Invite = domain.shape( "Invite", z.object({ recipient: zodFromKit(Email), message: z.string().min(1), }));Reusing shapes in parent schemas
Section titled “Reusing shapes in parent schemas”const Address = domain.shape( "Address", z.object({ city: z.string().min(1), country: z.string().length(2), }));
const User = domain.shape( "User", z.object({ id: z.string(), address: zodFromKit(Address), }));
const user = User.create({ id: "user_1", address: { city: "Rome", country: "IT" },});This avoids duplicating nested domain semantics in parent schemas.
Generated CMS schemas
Section titled “Generated CMS schemas”When you use @xndrjs/contentful-to-zod, parse and flatten at the transport boundary first, then wire the flat field schema into domain:
import { domain } from "@xndrjs/domain-zod";import { BlogPostFieldsSchema } from "./generated/contentful.schemas";
export const BlogPost = domain.shape("BlogPost", BlogPostFieldsSchema);Transport schemas describe what can arrive; domain rules define what you trust. See Contentful to Zod for the full pipeline.
Build a complete kit with First model, or compare engines in Choose an adapter.