A lightweight TypeScript utility that integrates Zod schema validation with the Result
type from neverthrow for streamlined robust error handling.
npm install zod-neverthrow zod neverthrow
# or
pnpm add zod-neverthrow zod neverthrow
This package requires the following peer dependencies:
import { z } from 'zod';
import 'zod-neverthrow';
const UserSchema = z.object({
name: z.string(),
age: z.number(),
enabled: z.boolean(),
});
// Result<{ name: string; age: number; enabled: boolean; }, z.ZodError<any>>
const result = UserSchema.neverthrowParse({
name: 'John Doe',
age: 30,
enabled: true,
});
result.match(
(user) => console.log('User processed successfully:', user),
(error) => console.error('Invalid user data:', error.errors),
);
MIT License