Skip to content

Instantly share code, notes, and snippets.

@Evan-Kelly
Evan-Kelly / zod-validator.ts
Last active April 10, 2025 04:00
A custom class for validating zod schemas. Supports validation of objects. arrays. partials, and individual fields. Emits a CustomZodError which wraps ZodError.
import { z } from 'zod';
/**
* Simplified ZodError with focused error formatting capabilities
*/
class SimplifiedZodError extends z.ZodError {
formatToSchema(schema: z.ZodType): any {
const formatted = this.format() as Record<string, any>;
if (schema instanceof z.ZodObject) {