From 6b8f6557d9b53ea7ad3fda753a51cdf153ba1fb7 Mon Sep 17 00:00:00 2001 From: cunzaizhuyi <877824709@qq.com> Date: Sun, 5 Mar 2023 15:24:41 +0800 Subject: [PATCH] docs: improve cn readme (#2143) --- README_ZH.md | 71 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/README_ZH.md b/README_ZH.md index 38d04d0c6..08d9e5fc8 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -36,8 +36,8 @@ # 內容 - [什么是 Zod](#什么是Zod) -- [安装](#安装) - [生态体系](#生态系统) +- [安装](#安装) - [基本用法](#基本用法) - [定义模式](#定义模式) - [基本原理](#基本原理) @@ -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。 @@ -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 @@ -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错误信息。 # 基本用法 @@ -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 模式 @@ -342,9 +365,9 @@ const User = z.object({ username: z.string(), }); -User.parse({ username: string }); +User.parse({ username: "Ludwig" }); -// 抽出推断的类型 +// 提取出推断的类型 type User = z.infer; // { username: string } ```