Skip to content
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

docs: improve cn readme #2143

Merged
merged 1 commit into from Mar 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 47 additions & 24 deletions README_ZH.md
Expand Up @@ -36,8 +36,8 @@
# 內容

- [什么是 Zod](#什么是Zod)
- [安装](#安装)
- [生态体系](#生态系统)
- [安装](#安装)
- [基本用法](#基本用法)
- [定义模式](#定义模式)
- [基本原理](#基本原理)
Expand Down Expand Up @@ -112,7 +112,7 @@ Zod 被设计成对开发者尽可能友好。其目的是消除重复的类型
- 可以工作在浏览器和 Node.js
- 小巧: 8kb minified + zipped
- 不可变: 方法(即 `.optional()` )返回一个新的实例
- 简洁的、可连锁的接口
- 简洁的、可链式调用的接口
- 功能性方法: [解析,不验证](https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/)
- 也可用于普通的 JavaScript! 你不需要使用 TypeScript。

Expand Down Expand Up @@ -280,15 +280,27 @@ Zod 被设计成对开发者尽可能友好。其目的是消除重复的类型

_要在这里看到你的名字 + Twitter + 網站 , 请在[Freelancer](https://github.com/sponsors/colinhacks) 或 [Consultancy](https://github.com/sponsors/colinhacks)赞助 Zod ._

# 安装

安装 Zod v3:
# 生态系统

```sh
npm install zod
```
有越来越多的工具是建立在 Zod 之上或原生支持 Zod 的! 如果你在 Zod 的基础上建立了一个工具或库,请在[Twitter](https://twitter.com/colinhacks) 或者 [Discussion](https://github.com/colinhacks/zod/discussions)上告诉我。我会在下面添加,并在推特上发布。

- [`tRPC`](https://github.com/trpc/trpc): 在没有 GraphQL 的情况下建立端到端的类型安全 API
- [`react-hook-form`](https://github.com/react-hook-form/resolvers): 使用 React Hook Form 和 Zod 解析器轻松构建类型安全的表单。
- [`ts-to-zod`](https://github.com/fabien0102/ts-to-zod): 将 TypeScript 定义转换成 Zod 模式。
- [`zod-mocking`](https://github.com/dipasqualew/zod-mocking): 从你的 Zod 模式中生成模拟数据。
- [`zod-fast-check`](https://github.com/DavidTimms/zod-fast-check): 从 Zod 模式中生成 `fast-check` 的任意数据。
- [`zod-endpoints`](https://github.com/flock-community/zod-endpoints): 约定优先的严格类型的端点与 Zod。兼容 OpenAPI。
- [`express-zod-api`](https://github.com/RobinTail/express-zod-api): 用 I/O 模式验证和自定义中间件构建基于 Express 的 API 服务
- [`zod-i18n-map`](https://github.com/aiji42/zod-i18n): 有助于翻译zod错误信息。


# 安装

### 必要条件

⚠️ 重要提示:你必须在你的`tsconfig.json`中启用`strict`模式。这是所有 TypeScript 项目的最佳实践。
- TypeScript 4.5+!
- 你必须在你的`tsconfig.json`中启用`strict`模式。这是所有 TypeScript 项目的最佳实践。

```ts
// tsconfig.json
Expand All @@ -301,24 +313,29 @@ npm install zod
}
```

#### TypeScript 的要求
### 从`npm`(Node/Bun)安装
```sh
npm install zod
yarn add zod # yarn
bun add zod # bun
pnpm add zod # pnpm
```

- Zod 3.x requires TypeScript 4.1+
- Zod 2.x requires TypeScript 3.7+
- Zod 1.x requires TypeScript 3.3+
### 从`deno.land/x` (Deno)安装

# 生态系统
和Node不同,Demo依靠一个直接的URL导入而非像npm这样的包管理器。可以这样导入最新版本的Zod:
```ts
import { z } from "https://deno.land/x/zod/mod.ts";
```

有越来越多的工具是建立在 Zod 之上或原生支持 Zod 的! 如果你在 Zod 的基础上建立了一个工具或库,请在[Twitter](https://twitter.com/colinhacks) 或者 [Discussion](https://github.com/colinhacks/zod/discussions)上告诉我。我会在下面添加,并在推特上发布。
你也可以指定一个具体的版本:

```ts
import { z } from "https://deno.land/x/zod@v3.16.1/mod.ts";
```

> README的剩余部分假定你是直接通过npm安装的`zod`包。

- [`tRPC`](https://github.com/trpc/trpc): 在没有 GraphQL 的情况下建立端到端的类型安全 API
- [`react-hook-form`](https://github.com/react-hook-form/resolvers): 使用 React Hook Form 和 Zod 解析器轻松构建类型安全的表单。
- [`ts-to-zod`](https://github.com/fabien0102/ts-to-zod): 将 TypeScript 定义转换成 Zod 模式。
- [`zod-mocking`](https://github.com/dipasqualew/zod-mocking): 从你的 Zod 模式中生成模拟数据。
- [`zod-fast-check`](https://github.com/DavidTimms/zod-fast-check): 从 Zod 模式中生成 `fast-check` 的任意数据。
- [`zod-endpoints`](https://github.com/flock-community/zod-endpoints): 约定优先的严格类型的端点与 Zod。兼容 OpenAPI。
- [`express-zod-api`](https://github.com/RobinTail/express-zod-api): 用 I/O 模式验证和自定义中间件构建基于 Express 的 API 服务
- [`zod-i18n-map`](https://github.com/aiji42/zod-i18n): 有助于翻译zod错误信息。

# 基本用法

Expand All @@ -329,8 +346,14 @@ import { z } from "zod";

// 创建一个字符串的模式
const mySchema = z.string();

// 解析
mySchema.parse("tuna"); // => "tuna"
mySchema.parse(12); // => throws ZodError

// "安全"解析(如果验证失败不抛出错误)
mySchema.safeParse("tuna"); // => { success: true; data: "tuna" }
mySchema.safeParse(12); // => { success: false; error: ZodError }
```

创建一个 Object 模式
Expand All @@ -342,9 +365,9 @@ const User = z.object({
username: z.string(),
});

User.parse({ username: string });
User.parse({ username: "Ludwig" });

// 抽出推断的类型
// 提取出推断的类型
type User = z.infer<typeof User>;
// { username: string }
```
Expand Down