JSON 具有特定对象数组(不同类型)的模式验证器
JSON Schema validator with an array of specific objects (different types)
我有以下 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 架构无关,而是与我调用正在解析架构的库的方式有关。
我正在使用 https://github.com/justinrainbow/json-schema 并向 class 传递了错误的数据类型。呸!
我有以下 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 架构无关,而是与我调用正在解析架构的库的方式有关。
我正在使用 https://github.com/justinrainbow/json-schema 并向 class 传递了错误的数据类型。呸!