Think of a Zod Schema as a rulebook for your data. It’s where you define exactly what a piece of information should look like. For example, a “Sign Up” schema could have these rules:
- An
email
field must be provided, and it must look like a real email address. - A
password
field must be provided, and it must be at least 8 characters long.
Why Use Zod Schemas?
Zod Schemas are a versatile tool that you will use in many different parts of your application.Powering Your Forms
This is the most common use case. By linking a Form element to a Zod Schema,
you automatically get data structure and validation. The form will know
which fields are required, what type of data to expect, and will even show
error messages for you.
Validating Data in Workflows
Using the
Validate Data
action, you can check if any piece of data (e.g.,
from an API call) matches your schema’s rules before you try to save it to
your database, ensuring data integrity.Structuring AI Outputs
When working with the AI SDK, you can provide a Zod
Schema to an AI model. This forces the AI to return its response in a
perfectly structured JSON format that matches your rules, making AI outputs
reliable and easy to work with.
Creating a Zod Schema
Let’s create a simpleSignUpSchema
to define the rules for a user registration form.
1
1. Navigate to the Zod Schemas Tab
In the top navigation bar, click the Data tab, then select the “Zod Schemas” sub-tab.
2
2. Create a New Schema
Enter a name for your schema (e.g.,
SignUpSchema
) and click “Create”.3
3. Add Fields and Validation Rules
With your new schema selected, click ”+ Add new field”. For each field, you must define its rules in the properties panel.
Field Validation Rules
For each field in your schema, you can define:- Field Name: The name of the property (e.g.,
email
). - Data Type: The type of data, such as
Text
,Number
, orBoolean
. - Validation Rules: A set of rules to enforce, such as:
- Required: Is this field mandatory?
- Minimum / Maximum Length: For text fields.
- Email Format: Checks if the text is a valid email address.
- Custom Error Message: You can write your own error message that will be shown to the user if their input is invalid.
SignUpSchema
Structure
Here is how you would configure the fields for our SignUpSchema
.
Field Name | Data Type | Key Validation Rules | Error Message Example |
---|---|---|---|
name | Text | Required, Minimum length: 2 | ”Name must be at least 2 characters.” |
email | Email | Required | ”Please enter a valid email address.” |
password | Text | Required, Minimum length: 8 | ”Password must be 8+ characters.” |