【问题标题】:JSON Schema validator with an array of specific objects (different types)具有特定对象数组(不同类型)的 JSON Schema 验证器
【发布时间】:2017-05-08 17:30:14
【问题描述】:

我想要验证以下 JSON 数据。

[
    { "fieldType": "oneThing" },
    { "fieldType": "anotherThing" },
    { "fieldType": "oneThing" }
]

我当前(非工作)架构是:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array",
  "items": {
    "oneOf": [
      { "$ref": "#/definitions/oneThing" },
      { "$ref": "#/definitions/anotherThing" }
    ]
  },
  "definitions": {
    "oneThing": {
      "type": "object",
      "properties": {
        "fieldType": {
          "type": "string",
          "pattern": "oneThing"
        }
      },
      "required": [
        "fieldType"
      ]
    },
    "anotherThing": {
      "type": "object",
      "properties": {
        "fieldType": {
          "type": "string",
          "pattern": "anotherThing"
        }
      },
      "required": [
        "fieldType"
      ]
    }
  }
}

我收到以下错误,但我看不到我做错了什么。

[] Object value found, but an array is required

更多上下文:我正在生成一个基于 JSON 配置的动态 HTML 表单。 HTML 表单将具有一组特定的有效字段类型,并且相同的字段类型可能在配置中存在多次,因此oneThing 在上述示例 json 中出现多次。

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    事实证明,这与我的 JSON 架构无关,而是与我调用解析架构的库的方式有关。

    我正在使用https://github.com/justinrainbow/json-schema 并将错误的数据类型传递给类。呵呵!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多