.parse vs .safeParse
.parse() throws a ZodError on invalid input. If it is not caught, your app crashes. .safeParse() always returns a result object. The output below uses safeParse. Try making the input valid to see the success path.
import { z } from 'zod';
const schema = z.object({ name: z.string(), age: z.number() });
// ❌ Throws on invalid input — crashes if uncaught
const data = schema.parse(input);
// ✅ Always returns { success, data | error }
const result = schema.safeParse(input);
if (result.success) {
console.log(result.data); // typed and safe
} else {
console.log(result.error); // ZodError with issues
}// safeParse return type:
type SafeParseResult<T> =
| { success: true; data: T }
| { success: false; error: ZodError }- age