From b83c01dc797fc8b5f7cdcfce7c57e2823c5e082f Mon Sep 17 00:00:00 2001 From: liruifengv Date: Sat, 13 Dec 2025 19:04:27 +0800 Subject: [PATCH 1/2] i18n(zh-cn): Update `zod` related pages. --- src/content/docs/zh-cn/guides/astro-db.mdx | 2 +- .../docs/zh-cn/guides/content-collections.mdx | 62 ++++++------------- src/content/docs/zh-cn/guides/images.mdx | 3 +- .../zh-cn/guides/integrations-guide/mdx.mdx | 3 +- .../zh-cn/guides/integrations-guide/react.mdx | 2 +- src/content/docs/zh-cn/guides/sessions.mdx | 2 +- .../docs/zh-cn/recipes/build-forms-api.mdx | 26 -------- src/content/docs/zh-cn/recipes/i18n.mdx | 3 +- .../reference/content-loader-reference.mdx | 9 +-- .../docs/zh-cn/reference/legacy-flags.mdx | 2 +- .../zh-cn/reference/modules/astro-content.mdx | 3 +- .../docs/zh-cn/tutorial/6-islands/4.mdx | 4 +- 12 files changed, 38 insertions(+), 83 deletions(-) diff --git a/src/content/docs/zh-cn/guides/astro-db.mdx b/src/content/docs/zh-cn/guides/astro-db.mdx index 7795c8aa5f755..314811ad3721a 100644 --- a/src/content/docs/zh-cn/guides/astro-db.mdx +++ b/src/content/docs/zh-cn/guides/astro-db.mdx @@ -401,7 +401,7 @@ const comments = await db.select().from(Comment); // src/actions/index.ts import { db, Comment } from 'astro:db'; import { defineAction } from 'astro:actions'; -import { z } from 'astro:schema'; +import { z } from 'astro/zod'; export const server = { addComment: defineAction({ // Actions 用 Zod 保证类型安全,不需要再 diff --git a/src/content/docs/zh-cn/guides/content-collections.mdx b/src/content/docs/zh-cn/guides/content-collections.mdx index 4898982009755..296b55a5bead9 100644 --- a/src/content/docs/zh-cn/guides/content-collections.mdx +++ b/src/content/docs/zh-cn/guides/content-collections.mdx @@ -66,16 +66,19 @@ Astro 5.0 引入了内容层(Content Layer)API,用于定义和查询内容 ```ts title="src/content.config.ts" // 1. 从 `astro:content` 导入工具函数 -import { defineCollection, z } from 'astro:content'; +import { defineCollection } from 'astro:content'; // 2. 导入加载器 import { glob, file } from 'astro/loaders'; -// 3. 定义你的集合 +// 3. 导入 Zod +import { z } from 'astro/zod'; + +// 4. 定义你的集合 const blog = defineCollection({ /* ... */ }); const dogs = defineCollection({ /* ... */ }); -// 4. 导出一个 `collections` 对象来注册你的集合 +// 5. 导出一个 `collections` 对象来注册你的集合 export const collections = { blog, dogs }; ``` @@ -91,9 +94,10 @@ Astro 提供了 [两个内置的加载器函数(`glob()` 和 `file()`)](/zh- [`file()` 加载器](/zh-cn/reference/content-loader-reference/#file-加载器) 从单个本地文件创建多个条目。文件中的每个条目必须有一个唯一的 `id` 键属性。它接受一个 相对你的文件的 `base` 文件路径,以及一个可选的 [`parser` 函数](#parser-函数) 用于它无法自动解析的数据文件。当你的数据文件可以解析为对象数组时,请使用此加载器。 -```ts title="src/content.config.ts" {5,9} -import { defineCollection, z } from 'astro:content'; +```ts title="src/content.config.ts" {6,10} +import { defineCollection } from 'astro:content'; import { glob, file } from 'astro/loaders'; // 不适用于旧版 API +import { z } from 'astro/zod'; const blog = defineCollection({ loader: glob({ pattern: "**/*.md", base: "./src/data/blog" }), @@ -210,11 +214,12 @@ const countries = defineCollection({ 为了让 Astro 识别新的或更新后的模式,你可能需要重启开发服务器,或者 [同步内容层](/zh-cn/reference/cli-reference/#astro-dev) (s + enter) 来定义 `astro:content` 模块。 ::: -集合条目的每个 frontmatter 或数据属性都必须使用 Zod 数据类型定义: +集合条目的每个 frontmatter 或数据属性都必须使用 [Zod 数据类型](/zh-cn/reference/modules/astro-zod/#common-data-type-validators)定义: -```ts title="src/content.config.ts" {6-11,15-19} -import { defineCollection, z } from 'astro:content'; -import { glob, file } from 'astro/loaders'; // 不适用于旧版 API +```ts title="src/content.config.ts" {7-12,16-20} +import { defineCollection } from 'astro:content'; +import { glob, file } from 'astro/loaders'; +import { z } from 'astro/zod'; const blog = defineCollection({ loader: glob({ pattern: "**/*.md", base: "./src/data/blog" }), @@ -241,43 +246,13 @@ export const collections = { blog, dogs }; Astro 使用 [Zod](https://github.com/colinhacks/zod) 来支持其内容模式。使用 Zod,Astro 能够验证集合中每个文件的数据,并在你查询内容时提供自动的 TypeScript 类型。 -要在 Astro 中使用 Zod,请从 `"astro:content"` 导入 `z` 工具函数。这是 Zod 库的重新导出,支持 Zod 的所有功能。 - -```ts -// 示例:许多常见 Zod 数据类型的备忘单 -import { z, defineCollection } from 'astro:content'; - -defineCollection({ - schema: z.object({ - isDraft: z.boolean(), - title: z.string(), - sortOrder: z.number(), - image: z.object({ - src: z.string(), - alt: z.string(), - }), - author: z.string().default('Anonymous'), - language: z.enum(['en', 'es']), - tags: z.array(z.string()), - footnote: z.string().optional(), - - // 在 YAML 中,不带引号的日期被解释为 Date 对象 - publishDate: z.date(), // 例如 2024-09-17 - - // 将日期字符串(例如 “2022-07-08”)转换为 Date 对象 - updatedDate: z.string().transform((str) => new Date(str)), - - authorContact: z.string().email(), - canonicalURL: z.string().url(), - }) -}) -``` +要在 Astro 中使用 Zod,请从 `"astro/zod"` 导入 `z` 工具函数。这是 Zod 库的重新导出,它支持 Zod 的所有功能。 -有关 Zod 如何工作及其可用功能的完整文档,请参阅 [Zod 的 README](https://github.com/colinhacks/zod)。 +请参阅 [`z` 实用参考](/zh-cn/reference/modules/astro-zod/) 以查看常用数据类型速查表并了解 Zod 的工作原理及可用功能。 ##### Zod 模式方法 -所有 [Zod 模式方法](https://zod.dev/?id=schema-methods)(例如 `.parse()`、`.transform()`)都可用,但有一些限制。特别是,使用 `image().refine()` 对图像执行自定义验证检查是不支持的。 +所有 [Zod 模式方法](/en/reference/modules/astro-zod/#using-zod-methods)(例如 `.parse()`、`.transform()`)都可用,但有一些限制。特别是,使用 `image().refine()` 对图像执行自定义验证检查是不支持的。 #### 定义集合引用 @@ -288,8 +263,9 @@ defineCollection({ 一个常见的例子是一个引用存储为 JSON 的可重用作者配置文件的博客文章,或者存储在同一集合中的相关文章 URL: ```ts title="src/content.config.ts" -import { defineCollection, reference, z } from 'astro:content'; +import { defineCollection, reference } from 'astro:content'; import { glob } from 'astro/loaders'; +import { z } from 'astro/zod'; const blog = defineCollection({ loader: glob({ pattern: '**/[^_]*.md', base: "./src/data/blog" }), diff --git a/src/content/docs/zh-cn/guides/images.mdx b/src/content/docs/zh-cn/guides/images.mdx index 864f58aaf9ac2..15b54d054daa5 100644 --- a/src/content/docs/zh-cn/guides/images.mdx +++ b/src/content/docs/zh-cn/guides/images.mdx @@ -589,7 +589,8 @@ coverAlt: "一张山脉后面的日落照片。" 内容集合 schema 中的 `image` 助手允许你验证并导入图像。 ```ts title="src/content.config.ts" -import { defineCollection, z } from "astro:content"; +import { defineCollection } from "astro:content"; +import { z } from "astro/zod"; const blogCollection = defineCollection({ schema: ({ image }) => z.object({ diff --git a/src/content/docs/zh-cn/guides/integrations-guide/mdx.mdx b/src/content/docs/zh-cn/guides/integrations-guide/mdx.mdx index d6c037fa4ccdc..af42fefef6dbc 100644 --- a/src/content/docs/zh-cn/guides/integrations-guide/mdx.mdx +++ b/src/content/docs/zh-cn/guides/integrations-guide/mdx.mdx @@ -100,8 +100,9 @@ export default defineConfig({ 要将 MDX 文件包含在内容集合中,请确保你的 [集合 loader](/zh-cn/guides/content-collections/#定义集合-loader) 配置为从 `.mdx` 文件加载内容: ```js title="src/content.config.ts" ins="mdx" -import { defineCollection, z } from 'astro:content'; +import { defineCollection } from 'astro:content'; import { glob } from 'astro/loaders'; +import { z } from 'astro/zod'; const blog = defineCollection({ loader: glob({ pattern: "**/*.{md,mdx}", base: "./src/blog" }), diff --git a/src/content/docs/zh-cn/guides/integrations-guide/react.mdx b/src/content/docs/zh-cn/guides/integrations-guide/react.mdx index 2e1de40d51911..39e4a78b1dfc3 100644 --- a/src/content/docs/zh-cn/guides/integrations-guide/react.mdx +++ b/src/content/docs/zh-cn/guides/integrations-guide/react.mdx @@ -166,7 +166,7 @@ export function Like({ postId }: { postId: string }) { ```ts title="actions.ts" ins={3,11} import { defineAction, type SafeResult } from 'astro:actions'; -import { z } from 'astro:schema'; +import { z } from 'astro/zod'; import { getActionState } from '@astrojs/react/actions'; export const server = { diff --git a/src/content/docs/zh-cn/guides/sessions.mdx b/src/content/docs/zh-cn/guides/sessions.mdx index 3ec35f4b48ca4..27f8b1b8b25d2 100644 --- a/src/content/docs/zh-cn/guides/sessions.mdx +++ b/src/content/docs/zh-cn/guides/sessions.mdx @@ -87,7 +87,7 @@ export async function POST(context: APIContext) { ```ts title="src/actions/addToCart.ts" "context.session" import { defineAction } from 'astro:actions'; -import { z } from 'astro:schema'; +import { z } from 'astro/zod'; export const server = { addToCart: defineAction({ diff --git a/src/content/docs/zh-cn/recipes/build-forms-api.mdx b/src/content/docs/zh-cn/recipes/build-forms-api.mdx index f127e892300b0..cc39f0075935f 100644 --- a/src/content/docs/zh-cn/recipes/build-forms-api.mdx +++ b/src/content/docs/zh-cn/recipes/build-forms-api.mdx @@ -429,29 +429,3 @@ import PackageManagerTabs from "~/components/tabs/PackageManagerTabs.astro"; - -{/* ## Extension: Use Zod to validate your form - -[Zod form data](https://www.npmjs.com/package/zod-form-data) builds on top of [Zod](https://github.com/colinhacks/zod) to validate your form using a schema. This simplifies your code, as it allows you to declare the fields and their requirements, and let Zod handle the validation. - -1. Install `zod` and `zod-form-data`. - - - - ```shell - npm i zod zod-form-data - ``` - - - ```shell - pnpm i zod zod-form-data - ``` - - - ```shell - yarn add zod zod-form-data - ``` - - - -2. In your API Route file, declare your schema using `zfd.formData` and export it. */} diff --git a/src/content/docs/zh-cn/recipes/i18n.mdx b/src/content/docs/zh-cn/recipes/i18n.mdx index 2db260f868956..3ddb2e30777af 100644 --- a/src/content/docs/zh-cn/recipes/i18n.mdx +++ b/src/content/docs/zh-cn/recipes/i18n.mdx @@ -87,7 +87,8 @@ import StaticSsrTabs from '~/components/tabs/StaticSsrTabs.astro'; ```ts //src/content.config.ts - import { defineCollection, z } from 'astro:content'; + import { defineCollection } from 'astro:content'; + import { z } from 'astro/zod'; const blogCollection = defineCollection({ schema: z.object({ diff --git a/src/content/docs/zh-cn/reference/content-loader-reference.mdx b/src/content/docs/zh-cn/reference/content-loader-reference.mdx index 90d9bb832ed2a..0211d032120aa 100644 --- a/src/content/docs/zh-cn/reference/content-loader-reference.mdx +++ b/src/content/docs/zh-cn/reference/content-loader-reference.mdx @@ -182,7 +182,7 @@ const countries = defineCollection({ ```ts title=loader.ts import type { Loader, LoaderContext } from 'astro/loaders'; -import { z } from 'astro:content'; +import { z } from 'astro/zod'; import { loadFeedData } from "./feed.js"; // 定义加载器需要的任何选项 @@ -208,9 +208,10 @@ export function myLoader(options: { url: string, apiKey: string }): Loader { 然后就可以在定义集合时设置这些配置选项: -```ts title="src/content.config.ts" {2,5-8} -import { defineCollection, z } from 'astro:content'; -import myLoader from '../../loader.ts'; +```ts title="src/content.config.ts" {3,6-9} +import { defineCollection } from 'astro:content'; +import { z } from 'astro/zod'; +import myLoader from '../../loader.ts'; const blog = defineCollection({ loader: myLoader({ diff --git a/src/content/docs/zh-cn/reference/legacy-flags.mdx b/src/content/docs/zh-cn/reference/legacy-flags.mdx index 33972dbcdc368..15b7b31adfed9 100644 --- a/src/content/docs/zh-cn/reference/legacy-flags.mdx +++ b/src/content/docs/zh-cn/reference/legacy-flags.mdx @@ -42,7 +42,7 @@ export default defineConfig({ ```js // src/content/config.ts -import { defineCollection, z } from 'astro:content'; +import { defineCollection } from 'astro:content'; const blog = defineCollection({ }) diff --git a/src/content/docs/zh-cn/reference/modules/astro-content.mdx b/src/content/docs/zh-cn/reference/modules/astro-content.mdx index 8e7205f8f2d87..9198542f1ba81 100644 --- a/src/content/docs/zh-cn/reference/modules/astro-content.mdx +++ b/src/content/docs/zh-cn/reference/modules/astro-content.mdx @@ -18,7 +18,6 @@ import ReadMore from '~/components/ReadMore.astro'; ```js import { - z, defineCollection, getCollection, getEntry, @@ -77,7 +76,7 @@ export const collections = { blog };

