prefid-zod
v1.0.1
Published
Zod schemas for prefid — validate type-safe, prefixed IDs (user_…) in request bodies and forms.
Downloads
129
Maintainers
Readme
prefID-zod
Zod schemas for prefID — validate type-safe, prefixed IDs (user_…) in request bodies and forms.
isId() from prefID is a type guard; prefID-zod turns that same check into a
Zod schema so a prefixed ID drops straight into the validation you already write
for an API route, form, or config.
Install
npm install prefid-zod prefid zodprefid and zod are used at runtime — prefid ships as a dependency and
zod is a peer dependency, so your app's version of Zod is the one that runs.
Usage
import { z } from "zod";
import { zId } from "prefid-zod";
const CreateOrder = z.object({
userId: zId("user"), // validated AND typed `user_${string}`
amount: z.number().positive(),
});
CreateOrder.parse({ userId: "user_a8Kd0f2b", amount: 42 }); // ✅
CreateOrder.parse({ userId: "order_9f8e7d", amount: 42 }); // ❌ throws — wrong prefixThe parsed value keeps prefID's literal type, so data.userId is
`user_${string}` downstream — the compiler still stops you from passing it
where an order ID is expected.
Options
zId("user", { separator: "-" }); // matches "user-…" instead of "user_…"
zId("user", { message: "Invalid user id" }); // custom error messageLicense
MIT © Syed Suhail Ahmed
