parse() vs safeParse() in Zod
From the Zod cheat sheet ยท Parsing & Error Handling ยท verified Jul 2026
parse() vs safeParse()
Throwing and non-throwing parse methods
typescript
// parse - throws on error
try {
const user = UserSchema.parse(data);
} catch (err) {
if (err instanceof z.ZodError) {
console.log(err.issues);
}
}
// safeParse - returns result object
const result = UserSchema.safeParse(data);
if (result.success) {
console.log(result.data);
} else {
console.log(result.error.issues);
}๐ก Prefer safeParse() in API routes โ no try/catch, cleaner control flow
โก z.flattenError() is perfect for form validation โ gives field-level error arrays
๐ Use parseAsync() when your schema has async refinements like database lookups
๐ข ZodError.issues is an array โ each issue has code, path, message for granular handling
parsesafeParseerrors