npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@xndrjs/domain-zod

v0.3.0

Published

Zod 4 adapter for @xndrjs/domain with function-first APIs: zodToValidator and zodFromKit, plus full domain re-export.

Downloads

676

Readme

@xndrjs/domain-zod

Zod 4.x adapter for @xndrjs/domain. Use zodToValidator(schema) for a Validator<input, output>, and zodFromKit(kit) to compose core kits as nested Zod fields.

Zod 4 adapter for @xndrjs/domain: use zodToValidator and zodFromKit while keeping the domain core as the source of truth. Further validation adapters for @xndrjs/domain are on the roadmap (several likely in the near term).

This package re-exports @xndrjs/domain (domain, compose + types), so you can import from one place when you use Zod.

Install

pnpm add @xndrjs/domain-zod zod@^4

@xndrjs/domain is a direct dependency of this package; you do not have to add it separately unless you want to pin its version explicitly.

Usage

Import domain, zodToValidator, and zodFromKit from this package; import z from "zod". Modeling factories live on domain; compose, pipe, and DomainValidationError come from the root re-export of @xndrjs/domain.

Quickstart (step 2 in the stack)

Primitive

import { domain, zodToValidator } from "@xndrjs/domain-zod";
import { z } from "zod";

const Email = domain.primitive("Email", zodToValidator(z.email()));
const email = Email.create("[email protected]");

Shape

import { domain, zodToValidator } from "@xndrjs/domain-zod";
import { z } from "zod";

const User = domain.shape(
  "User",
  zodToValidator(
    z.object({
      type: z.literal("User").default("User"),
      id: z.string(),
    })
  )
);
const user = User.create({ id: "u-1" });

Nested composition from existing kits

import { domain, zodFromKit, zodToValidator } from "@xndrjs/domain-zod";
import { z } from "zod";

const Address = domain.shape(
  "Address",
  zodToValidator(z.object({ type: z.literal("Address").default("Address"), city: z.string() }))
);

const User = domain.shape(
  "User",
  zodToValidator(
    z.object({
      type: z.literal("User").default("User"),
      address: zodFromKit(Address),
    })
  )
);

Proof

import { domain, zodToValidator } from "@xndrjs/domain-zod";
import { z } from "zod";

const Verified = domain.proof("Verified", zodToValidator(z.object({ ok: z.literal(true) })));

Proof + refineType + pipe

import { domain, pipe, zodToValidator } from "@xndrjs/domain-zod";
import { z } from "zod";

const ItemSchema = z.object({
  id: z.string(),
  tier: z.enum(["free", "pro"]),
  count: z.number().int().nonnegative(),
});

const ProTier = domain
  .proof("ProTier", zodToValidator(ItemSchema))
  .refineType((row): row is typeof row & { tier: "pro" } => row.tier === "pro");

const Stocked = domain
  .proof("Stocked", zodToValidator(ItemSchema))
  .refineType((row): row is typeof row & { count: number } => row.count > 0);

const out = pipe({ id: "i-1", tier: "pro", count: 4 }, Stocked.assert, ProTier.assert);

Capabilities

import { domain, zodToValidator } from "@xndrjs/domain-zod";
import { z } from "zod";

const UserShape = domain.shape(
  "User",
  zodToValidator(z.object({ id: z.string(), isVerified: z.boolean() }))
);
const User = domain
  .capabilities<{ isVerified: boolean }>()
  .methods((patch) => ({
    verify(u) {
      return patch(u, { isVerified: true });
    },
  }))
  .attach(UserShape);

Recipes

Nested sub-shape composition from kit

  • Define child kits once with domain.shape(...).
  • Reference them in parent schemas via zodFromKit(childKit).
  • Keep all shape semantics in domain, not inside ad-hoc schema fragments.

Capabilities + patch re-validation

  • Attach capability bundles with domain.capabilities().methods(...).attach(shape).
  • Use the provided patch closure for every transition, so updates are always revalidated by the shape validator.

Cross-engine composition pattern

  • Keep core model modules adapter-agnostic.
  • Use zodToValidator only at IO boundaries that already use Zod.
  • If another boundary needs a different engine, keep the same kits and swap adapter package.

Pitfalls and design decisions

  • Avoid treating adapter schema APIs as domain extension APIs; extension belongs to adapter schema composition, not to core kit internals.
  • is checks are prototype/marker based; data after JSON roundtrip must be recreated with create.
  • Prefer explicit proof steps (proof.assert/pipe) over unchecked casts.

Validation errors

Zod failures become DomainValidationError with failure.engine === "zod", failure.issues normalized for the domain core, and failure.raw holding the original ZodError when useful for tooling.

License

MIT