JSON Schema验证报错求助:null类型不符合string类型要求
问题分析与解决
报错 instance type (null) does not match any allowed primitive type (allowed: ["string"]) 的核心意思是:请求体中某个被Schema指定为string类型的字段,实际传入了null值。结合你的Schema,主要排查以下几点:
1. Schema拼写错误
你的metadata下paymentSkipped字段的类型写成了bolean(拼写错误),正确的类型应该是boolean。这个错误会导致Schema解析异常,干扰验证逻辑的正常判断,必须先修正。
修正后的Schema片段:
metadata: type: object required: [paymentSkipped] properties: paymentSkipped: boolean # 修正拼写错误
2. 必填string字段传入null
你的Schema定义了多个必填的string类型字段,只要其中任意一个字段传入null,就会触发该报错:
submission对象中的name字段questionobj对象中的category和question字段
例如下方请求体就会触发报错:
{ "name": null, // 错误:name应为string,不能是null "questions": [ { "category": "math", "question": "1+1=?" } ], "metadata": { "paymentSkipped": false } }
3. 数组及嵌套对象的完整性检查
确保questions数组不是null,且数组内每个questionobj对象都完整包含category和question两个非null的string字段,缺少字段或传入null都会触发验证失败。
内容的提问来源于stack exchange,提问作者Fred
相关产品推荐
相关产品推荐

