Skip to content

Commit fb02228

Browse files
[next]: astro/zod (#12815)
Co-authored-by: Sarah Rainsberger <[email protected]>
1 parent d2e0318 commit fb02228

File tree

48 files changed

+192
-108
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+192
-108
lines changed

src/content/docs/de/tutorial/6-islands/4.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ Aktualisiere Astro und alle Integrationen auf die neuesten Versionen, indem du i
119119

120120
```ts title="src/content.config.ts"
121121
import { glob } from "astro/loaders";
122-
import { z, defineCollection } from "astro:content";
122+
import { defineCollection } from "astro:content";
123+
import { z } from "astro/zod";
123124

124125
const blog = defineCollection({
125126
loader: glob({ pattern: '**/[^_]*.md', base: "./src/blog" }),

src/content/docs/en/guides/actions.mdx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Actions are defined in a `server` object exported from `src/actions/index.ts`:
2424

2525
```ts title="src/actions/index.ts"
2626
import { defineAction } from 'astro:actions';
27-
import { z } from 'astro:schema';
27+
import { z } from 'astro/zod';
2828

2929
export const server = {
3030
myAction: defineAction({ /* ... */ })
@@ -62,11 +62,11 @@ Follow these steps to define an action and call it in a `script` tag in your Ast
6262
}
6363
```
6464

65-
2. Import the `defineAction()` utility from `astro:actions`, and the `z` object from `astro:schema`.
65+
2. Import the `defineAction()` utility from `astro:actions`, and the `z` object from `astro/zod`.
6666

6767
```ts ins={1-2} title="src/actions/index.ts"
6868
import { defineAction } from 'astro:actions';
69-
import { z } from 'astro:schema';
69+
import { z } from 'astro/zod';
7070

7171
export const server = {
7272
// action declarations
@@ -77,7 +77,7 @@ Follow these steps to define an action and call it in a `script` tag in your Ast
7777

7878
```ts ins={5-12} title="src/actions/index.ts"
7979
import { defineAction } from 'astro:actions';
80-
import { z } from 'astro:schema';
80+
import { z } from 'astro/zod';
8181

8282
export const server = {
8383
getGreeting: defineAction({
@@ -213,7 +213,7 @@ This example throws an error from a `likePost` action when a user is not logged
213213

214214
```ts title="src/actions/index.ts" ins=/ActionError(?= )/ ins={9-12}
215215
import { defineAction, ActionError } from "astro:actions";
216-
import { z } from "astro:schema";
216+
import { z } from "astro/zod";
217217

218218
export const server = {
219219
likePost: defineAction({
@@ -290,7 +290,7 @@ Actions accept JSON data by default. To accept form data from an HTML form, set
290290

291291
```ts title="src/actions/index.ts" ins={6}
292292
import { defineAction } from 'astro:actions';
293-
import { z } from 'astro:schema';
293+
import { z } from 'astro/zod';
294294

295295
export const server = {
296296
comment: defineAction({
@@ -319,7 +319,7 @@ To apply a union of different validators, use the `z.discriminatedUnion()` wrapp
319319

320320
```ts title="src/actions/index.ts" {7-21} "create" "update"
321321
import { defineAction } from 'astro:actions';
322-
import { z } from 'astro:schema';
322+
import { z } from 'astro/zod';
323323

324324
export const server = {
325325
changeUser: defineAction({
@@ -378,7 +378,7 @@ The following example shows a validated newsletter registration form that accept
378378

379379
```ts title="src/actions/index.ts" ins={5-12}
380380
import { defineAction } from 'astro:actions';
381-
import { z } from 'astro:schema';
381+
import { z } from 'astro/zod';
382382

383383
export const server = {
384384
newsletter: defineAction({
@@ -492,7 +492,7 @@ For example, say you have a `createProduct` action that returns the generated pr
492492

493493
```ts title="src/actions/index.ts" mark={10}
494494
import { defineAction } from 'astro:actions';
495-
import { z } from 'astro:schema';
495+
import { z } from 'astro/zod';
496496

497497
export const server = {
498498
createProduct: defineAction({

src/content/docs/en/guides/astro-db.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ You can also use [Astro actions](/en/guides/actions/) to insert data into an Ast
401401
// src/actions/index.ts
402402
import { db, Comment } from 'astro:db';
403403
import { defineAction } from 'astro:actions';
404-
import { z } from 'astro:schema';
404+
import { z } from 'astro/zod';
405405

406406
export const server = {
407407
addComment: defineAction({

src/content/docs/en/guides/content-collections.mdx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ Each individual collection configures:
132132
- [a build-time `schema`](#defining-the-collection-schema) for type safety (optional, but highly recommended!)
133133

134134
```ts title="src/content.config.ts"
135-
// 1. Import utilities from `astro:content`
136-
import { defineCollection, z } from 'astro:content';
135+
// 1. Import utilities from `astro:content` and `astro/zod`
136+
import { defineCollection } from 'astro:content';
137+
import { z } from 'astro/zod';
137138

138139
// 2. Import loader(s)
139140
import { glob } from 'astro/loaders';
@@ -332,7 +333,8 @@ In order for Astro to recognize a new or updated schema, you may need to restart
332333
Providing a `schema` is optional, but highly recommended! If you choose to use a schema, then every frontmatter or data property of your collection entries must be defined using a [Zod data type](#defining-datatypes-with-zod):
333334

334335
```ts title="src/content.config.ts" {6-11,15-19}
335-
import { defineCollection, z } from 'astro:content';
336+
import { defineCollection } from 'astro:content';
337+
import { z } from 'astro/zod';
336338
import { glob, file } from 'astro/loaders';
337339

338340
const blog = defineCollection({
@@ -360,11 +362,12 @@ export const collections = { blog, dogs };
360362

361363
Astro uses [Zod](https://github.com/colinhacks/zod) to power its content schemas. With Zod, Astro is able to validate every file's data within a collection *and* provide automatic TypeScript types when you query content from inside your project.
362364

363-
To use Zod in Astro, import the `z` utility from `"astro:content"`. This is a re-export of the Zod library, and it supports all of the features of Zod 3.
365+
To use Zod in Astro, import the `z` utility from `"astro/zod"`. This is a re-export of the Zod library, and it supports all of the features of Zod 3.
364366

365367
```ts
366368
// Example: A cheatsheet of many common Zod datatypes
367-
import { z, defineCollection } from 'astro:content';
369+
import { defineCollection } from 'astro:content';
370+
import { z } from 'astro/zod';
368371

369372
defineCollection({
370373
schema: z.object({
@@ -405,7 +408,8 @@ With the [`reference()` function](/en/reference/modules/astro-content/#reference
405408
A common example is a blog post that references reusable author profiles stored as JSON, or related post URLs stored in the same collection:
406409

407410
```ts title="src/content.config.ts"
408-
import { defineCollection, reference, z } from 'astro:content';
411+
import { defineCollection, reference } from 'astro:content';
412+
import { z } from 'astro/zod';
409413
import { glob } from 'astro/loaders';
410414

411415
const blog = defineCollection({

src/content/docs/en/guides/images.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,8 @@ This is a blog post
590590
The `image` helper for the content collections schema lets you validate and import the image.
591591

592592
```ts title="src/content.config.ts"
593-
import { defineCollection, z } from "astro:content";
593+
import { defineCollection } from 'astro:content';
594+
import { z } from 'astro/zod';
594595

595596
const blogCollection = defineCollection({
596597
schema: ({ image }) => z.object({

src/content/docs/en/guides/integrations-guide/mdx.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ It also adds extra features to standard MDX, including support for Markdown-styl
101101
To include your local MDX files in a content collection, make sure that your [collection loader](/en/guides/content-collections/#build-time-collection-loaders) is configured to load content from `.mdx` files:
102102

103103
```js title="src/content.config.ts" ins="mdx"
104-
import { defineCollection, z } from 'astro:content';
104+
import { defineCollection } from 'astro:content';
105+
import { z } from 'astro/zod';
105106
import { glob } from 'astro/loaders';
106107

107108
const blog = defineCollection({

src/content/docs/en/guides/integrations-guide/react.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ The example below gets the current value of likes from a counter, typed as a num
167167

168168
```ts title="actions.ts" ins={3,11}
169169
import { defineAction, type SafeResult } from 'astro:actions';
170-
import { z } from 'astro:schema';
170+
import { z } from 'astro/zod';
171171
import { getActionState } from '@astrojs/react/actions';
172172

173173
export const server = {

src/content/docs/en/guides/sessions.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ In actions, the session object is available on the `context` object. For example
9090

9191
```ts title="src/actions/addToCart.ts" "context.session"
9292
import { defineAction } from 'astro:actions';
93-
import { z } from 'astro:schema';
93+
import { z } from 'astro/zod';
9494

9595
export const server = {
9696
addToCart: defineAction({

src/content/docs/en/guides/upgrade-to/v6.mdx

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,31 @@ someLogic(build.assetsPrefix)
155155

156156
<ReadMore>Read more about the [`astro:config` virtual modules](/en/reference/modules/astro-config/).</ReadMore>
157157

158+
### Deprecated: `astro:schema` and `z` from `astro:content`
159+
160+
<SourcePR number="14923" title="feat!: consolidate zod export" />
161+
162+
In Astro 5.x, `astro:schema` was introduced as an alias of `astro/zod`. `z` was also exported from `astro:content` for convenience. However this occasionally created confusion for users who were unsure about where they should be importing from.
163+
164+
Astro 6.0 deprecates `astro:schema` and `z` from `astro:content` in favor of `astro/zod`.
165+
166+
#### What should I do?
167+
168+
Replace any occurrences of `astro:schema` with `astro/zod`:
169+
170+
```ts del={1} ins={2}
171+
import { z } from "astro:schema"
172+
import { z } from "astro/zod"
173+
```
174+
175+
Remove `z` from your `astro:content` imports and import `z` separately from `astro/zod` instead:
176+
177+
```ts title="src/content.config.ts" del={1} ins={2-3}
178+
import { defineCollection, z } from "astro:content"
179+
import { defineCollection } from "astro:content"
180+
import { z } from "astro/zod"
181+
```
182+
<ReadMore>See more about [defining collection schemas with Zod](/en/guides/content-collections/#defining-datatypes-with-zod).</ReadMore>
158183
## Removed
159184

160185
The following features have now been entirely removed from the code base and can no longer be used. Some of these features may have continued to work in your project even after deprecation. Others may have silently had no effect.
@@ -207,9 +232,10 @@ Rename and move this file to `src/content.config.ts`
207232

208233
Import [Astro's built-in `glob()` loader](/en/guides/content-collections/#the-glob-loader) and define the `pattern` and `base` for your collection entries:
209234

210-
```ts ins={3,6}
235+
```ts ins={4,7}
211236
// src/content.config.ts
212-
import { defineCollection, z } from 'astro:content';
237+
import { defineCollection } from 'astro:content';
238+
import { z } from 'astro/zod';
213239
import { glob } from 'astro/loaders';
214240
215241
const blog = defineCollection({
@@ -228,9 +254,10 @@ const blog = defineCollection({
228254
<summary>a collection that defines a collection type (`type: 'content'` or `type: 'data'`) / ([`ContentCollectionInvalidTypeError`](/en/reference/errors/content-collection-invalid-type/))</summary>
229255
There are no longer different types of collections. This must be deleted from your collection definition.
230256

231-
```ts del={7}
257+
```ts del={8}
232258
// src/content.config.ts
233-
import { defineCollection, z } from 'astro:content';
259+
import { defineCollection } from 'astro:content';
260+
import { z } from 'astro/zod';
234261
import { glob } from 'astro/loaders';
235262

236263
const blog = defineCollection({

src/content/docs/en/recipes/i18n.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ If you prefer the default language to not be visible in the URL unlike other lan
8787

8888
```ts
8989
//src/content.config.ts
90-
import { defineCollection, z } from 'astro:content';
90+
import { defineCollection } from 'astro:content';
91+
import { z } from 'astro/zod';
9192

9293
const blogCollection = defineCollection({
9394
schema: z.object({

0 commit comments

Comments
 (0)