-`schema` 是一个可选的 Zod 对象,用于配置集合的文档 frontmatter 的类型和形状。每个值必须使用 [Zod 验证器](https://github.com/colinhacks/zod) +`schema` 是一个可选的 Zod 对象,用于配置集合的文档 frontmatter 的类型和形状。每个值必须使用 [Zod 验证器](/zh-cn/reference/modules/astro-zod/#common-data-type-validators) 更多有关示例请 [参考`内容集合`指南](/zh-cn/guides/content-collections/#定义集合模式schema)。 diff --git a/src/content/docs/zh-cn/tutorial/6-islands/4.mdx b/src/content/docs/zh-cn/tutorial/6-islands/4.mdx index 695af8baf0fe6..969088d139c04 100644 --- a/src/content/docs/zh-cn/tutorial/6-islands/4.mdx +++ b/src/content/docs/zh-cn/tutorial/6-islands/4.mdx @@ -119,7 +119,9 @@ import { Steps } from '@astrojs/starlight/components'; // 导入 glob 加载器(loader) import { glob } from "astro/loaders"; // 从 `astro:content` 导入工具函数 - import { z, defineCollection } from "astro:content"; + import { defineCollection } from "astro:content"; + // 导入 Zod + import { z } from "astro/zod"; // 为每个集合定义一个 `loader` 和 `schema` const blog = defineCollection({ loader: glob({ pattern: '**/[^_]*.md', base: "./src/blog" }), From ca2e96dc9f7063c67b4e740b61b8b2b59cc31be5 Mon Sep 17 00:00:00 2001 From: liruifengv Date: Sun, 14 Dec 2025 13:51:00 +0800 Subject: [PATCH 2/2] Fix broken link --- src/content/docs/zh-cn/guides/content-collections.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/zh-cn/guides/content-collections.mdx b/src/content/docs/zh-cn/guides/content-collections.mdx index 296b55a5bead9..2eed4e545b7f4 100644 --- a/src/content/docs/zh-cn/guides/content-collections.mdx +++ b/src/content/docs/zh-cn/guides/content-collections.mdx @@ -252,7 +252,7 @@ Astro 使用 [Zod](https://github.com/colinhacks/zod) 来支持其内容模式 ##### Zod 模式方法 -所有 [Zod 模式方法](/en/reference/modules/astro-zod/#using-zod-methods)(例如 `.parse()`、`.transform()`)都可用,但有一些限制。特别是,使用 `image().refine()` 对图像执行自定义验证检查是不支持的。 +所有 [Zod 模式方法](/zh-cn/reference/modules/astro-zod/#using-zod-methods)(例如 `.parse()`、`.transform()`)都可用,但有一些限制。特别是,使用 `image().refine()` 对图像执行自定义验证检查是不支持的。 #### 定义集合引用