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

JSON Schema验证报错:预期Array却得Object,anyOf无法解析

JSON Schema验证问题解决

问题场景

我的JSON实例:

{"store_purchase_control": {"pass_except_these": [[{"vendor_name_list": {"value_contains": ["Amazon", "Amazon Toys"]}}]]}}

使用JSON Schema验证时,出现以下错误:

核心错误:

JSON does not match any schemas from 'anyOf'.

关联Schema路径:#/definitions/pass_except_these/items/items/anyOf

附加类型错误:

Invalid type. Expected Array but got Object.

涉及Schema路径:#/definitions/store_id_list/type、#/definitions/store_list/type、#/definitions/vendor_name_list/type

最初误以为是工具无法解析anyOf关键字,实际是结构不匹配导致的问题。

问题根源

不是工具不支持anyOf,而是JSON实例完全不符合Schema的类型定义:

  • Schema中vendor_name_list、store_id_list、store_list被定义为数组类型,但传入的是{"value_contains": [...]}这样的对象,类型直接不匹配。
  • 类型不匹配导致无法命中anyOf中的任何一个分支规则,进而触发顶层的anyOf验证失败。

解决步骤

  1. 修正字段类型
    查看Schema中vendor_name_list的具体定义:

    • 如果Schema期望它是字符串数组,直接改成:
      "vendor_name_list": ["Amazon", "Amazon Toys"]
      
    • 如果Schema期望它是包含特定对象的数组,改成:
      "vendor_name_list": [{"value_contains": ["Amazon", "Amazon Toys"]}]
      

    同理修正store_id_list、store_list的结构(如果用到的话)。

  2. 核对数组嵌套层级
    检查pass_except_these的Schema定义:当前用了双层数组[[...]],确认Schema是否要求这样的嵌套结构,如果不需要,简化为单层数组[...]。

  3. 验证anyOf分支匹配
    确认JSON实例满足anyOf中至少一个分支的所有规则:比如anyOf可能包含“匹配vendor规则”“匹配store规则”等分支,确保结构完全符合其中某一个分支的要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 17:36:31