Skip to content
Sarj Code Standards

prefer-zod-parse-output-type

Consider deriving validated return contracts from the Zod schema when the schema owns the contract.

Why

A hand-written contract can drift from the runtime-validated value. Schema ownership determines whether deriving the type preserves the domain architecture.

Fix

When the schema owns the contract, derive it with z.output<typeof Schema>. Preserve independently owned domain and public API contracts, and validate schema compatibility with those contracts.

Examples

Before — flagged Do not hand-write the parsed return shape
src/contracts.ts
export interface ParsedRow {
id: string;
}
src/row.ts
import { z } from "zod";
import type { ParsedRow } from "./contracts.js";
const RowSchema = z.object({ id: z.string() });
function load(): ParsedRow {
return RowSchema.parse({});
}
After — preferred Derive the validated return contract
src/row.ts
import { z } from "zod";
const RowSchema = z.object({ id: z.string() });
type Row = z.output<typeof RowSchema>;
function load(): Row {
return RowSchema.parse({});
}