Back to results
jbt95/ts-zod-decorators(opens in a new tab)
Language: TypeScript
Zod decorators for typescript
- stars
- 5
- watchers
- 1
- forks
- 1
- open issues
- 1
- Last updated
- 01/21/2026
Language: TypeScript
Zod decorators for typescript
This repository enhances the functionality of the Zod library by providing validation decorators. Zod is a TypeScript-first schema declaration and validation library. The added decorators make it easier to validate data against predefined schemas in TypeScript projects.
npm install ts-zod-decorators
pnpm add ts-zod-decorators
import { ZodInput, Validate } from 'ts-zod-decorators'; import * as z from 'zod'; const schema = z.object({ name: z.string(), age: z.number() }); class Foo { @Validate public bar(@ZodInput(schema) input: z.infer<typeof schema>) {} }
import { ZodInput, Validate, ZodOutput } from 'ts-zod-decorators'; import * as z from 'zod'; const schema = z.object({ name: z.string(), age: z.number() }); class Foo { @Validate @ZodOutput(schema) public bar(input: unknown): Promise<z.infer<schema>> { return Promise.resolve({ name: 'foo', age: 1 }); } }
import { Validate } from 'ts-zod-decorators'; class Foo { @Validate({ input: 'safeParse', output: 'parseAsync', mode: 'collect-all', onError: (err) => new Error(`validation failed: ${String(err)}`), }) public async bar() { return { ok: true }; } }
import { Validate, ZodInputs } from 'ts-zod-decorators'; import * as z from 'zod'; const schema = z.object({ name: z.string(), age: z.number() }); class Foo { @Validate @ZodInputs(schema, { name: 0, age: 1 }) public async bar(name: string, age: number) {} }
import { setValidationConfig } from 'ts-zod-decorators'; setValidationConfig({ enabled: false });
import { createExpressValidator, formatZodError, getValidationMetadata } from 'ts-zod-decorators'; import * as z from 'zod'; const middleware = createExpressValidator(z.object({ name: z.string() }), 'body'); const metadata = getValidationMetadata(Foo.prototype); const prettyError = formatZodError(new z.ZodError([]));
@Validate can be used as @Validate or @Validate({ ...options }).TS_ZOD_DECORATORS_ENABLED=0 to disable validation globally.