zod-api-validator 中文文档教程

发布于 2年前 浏览 13 更新于 2年前

????建设中 ????

该软件包仍在进行中...

Zod API 验证器 (ZAV)

Zod API 验证器是一个 npm 软件包。这个概念是从 CLI 运行一个命令来读取所有内容 存储库中的 zav.config.mjs 文件,并调用每个端点将响应与指定的 Zod 架构进行比较。

????路线图

  • 添加对 cjs (commonjs) 和 mjs (模块) 的支持
  • 从这个包中公开 Zod,不需要在父仓库中安装 Zod
  • 更好的错误处理 - axios 和代码错误应该被处理和清理,目前它唯一好的 zod 错误

⚙️工作流程

先决条件

您必须将 Zod 安装到您的应用程序中。这样您就可以在存储库中定义 Zod 模式。

$ npm install zod

设置

  1. 将软件包安装到您的应用程序中
$ npm install zod-api-validator
  1. 在您的存储库中创建一个名为 zav.config.mjs 的新文件,其中包含以下内容:

zav.config.mjs

import { z } from 'zod';

/**
 * An array of endpoint objectst. Each endpoint object should have the following properties:
 * method - The method to use on the request as a string (GET/POST/PUT/PATCH/DELTE). e.g "GET"
 * url - The http/https endpoint to call as a string. e.g "http://localhost:5000/api/test"
 * schema - A Zod schema object. e.g z.object({ name: z.string(), age: z.number() })  
 */
const endpoints = [
    // { method: 'GET', url: 'http://localhost:5000/api/test', schema: userSchema },
];

export default endpoints;

用法

  1. 创建 Zod 架构您要测试的端点

UserSchema.zav.mjs

export const userSchema = z.object({
    name: z.string(),
    email: z.string().min(20, 'Must be more than 20 chars').email('Invalid email'),
    age: z.number().min(18, 'You must be over 18'),
})
  1. 将端点和架构添加到配置文件

zav.config.mjs

import { z } from 'zod';

import { userSchema } from './UserSchema.zav.mjs'

/**
 * An array of endpoint objectst. Each endpoint object should have the following properties:
 * method - The method to use on the request as a string (GET/POST/PUT/PATCH/DELTE). e.g "GET"
 * url - The http/https endpoint to call as a string. e.g "http://localhost:5000/api/test"
 * schema - A Zod schema object. e.g z.object({ name: z.string(), age: z.number() })  
 */
const endpoints = [
    { method: 'GET', url: 'http://localhost:5000/api/user', schema: userSchema },
];

export default endpoints;
  1. 在项目的根目录中运行以下命令:
$ zav
  1. 在您的终端中,您将看到以下格式的输出

来自 API 的有效响应:

***** (zav.config.mjs) *****
GET http://localhost:5000/api/user - VALID

来自 API 的无效响应:

***** (zav.config.mjs) *****
GET http://localhost:5000/api/user - INVALID
name: Expected string, received boolean,    
email: Must be more than 20 chars,
email: Invalid email

???? Under Construction ????

This package is still a work in progress…

Zod API Validator (ZAV)

Zod API Validator is an npm package. The concept is to run a single command from the CLI that will read all zav.config.mjs files in the repo, and call each endpoint comparing the response to the specified Zod schema.

???? Roadmap

  • Add support for both cjs (commonjs) and mjs (module)
  • Expose Zod from this package, dont need to install Zod in parent repo
  • Better error handling - axios and code errors should be handled and cleaner, at the moment its only nice zod errors

⚙️Workflow

Prerequisites

You must have Zod installed into your app. This is so you can defined Zod schemas in your repo.

$ npm install zod

Setup

  1. Install the package into your app
$ npm install zod-api-validator
  1. Create a new file in your repo called zav.config.mjs with the following contents:

zav.config.mjs

import { z } from 'zod';

/**
 * An array of endpoint objectst. Each endpoint object should have the following properties:
 * method - The method to use on the request as a string (GET/POST/PUT/PATCH/DELTE). e.g "GET"
 * url - The http/https endpoint to call as a string. e.g "http://localhost:5000/api/test"
 * schema - A Zod schema object. e.g z.object({ name: z.string(), age: z.number() })  
 */
const endpoints = [
    // { method: 'GET', url: 'http://localhost:5000/api/test', schema: userSchema },
];

export default endpoints;

Usage

  1. Create a Zod schema for the endpoint you want to test

UserSchema.zav.mjs

export const userSchema = z.object({
    name: z.string(),
    email: z.string().min(20, 'Must be more than 20 chars').email('Invalid email'),
    age: z.number().min(18, 'You must be over 18'),
})
  1. Add your endpoint and schema to the config file

zav.config.mjs

import { z } from 'zod';

import { userSchema } from './UserSchema.zav.mjs'

/**
 * An array of endpoint objectst. Each endpoint object should have the following properties:
 * method - The method to use on the request as a string (GET/POST/PUT/PATCH/DELTE). e.g "GET"
 * url - The http/https endpoint to call as a string. e.g "http://localhost:5000/api/test"
 * schema - A Zod schema object. e.g z.object({ name: z.string(), age: z.number() })  
 */
const endpoints = [
    { method: 'GET', url: 'http://localhost:5000/api/user', schema: userSchema },
];

export default endpoints;
  1. In the root of your project run the following command:
$ zav
  1. In your terminal you will see an output in this format

Valid response from API:

***** (zav.config.mjs) *****
GET http://localhost:5000/api/user - VALID

Invalid response from API:

***** (zav.config.mjs) *****
GET http://localhost:5000/api/user - INVALID
name: Expected string, received boolean,    
email: Must be more than 20 chars,
email: Invalid email
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文