Skip to content

prefer-module-level-refined-schema

Declare closed Zod scalar, format, and wrapper schemas at module scope.

Why

A closed validation pipeline created inside a function is rebuilt on every invocation and obscures a reusable constraint.

Fix

Move the validation schema to module scope, name it with a PascalCase Schema suffix, and call parse on the shared schema.

Examples

Before — flagged Do not rebuild a closed validation chain
src/options.ts
import { z } from "zod";
export function parse(value: unknown) {
return z.string().trim().min(1).max(128).parse(value);
}
After — preferred Share the validation schema
src/options.ts
import { z } from "zod";
const BatchSizeSchema = z.number().int().min(1).max(1000);
export function parse(value: unknown) {
return BatchSizeSchema.parse(value);
}