Schema definition helps you describe the shape and rules of your data clearly. It makes sure your app uses data correctly and avoids errors.
Schema definition in NextJS
Start learning this pattern below
Jump into concepts and practice - no test required
import { z } from 'zod'; const schema = z.object({ name: z.string(), age: z.number().int().positive(), email: z.string().email().optional() });
This example uses zod, a popular schema library in Next.js projects.
Each field defines the expected type and rules, like string, number, or optional fields.
const userSchema = z.object({ username: z.string().min(3), password: z.string().min(8) });
const productSchema = z.object({
id: z.string(),
price: z.number().nonnegative(),
tags: z.array(z.string()).optional()
});const responseSchema = z.union([ z.object({ success: z.literal(true), data: z.any() }), z.object({ success: z.literal(false), error: z.string() }) ]);
This code defines a user schema and checks if userData matches it. If valid, it prints the user data. If not, it shows errors.
import { z } from 'zod'; const userSchema = z.object({ name: z.string(), age: z.number().int().positive(), email: z.string().email().optional() }); // Example data to validate const userData = { name: 'Alice', age: 30, email: 'alice@example.com' }; try { const validatedUser = userSchema.parse(userData); console.log('Valid user:', validatedUser); } catch (e) { console.log('Validation error:', e.errors); }
Always validate data before using it to avoid bugs and security issues.
Schema libraries like zod integrate well with Next.js and TypeScript for better developer experience.
Optional fields let you accept data that may or may not be present.
Schema definition describes the shape and rules of your data.
It helps catch errors early by validating data before use.
Using libraries like zod makes schema definition easy and clear in Next.js projects.
Practice
Solution
Step 1: Understand schema definition role
Schema definition is about specifying how data should look and behave.Step 2: Identify its main use in Next.js
It helps validate data to catch errors early before using it in the app.Final Answer:
To describe the shape and rules of your data -> Option BQuick Check:
Schema = Data shape and rules [OK]
- Confusing schema with UI styling
- Thinking schema manages routing
- Assuming schema handles rendering
zod library in Next.js?Solution
Step 1: Recall zod syntax for string schema
In zod, string schema is created by calling z.string() as a function.Step 2: Check each option's syntax
const schema = z.string(); uses z.string() correctly. Others miss parentheses or use wrong casing or new keyword.Final Answer:
const schema = z.string(); -> Option AQuick Check:
z.string() is correct syntax [OK]
- Omitting parentheses after z.string
- Using uppercase 'String' instead of 'string'
- Using 'new' keyword incorrectly
const userSchema = z.object({
name: z.string(),
age: z.number().min(18)
});
const result = userSchema.safeParse({ name: "Alice", age: 16 });
console.log(result.success);What will be logged to the console?
Solution
Step 1: Understand schema rules
The schema requires 'name' as string and 'age' as number at least 18.Step 2: Check input data against schema
Input has age 16, which is less than minimum 18, so validation fails.Final Answer:
false -> Option CQuick Check:
Validation fails because age < 18 [OK]
- Assuming validation passes despite age < 18
- Confusing safeParse result with direct parse
- Expecting an error instead of false success
const productSchema = z.object({
id: z.number,
title: z.string(),
price: z.number()
});Solution
Step 1: Check each property schema syntax
'id' uses z.number without parentheses, which is incorrect syntax.Step 2: Verify other properties and object structure
Other properties are correct; object schema is correctly defined as an object, not array.Final Answer:
Missing parentheses after z.number for 'id' -> Option DQuick Check:
z.number() needs parentheses [OK]
- Forgetting parentheses after z.number or z.string
- Thinking schema must be an array
- Missing commas between properties
Solution
Step 1: Understand optional email schema in zod
To make a field optional but validate if present, use .optional() after .email().Step 2: Check each option's method chaining
const userProfileSchema = z.object({ email: z.string().email().optional() }); correctly chains z.string().email().optional(). const userProfileSchema = z.object({ email: z.optional(z.string().email) }); uses z.optional incorrectly. const userProfileSchema = z.object({ email: z.string().optional().email() }); calls optional before email, which breaks validation. const userProfileSchema = z.object({ email: z.string().email() || undefined }); uses invalid syntax.Final Answer:
const userProfileSchema = z.object({ email: z.string().email().optional() }); -> Option AQuick Check:
Use .optional() after .email() for optional validated fields [OK]
- Placing .optional() before .email()
- Using z.optional() wrapper incorrectly
- Trying to use || undefined for optional
