我有一个端点,它应该得到一个参数method,它应该符合axios类型的Method。
如何在Zod中创建一个模式来验证值是否使用了Schema类型
import { Method } from 'axios';
const Schema = zod.object({
method: zod.someHowUseTheTypeFrom(Method),
});axios包中的Method类型是:
export type Method =
| 'get' | 'GET'
| 'delete' | 'DELETE'
| 'head' | 'HEAD'
| 'options' | 'OPTIONS'
| 'post' | 'POST'
| 'put' | 'PUT'
| 'patch' | 'PATCH'
| 'purge' | 'PURGE'
| 'link' | 'LINK'
| 'unlink' | 'UNLINK'发布于 2022-05-11 02:43:17
阅读您的注释,听起来像是要确保您的模式与axios中的Method类型同步。我建议采取以下行动:
import { z } from 'zod';
import type { Method } from 'axios';
const methods: z.ZodType<Method> = z.enum(['get', 'GET', ...]);这至少将强制执行表达式右侧的模式将解析有效的axios Method结果。不幸的是,除非axios还导出一个包含与Method类型中的值相对应的字符串的数组,否则任何事情都是遥不可及的。
您要寻找的原始z.something(<type here>)无法工作,因为zod使用的是实际的运行时对象,并且在运行时不存在像Method这样的类型。如果axios导出了一个包含方法的数组,那么这将是一个运行时值,您可以使用该值(可能使用某种类型转换)来生成您的methods模式(稍后将详细介绍)。
这种方法的另一个缺点是,类似这样的东西会打印出来:
const methods z.ZodType<Method> = z.enum(['get']);这是因为类型在TypeScript中是如何工作的。enum模式只会成功地为'get'解析,但是由于文字'get'是Method中定义的更大的联合类型的子类型,因此生成的模式也是可分配的。
因此,我要提出的下一个选项感觉有点自毁,因为它需要重新声明Method中的所有值,但是,您可以继续使用axios Method类型,并且您肯定会有一个解析Method中所有值的模式(即不会屈服于上述问题):
import { z } from "zod";
import { Method } from "axios";
const METHOD_MAP: { [K in Method]: null } = {
get: null,
GET: null,
delete: null,
DELETE: null,
head: null,
HEAD: null,
options: null,
OPTIONS: null,
post: null,
POST: null,
put: null,
PUT: null,
patch: null,
PATCH: null,
purge: null,
PURGE: null,
link: null,
LINK: null,
unlink: null,
UNLINK: null
};
const METHODS = (Object.keys(METHOD_MAP) as unknown) as readonly [
Method,
...Method[]
];
const methods: z.ZodType<Method> = z.enum(METHODS);METHODS的类型断言在这里是安全的,因为METHODS_MAP没有导出,而且我们确切地知道它有哪些键。现在,如果缺少任何METHOD_MAP值,则Method对象将导致类型错误,这意味着生成的模式将解析所有Method值,作为编译时强制执行的保证。
发布于 2022-04-07 22:10:58
如果要直接使用该类型,可以使用以下命令:
const methods = ['get','GET',...] as const;
export type Method = (typeof methods)[number];
zod.enum(methods);通过这种方式,您可以利用这两个世界的优势;拥有可以使用的值(数组)中的方法,以及您最初想要的类型。
https://stackoverflow.com/questions/71782572
复制相似问题