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

Fastify应用共享验证Schema引用失败问题求助

问题原因及解决方案

你引用共享Schema的格式写错了。Fastify中通过addSchema注册的共享Schema,引用时要使用#<schema的$id>的格式,而非你写的<schema的$id>#。

把代码里的$ref: 'auth#'改成$ref: '#auth'就能正常工作了。

之所以会触发验证错误,是因为Fastify会将addSchema添加的Schema统一存放在内部的引用空间,你写反的引用格式会导致Fastify找不到对应的共享Schema,相当于没启用这个验证规则,自然会提示缺少Authorization字段。

修正后的控制器Schema示例:

const schema = {
    headers: { $ref: '#auth' },
    response: {
        200: {
            $merge: {
                source: { $ref: '#payload/properties/data' },
                with: {
                    properties: {
                        antivirus: { type: 'string' },
                        db: { type: 'string' },
                        started: { type: 'number' },
                        deployVersion: { type: 'string' },
                    },
                },
            },
        },
    },
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 21:42:43