Performance: Schema definition
Schema definition affects the initial data validation and serialization speed, impacting server response time and client hydration speed.
Jump into concepts and practice - no test required
import { z } from 'zod'; const schema = z.object({ name: z.string(), age: z.number().min(0), email: z.string().email(), address: z.object({ street: z.string(), city: z.string(), zip: z.string() }) });
import * as yup from 'yup'; const schema = yup.object().shape({ name: yup.string().required(), age: yup.number().required().min(0), email: yup.string().email().required(), address: yup.object().shape({ street: yup.string().required(), city: yup.string().required(), zip: yup.string().required() }) });
| Pattern | Server Validation Time | Bundle Size Impact | Client Hydration Impact | Verdict |
|---|---|---|---|---|
| Yup schema | 20-50ms | +15kb | Slower hydration | [X] Bad |
| Zod schema | 10-25ms | +5kb | Faster hydration | [OK] Good |
zod library in Next.js?const userSchema = z.object({
name: z.string(),
age: z.number().min(18)
});
const result = userSchema.safeParse({ name: "Alice", age: 16 });
console.log(result.success);const productSchema = z.object({
id: z.number,
title: z.string(),
price: z.number()
});