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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 20:10:36