eslint-plugin-zod2
v0.0.6
Published
An ESLint plugin to enforce rules using Zod.
Readme
eslint-plugin-zod2
An ESLint plugin to enforce rules using Zod.
Installation
Install with pnpm:
pnpm add -D eslint-plugin-zod2Or with npm:
npm install --save-dev eslint-plugin-zod2Or with yarn:
yarn add --dev eslint-plugin-zod2Usage
Add zod2 to the plugins section of your ESLint configuration and enable the rule:
{
"plugins": ["zod2"],
"rules": {
"zod2/export-zod-type": "error"
}
}Rule: export-zod-type
Ensures that when you export a Zod schema as a const, you also export the corresponding TypeScript type using z.infer.
❌ Incorrect
export const Schema = z.object({});✅ Correct
export const Schema = z.object({});
export type Schema = z.infer<typeof Schema>;Option: excludeNameRegex
Suppose you want to exclude all schema names that start with an underscore from requiring a type export. You can use the excludeNameRegex option:
{
"rules": {
"zod2/export-zod-type": ["error", {
"excludeNameRegex": "^_"
}]
}
}Now, the following is allowed:
export const _Internal = z.object({});
// No type export required for _InternalOption: customZodSchemaBuilders
Suppose you have a custom function myZodSchema that returns a Zod schema:
export const MySchema = myZodSchema({});By default, the rule does not recognize myZodSchema as a Zod schema builder. To enforce type export for this, add it to customZodSchemaBuilders:
{
"rules": {
"zod2/export-zod-type": ["error", {
"customZodSchemaBuilders": ["myZodSchema"]
}]
}
}Now, the following will be required:
export const MySchema = myZodSchema({});
export type MySchema = z.infer<typeof MySchema>;