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
相关产品推荐
相关产品推荐

