Json 架构验证类型无效。期望数组但得到对象

问题描述 投票:0回答:2

我有一些Json

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

}

我正在尝试验证 json 模式..

{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Controls",
"description": "Controls",    
"definitions": {
  "transaction_amount_limit": {
    "type": "integer"
  },
  "mcc": {
    "type": "string",
    "pattern": "[0-9][0-9][0-9][0-9]"
  },
  "val_contains": {
    "type": "string"
  },
  "value_equals": {
    "type": "string"
  },
  "range": {
    "type": "object",
    "required": ["from", "through"],
    "properties": {
      "from": {
        "$ref": "#/definitions/mcc"
      },
      "through": {
        "$ref": "#/definitions/mcc"
      }
    }
  },
  "store_list": {        
    "type": "array",
    "items": {
      "$ref": "#/definitions/range"
    }
  },
  "vendor_name_list": {
    "type": "array",
    "items": {
      "$ref": "#/definitions/val_contains"
    }
  },
  "store_id_list": {
    "type": "array",
    "items": {
      "$ref": "#/definitions/value_equals"
    }
  },
  "allow_it": {
    "type": "array",
    "items": {
      "anyOf": [
        {"$ref": "#/definitions/vendor_name_list"},
        {"$ref": "#/definitions/store_list"},
        {"$ref": "#/definitions/store_id_list"}
      ]
    }
  },
  "always_block": {
    "type": "array",
    "items": {
      "anyOf": [
        {"$ref": "#/definitions/vendor_name_list"},
        {"$ref": "#/definitions/store_list"},
        {"$ref": "#/definitions/store_id_list"}
      ]
    }
  },
  "pass_except_these": {
    "type": "array",
    "items": {
      "type": "array",
      "items": {
        "anyOf": [
          {"$ref": "#/definitions/vendor_name_list"},
          {"$ref": "#/definitions/store_list"},
          {"$ref": "#/definitions/store_id_list"}
        ]
      },
      "minItems": 1,
      "maxItems": 3
    }
  },
  "block_all_except": {
    "type": "array",
    "items": {
      "anyOf": [
        {"$ref": "#/definitions/vendor_name_list"},
        {"$ref": "#/definitions/store_list"},
        {"$ref": "#/definitions/store_id_list"}
      ]
    }
  },
  "store_purchase_control": {
    "type": "object",
    "properties": {
      "pass_except_these": {"$ref": "#/definitions/pass_except_these"},
      "allow_it": {"$ref": "#/definitions/allow_it"},
      "always_block": {"$ref": "#/definitions/always_block"}
    },
    "additionalProperties": false
  }
},
"type": "object",
"properties": {
  "store_purchase_control": {"$ref": "#/definitions/store_purchase_control"}
},
"additionalProperties": false

}

使用 .. 解析此内容

https://www.jsonschemavalidator.net/

JSON does not match any schemas from 'anyOf'.

架构路径: #/definitions/pass_ except_these/items/items/anyOf

留言: 类型无效。需要数组但得到对象。 架构路径: #/定义/store_id_list/类型

留言: 类型无效。需要数组但得到对象。 架构路径: #/定义/store_list/类型

留言: 类型无效。需要数组但得到对象。 架构路径: #/definitions/vendor_name_list/type

似乎无法解析anyOf关键字

json jsonschema
2个回答
1
投票

错误消息“类型无效。需要数组,但得到对象”表示 JSON 架构中的预期类型和实际类型不匹配。在架构的第 3 行中,您将属性“store_purchase_control”定义为一个对象,但在 JSON 数据中,它是一个对象而不是数组。

要解决此问题,您需要调整 JSON 数据以匹配架构定义。以下是与您的架构一致的更正后的 JSON 数据:

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

如果您无法更改 json,您可以更改架构验证。 要修改 JSON 架构以进行验证,您可以根据您的要求更改架构定义。以下是如何修改架构的示例:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Controls",
  "description": "Controls",
  "definitions": {
    "transaction_amount_limit": {
      "type": "integer"
    },
    "mcc": {
      "type": "string",
      "pattern": "[0-9][0-9][0-9][0-9]"
    },
    "val_contains": {
      "type": "string"
    },
    "value_equals": {
      "type": "string"
    },
    "range": {
      "type": "object",
      "required": ["from", "through"],
      "properties": {
        "from": {
          "type": "string"
        },
        "through": {
          "type": "string"
        }
      }
    },
    "store_list": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/range"
      }
    },
    "vendor_name_list": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/val_contains"
      }
    },
    "store_id_list": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/value_equals"
      }
    },
    "allow_it": {
      "type": "array",
      "items": {
        "anyOf": [
          { "$ref": "#/definitions/vendor_name_list" },
          { "$ref": "#/definitions/store_list" },
          { "$ref": "#/definitions/store_id_list" }
        ]
      }
    },
    "always_block": {
      "type": "array",
      "items": {
        "anyOf": [
          { "$ref": "#/definitions/vendor_name_list" },
          { "$ref": "#/definitions/store_list" },
          { "$ref": "#/definitions/store_id_list" }
        ]
      }
    },
    "pass_except_these": {
      "type": "array",
      "items": {
        "anyOf": [
          { "$ref": "#/definitions/vendor_name_list" },
          { "$ref": "#/definitions/store_list" },
          { "$ref": "#/definitions/store_id_list" }
        ]
      },
      "minItems": 1,
      "maxItems": 3
    },
    "block_all_except": {
      "type": "array",
      "items": {
        "anyOf": [
          { "$ref": "#/definitions/vendor_name_list" },
          { "$ref": "#/definitions/store_list" },
          { "$ref": "#/definitions/store_id_list" }
        ]
      }
    },
    "store_purchase_control": {
      "type": "object",
      "properties": {
        "pass_except_these": { "$ref": "#/definitions/pass_except_these" },
        "allow_it": { "$ref": "#/definitions/allow_it" },
        "always_block": { "$ref": "#/definitions/always_block" }
      },
      "additionalProperties": false
    }
  },
  "type": "object",
  "properties": {
    "store_purchase_control": { "$ref": "#/definitions/store_purchase_control" }
  },
  "additionalProperties": false
}

0
投票

尚不清楚您想用它做什么,但似乎您正在尝试向数据添加一些架构约束。

这是与模式匹配的更正数据:

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

pass_except_these
属性需要一个数组数组,其中辅助数组中的每个项目都是以下之一:

  • vendor_name_list
    这是一个字符串数组
  • store_list
    ,它是
    range
    值的数组
  • store_id_list
    这是一个字符串数组

所以你有一个数组的数组,其中每个项目也是一个数组。您有三层数组。


从模式内部事物的命名来看,我预计这可能不是您想要做的,但这是满足模式的数据。如果您怀疑架构可能是错误的,那么问题并不清楚。

© www.soinside.com 2019 - 2024. All rights reserved.