-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add docs for astro/zod
#12828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ArmandPhilippot
merged 13 commits into
withastro:main
from
ArmandPhilippot:feat/add-docs-for-astro-zod
Dec 9, 2025
Merged
Add docs for astro/zod
#12828
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
253a071
add astro/zod to the sidebar
ArmandPhilippot 5def05b
add docs for imports
ArmandPhilippot 614954d
fix typo
ArmandPhilippot 7788c5f
more descriptive module introduction
ArmandPhilippot a946776
add details about what is z and how it can be used
ArmandPhilippot de4345e
replace external links/add links to the reference page
ArmandPhilippot 52fe24f
fix link
ArmandPhilippot d358e58
replace `astro:content` with `astro/zod`
ArmandPhilippot dbb2cdc
do not import Zod from `astro:content`
ArmandPhilippot a8a772a
do not import Zod from `astro:schema`
ArmandPhilippot b3dc244
remove unused recipe docs commented since 2-3 years
ArmandPhilippot e2dba27
no legacy mention
ArmandPhilippot 59ec3aa
Merge branch 'main' into feat/add-docs-for-astro-zod
ArmandPhilippot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| --- | ||
| title: Zod API Reference | ||
| sidebar: | ||
| label: 'astro/zod' | ||
| i18nReady: true | ||
| tableOfContents: | ||
| minHeadingLevel: 2 | ||
| maxHeadingLevel: 6 | ||
| --- | ||
| import ReadMore from '~/components/ReadMore.astro'; | ||
|
|
||
| [Zod](https://github.com/colinhacks/zod) is a TypeScript-based schema declaration and validation library. This allows you to define schemas you can use to validate data and transform data, from a simple type (e.g. `string`, `number`) to complex data structures (e.g. nested objects). | ||
|
|
||
| The `astro/zod` module exposes a re-export of Zod that gives you access to all the features of Zod v3. By using this module, you do not need to install Zod yourself. This also ensures that your project uses the same API versions as Astro when using features such as [Content Collections](/en/guides/content-collections/) or [Actions](/en/guides/actions/). | ||
|
|
||
| <ReadMore>See [Zod v3 website](https://v3.zod.dev/) for complete documentation on how Zod works and what features are available.</ReadMore> | ||
|
|
||
| ## Imports from `astro/zod` | ||
|
|
||
| ```ts | ||
| import { z } from 'astro/zod'; | ||
| ``` | ||
|
|
||
| ### `z` | ||
|
|
||
| <p> | ||
|
|
||
| **Type:** `object` | ||
| </p> | ||
|
|
||
| The `z` utility gives you access to validators for a wide range of data types, methods and types for working with your data. | ||
|
|
||
| <ReadMore>Learn more about the `z` utility in [Zod documentation](https://v3.zod.dev/?id=basic-usage)</ReadMore> | ||
|
|
||
| #### Common data type validators | ||
|
|
||
| With Zod, you can validate any type of data, such as [primitives](https://v3.zod.dev/?id=primitives), [objects](https://v3.zod.dev/?id=objects), [arrays](https://v3.zod.dev/?id=arrays) and more. | ||
|
|
||
| The following example shows a cheatsheet of many common Zod data types to create a `user` schema: | ||
|
|
||
| ```ts | ||
| import { z } from 'astro/zod'; | ||
|
|
||
| const user = z.object({ | ||
| username: z.string(), | ||
| name: z.string().min(2), | ||
| email: z.string().email(), | ||
| role: z.enum(["admin", "editor"]), | ||
| language: z.enum(["en", "fr", "es"]).default("en"), | ||
| hobbies: z.array(z.string()), | ||
| age: z.number(), | ||
| isEmailConfirmed: z.boolean(), | ||
| inscriptionDate: z.date(), | ||
| website: z.string().url().optional(), | ||
| }); | ||
| ``` | ||
|
|
||
| #### Extracting a Typescript type | ||
|
|
||
| Zod allows you to create a Typescript type from any schema [using Zod type inference](https://v3.zod.dev/?id=type-inference). This can be useful for describing an expected data structure when [defining component props](/en/guides/typescript/#component-props). | ||
|
|
||
| The following example create a `User` type based on the previous schema: | ||
|
|
||
| ```ts | ||
| type User = z.infer<typeof user>; | ||
|
|
||
| /* The `User` type will be: | ||
| * type User = { | ||
| * username: string; | ||
| * name: string; | ||
| * email: string; | ||
| * role: "admin" | "editor"; | ||
| * language: "en" | "fr" | "es"; | ||
| * hobbies: string[]; | ||
| * age: number; | ||
| * isEmailConfirmed: boolean; | ||
| * inscriptionDate: Date; | ||
| * website?: string | undefined; | ||
| * } | ||
| */ | ||
| ``` | ||
|
|
||
| #### Using Zod methods | ||
|
|
||
| Zod provides various [schema methods](https://v3.zod.dev/?id=schema-methods) to customize error messages, transform data, or create custom validation logics. | ||
|
|
||
| ```ts | ||
| // Customize the error message | ||
| const nonEmptyStrings = z.string().array().nonempty({ | ||
| message: "Can't be empty!", | ||
| }); | ||
|
|
||
| // Validate a data from a schema | ||
| nonEmptyStrings.parse([]); // will throws our custom error | ||
|
|
||
| // Create an object from a URL for a decorative img | ||
| const decorativeImg = z.string().transform((value) => { | ||
| return { src: value, alt: "" }; | ||
| }); | ||
|
|
||
| // Create a custom validator and error message for a string | ||
| const constrainedString = z | ||
| .string() | ||
| .refine((val) => val.length > 0 && val.length <= 255, { | ||
| message: "Must be between 1 and 255 characters.", | ||
| }); | ||
| ``` | ||
|
|
||
| ### Individual imports | ||
|
|
||
| Alternatively, you can import all the Zod validators, methods and types available in the [`z` utility](#z) directly from the module. | ||
|
|
||
| The following example imports `coerce` to create a `Date` object from a date string: | ||
|
|
||
| ```ts | ||
| import { coerce } from 'astro/zod'; | ||
|
|
||
| const publishedOn = coerce.date(); | ||
| const publicationDate = publishedOn.parse("2025-12-03"); | ||
| ``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.