JSON 数组中只允许 $ref 类型
Allow only $ref type in JSON array
我有以下架构,其中数组 values
应仅接受类型 value
的对象。我正在使用 Newtonsoft.Json v11.0.2 进行验证。
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://some.thing/json",
"type": "object",
"definitions": {
"value": {
"$id": "/definitions/value",
"required": ["a", "b", "c"],
"properties": {
"a": {
"$id": "/properties/value/a",
"type": "string"
},
"b": {
"$id": "/properties/value/b",
"type": "string"
},
"c": {
"$id": "/properties/value/c",
"type": "string"
}
}
}
},
"required": ["values"],
"properties": {
"values": {
"$id": "/properties/values",
"type": "array",
"items":
{
"$id": "/properties/values/item",
"$ref": "/definitions/value"
},
"uniqueItems": true
}
}
}
这是可行的,因为它不会验证类似
的内容
{
"values": [
{
"a": "a2",
"b": "b2"
}
]
}
但确实验证
{
"values": [
{
"a": "a2",
"b": "b2",
"c": "c3"
},
"string"
]
}
不应该。
如何强制数组中只能有 value
个? "additionalItems": false
限制了项目的数量,"items" 内的 "additionalProperties": false
似乎什么也没做,所以这些也不是我要找的。
您正在尝试验证值数组的有效项目,而项目定义中的 "additionalProperties": false
只会影响一个对象。
相反,您需要将 "type": "object"
添加到您的值定义中...
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://some.thing/json",
"type": "object",
"definitions": {
"value": {
"$id": "/definitions/value",
"type": "object",
"required": [
"a",
"b",
"c"
],
"properties": {
"a": {
"$id": "/properties/value/a",
"type": "string"
},
"b": {
"$id": "/properties/value/b",
"type": "string"
},
"c": {
"$id": "/properties/value/c",
"type": "string"
}
}
}
},
"required": [
"values"
],
"properties": {
"values": {
"$id": "/properties/values",
"type": "array",
"items": {
"$ref": "/definitions/value"
}
},
"uniqueItems": true
}
}
我有以下架构,其中数组 values
应仅接受类型 value
的对象。我正在使用 Newtonsoft.Json v11.0.2 进行验证。
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://some.thing/json",
"type": "object",
"definitions": {
"value": {
"$id": "/definitions/value",
"required": ["a", "b", "c"],
"properties": {
"a": {
"$id": "/properties/value/a",
"type": "string"
},
"b": {
"$id": "/properties/value/b",
"type": "string"
},
"c": {
"$id": "/properties/value/c",
"type": "string"
}
}
}
},
"required": ["values"],
"properties": {
"values": {
"$id": "/properties/values",
"type": "array",
"items":
{
"$id": "/properties/values/item",
"$ref": "/definitions/value"
},
"uniqueItems": true
}
}
}
这是可行的,因为它不会验证类似
的内容{
"values": [
{
"a": "a2",
"b": "b2"
}
]
}
但确实验证
{
"values": [
{
"a": "a2",
"b": "b2",
"c": "c3"
},
"string"
]
}
不应该。
如何强制数组中只能有 value
个? "additionalItems": false
限制了项目的数量,"items" 内的 "additionalProperties": false
似乎什么也没做,所以这些也不是我要找的。
您正在尝试验证值数组的有效项目,而项目定义中的 "additionalProperties": false
只会影响一个对象。
相反,您需要将 "type": "object"
添加到您的值定义中...
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://some.thing/json",
"type": "object",
"definitions": {
"value": {
"$id": "/definitions/value",
"type": "object",
"required": [
"a",
"b",
"c"
],
"properties": {
"a": {
"$id": "/properties/value/a",
"type": "string"
},
"b": {
"$id": "/properties/value/b",
"type": "string"
},
"c": {
"$id": "/properties/value/c",
"type": "string"
}
}
}
},
"required": [
"values"
],
"properties": {
"values": {
"$id": "/properties/values",
"type": "array",
"items": {
"$ref": "/definitions/value"
}
},
"uniqueItems": true
}
}