You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用fastify-zod构建校验Schema时遇编译错误求助

Fastify-zod与Zod版本兼容导致的编译错误问题

问题代码(user.schema.ts)

import { z } from 'zod'
import { buildJsonSchemas } from 'fastify-zod';

export enum Role {
    Agent = 'Agent',
    Admin = 'Admin',
}

const createUserSchema = z.object({
    email: z.string({
        required_error: "Email is required",
        invalid_type_error: "Email must be a string"
    }).email(),
    role: z.nativeEnum(Role, {
        required_error: "Role is required"
    }),
    teamId: z.number({
        required_error: "Team id is required"
    })
});

export type CreateUserInput = z.infer<typeof createUserSchema>;

export const { schemas: userSchemas, $ref } = buildJsonSchemas({
    createUserSchema     // 此处出现编译错误
});

初始编译错误信息

(property) createUserSchema: z.ZodObject<{
    email: z.ZodString;
    role: z.ZodNativeEnum<typeof Role>;
    teamId: z.ZodNumber;
}, "strip", z.ZodTypeAny, {
    email: string;
    role: Role;
    teamId: number;
}, {
    email: string;
    role: Role;
    teamId: number;
}>
类型“ZodObject<{ email: ZodString; role: ZodNativeEnum<typeof Role>; teamId: ZodNumber; }, "strip", ZodTypeAny, { email: string; role: Role; teamId: number; }, { ...; }>”中缺少属性“brand”,但类型“ZodType<unknown, ZodTypeDef, unknown>”中需要该属性。ts(2741)
    types.d.ts(78, 5): 'brand' 在此处声明。
    Models.d.ts(2, 59): 预期类型来自属性“createUserSchema”,该属性在类型“Models<string>”上声明

依赖版本(package.json)

"dependencies": {
    "@firebase/app-types": "^0.7.0",
    "@prisma/client": "^4.3.1",
    "dotenv": "^16.0.2",
    "fastify": "^4.6.0",
    "fastify-cors": "^6.1.0",
    "fastify-zod": "^1.2.0",
    "firebase": "^9.9.4",
    "firebase-admin": "^11.0.1",
    "zod": "3.18.0",
    "typescript": "4.8.2"
  },

更新Zod版本后的新错误

将Zod从3.17.2升级到3.18.0后,出现以下错误:

类型实例化过深,可能无限循环。ts(2589)

问题核心

不清楚“Property 'brand' is missing in type”错误的来源,以及升级Zod版本后出现的无限类型实例化错误的原因,需要排查遗漏的配置或兼容问题。


内容的提问来源于stack exchange,提问作者ASR4

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 02:10:19