0
0
NextJSframework~10 mins

Schema definition in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Schema definition
Define schema object
Specify fields and types
Add validation rules
Export schema for use
Use schema to validate data
Handle validation result
This flow shows how to create a schema, add rules, export it, and use it to check data.
Execution Sample
NextJS
import { z } from 'zod';

const userSchema = z.object({
  name: z.string(),
  age: z.number().int().positive()
});

export default userSchema;
Defines a schema for a user with a name string and a positive integer age.
Execution Table
StepActionEvaluationResult
1Import zod libraryz importedz object available
2Define userSchema with name and agez.object calleduserSchema created
3Add name field as stringz.string()name field validated as string
4Add age field as positive integerz.number().int().positive()age field validated as positive int
5Export userSchemaexport statementschema ready for use
6Validate data {name: 'Alice', age: 30}userSchema.parse()passes validation
7Validate data {name: 'Bob', age: -5}userSchema.parse()throws validation error
8Validate data {name: 123, age: 20}userSchema.parse()throws validation error
💡 Validation stops when data matches schema or error is thrown
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 6After Step 7After Step 8
userSchemaundefinedobject with fieldsexported schema objectvalidates Alice datathrows error for negative agethrows error for wrong name type
Key Moments - 2 Insights
Why does validation fail when age is negative?
Because the schema requires age to be positive as shown in step 4 and step 7 of the execution_table.
Why does validation fail when name is a number?
Because the schema expects name to be a string, so passing a number breaks the rule as shown in step 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of validating {name: 'Alice', age: 30} at step 6?
Apasses validation
Bthrows validation error
Cskips validation
Dreturns undefined
💡 Hint
Check the 'Result' column at step 6 in execution_table
At which step does the schema add the rule that age must be a positive integer?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the 'Action' and 'Evaluation' columns in execution_table for age field
If we change the age rule to allow zero, how would the validation result change for age 0?
AValidation would still fail for age 0
BValidation would pass for age 0
CValidation would throw a different error
DValidation would ignore age field
💡 Hint
Consider the positive() rule in step 4 and how removing it affects validation
Concept Snapshot
Schema definition in Next.js uses libraries like zod.
Define a schema object with fields and types.
Add validation rules like string(), number(), positive().
Export schema and use parse() to validate data.
Validation passes if data matches rules, else throws error.
Full Transcript
In Next.js, schema definition is done by creating an object that describes the shape and rules for data. We import a library like zod, then define a schema object with fields such as name and age. Each field has a type and validation rules, for example, name must be a string and age must be a positive integer. After defining, we export the schema to use it elsewhere. When validating data, we call parse() on the schema with the data. If the data matches the schema, validation passes. If not, it throws an error. This helps ensure data is correct before using it in the app.