Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/content/docs/zh-cn/guides/astro-db.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 保证类型安全,不需要再
Expand Down
62 changes: 19 additions & 43 deletions src/content/docs/zh-cn/guides/content-collections.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
```

Expand All @@ -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" }),
Expand Down Expand Up @@ -210,11 +214,12 @@ const countries = defineCollection({
为了让 Astro 识别新的或更新后的模式,你可能需要重启开发服务器,或者 [同步内容层](/zh-cn/reference/cli-reference/#astro-dev) (<code>s + enter</code>) 来定义 `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" }),
Expand All @@ -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 的所有功能。

<ReadMore>有关 Zod 如何工作及其可用功能的完整文档,请参阅 [Zod 的 README](https://github.com/colinhacks/zod)。</ReadMore>
<ReadMore>请参阅 [`z` 实用参考](/zh-cn/reference/modules/astro-zod/) 以查看常用数据类型速查表并了解 Zod 的工作原理及可用功能。</ReadMore>

##### Zod 模式方法

所有 [Zod 模式方法](https://zod.dev/?id=schema-methods)(例如 `.parse()`、`.transform()`)都可用,但有一些限制。特别是,使用 `image().refine()` 对图像执行自定义验证检查是不支持的。
所有 [Zod 模式方法](/zh-cn/reference/modules/astro-zod/#using-zod-methods)(例如 `.parse()`、`.transform()`)都可用,但有一些限制。特别是,使用 `image().refine()` 对图像执行自定义验证检查是不支持的。

#### 定义集合引用

Expand All @@ -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" }),
Expand Down
3 changes: 2 additions & 1 deletion src/content/docs/zh-cn/guides/images.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
3 changes: 2 additions & 1 deletion src/content/docs/zh-cn/guides/integrations-guide/mdx.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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" }),
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/zh-cn/guides/integrations-guide/react.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/zh-cn/guides/sessions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
26 changes: 0 additions & 26 deletions src/content/docs/zh-cn/recipes/build-forms-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -429,29 +429,3 @@ import PackageManagerTabs from "~/components/tabs/PackageManagerTabs.astro";
</Fragment>
</UIFrameworkTabs>
</Steps>

{/* ## 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`.

<PackageManagerTabs>
<Fragment slot="npm">
```shell
npm i zod zod-form-data
```
</Fragment>
<Fragment slot="pnpm">
```shell
pnpm i zod zod-form-data
```
</Fragment>
<Fragment slot="yarn">
```shell
yarn add zod zod-form-data
```
</Fragment>
</PackageManagerTabs>

2. In your API Route file, declare your schema using `zfd.formData` and export it. */}
3 changes: 2 additions & 1 deletion src/content/docs/zh-cn/recipes/i18n.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
9 changes: 5 additions & 4 deletions src/content/docs/zh-cn/reference/content-loader-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

// 定义加载器需要的任何选项
Expand All @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/zh-cn/reference/legacy-flags.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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({ })

Expand Down
3 changes: 1 addition & 2 deletions src/content/docs/zh-cn/reference/modules/astro-content.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import ReadMore from '~/components/ReadMore.astro';

```js
import {
z,
defineCollection,
getCollection,
getEntry,
Expand Down Expand Up @@ -77,7 +76,7 @@ export const collections = { blog };
<Since v="2.0.0" />
</p>

`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)。

Expand Down
4 changes: 3 additions & 1 deletion src/content/docs/zh-cn/tutorial/6-islands/4.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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" }),
Expand Down