import { z } from 'zod'; const userSchema = z.object({ name: z.string().min(1), age: z.number().int().min(18), }); const result = userSchema.safeParse({ age: 20 }); console.log(result.success);
The schema requires both name and age. Since name is missing, validation fails and result.success is false.
username string and an optional email string.Option D correctly marks email as optional. Option D makes username optional which is wrong. Option D allows email to be null but not missing. Option D uses .required() which is not a Zod method.
import { z } from 'zod'; const productSchema = z.object({ id: z.number(), price: z.number().positive(), }); const data = { id: 1, price: -5 }; const result = productSchema.parse(data); console.log(result);
The price field must be positive. Since -5 is negative, parse throws an error instead of returning a result.
isValid after this Next.js schema check?isValid after validation?import { z } from 'zod'; const loginSchema = z.object({ email: z.string().email(), password: z.string().min(8), }); const input = { email: 'user@example.com', password: '1234567' }; const isValid = loginSchema.safeParse(input).success;
The password is only 7 characters but the schema requires at least 8, so validation fails and isValid is false.
Zod schemas are JavaScript objects and can be used anywhere JavaScript runs, including both server and client in Next.js, ensuring consistent validation.
