我正在尝试使用“链式”定义构建一个json架构。
对于给定的架构:
{
"$schema": "http://json-schema.org/schema#",
"id": "http://toto/filter-schema#",
"title": "filter schema definition",
"type": "object",
"description": "filter data",
"definitions": {
"timeFilters": {
"type": "object",
"oneOf": [
{
"properties": {
"since": {
"type": "string",
"format": "date-time",
"description": "sends only items that have been modified AFTER this"
}
},
"additionalProperties": false,
"required": [
"since"
]
},
{
"properties": {
"from": {
"type": "string",
"format": "date-time",
"description": "return only data that have a validity date AFTER this"
},
"to": {
"type": "string",
"format": "date-time",
"description": "return only data that have a validity date BEFORE this"
}
},
"additionalProperties": false,
"required": [
"from",
"to"
]
}
]
},
"objectFilters": {
"type": "object",
"properties": {
"objectFilters": {
"type": "array",
"description": "the array of object filter",
"minItems": 1,
"uniqueItems": true,
"items": [
{
"type": "object",
"additionalProperties": false,
"properties": {
"targetClass": {
"type": "string",
"description": "the target class"
},
"targetAttribute": {
"type": "string",
"description": "the target attribute"
},
"test": {
"type": "string",
"description": "the test on the attribute value"
}
}
}
]
}
},
"additionalProperties": false
}
},
"anyOf": [
{
"additionalProperties": false
},
{
"$ref": "#/definitions/timeFilters",
"additionalProperties": false
},
{
"$ref": "#/definitions/objectFilters",
"additionalProperties": false
}
]
}
此架构正在验证
{
"since": "2016-02-17T01:02:03.1Z"
}
和
{
"from": "2016-02-17T01:02:03.1Z",
"to": "2016-02-17T01:02:03.1Z",
}
和
{
"objectFilters": [
{
"targetClass": "test",
"targetAttribute": "test",
"test": "test"
}
]
}
甚至
{}
但不是
{
"since": "2016-02-17T01:02:03.1Z",
"objectFilters": [
{
"targetClass": "test",
"targetAttribute": "test",
"test": "test"
}
]
}
如何让它验证最后一个json?
我试图在“anyOf”中添加一个新定义,如下所示:
{
"$ref": "#/definitions/timeFilters",
"$ref": "#/definitions/objectFilters",
"additionalProperties": false
}
但它不起作用。
我使用草案v4。
编辑: 也尝试了
{
"$ref": "#/definitions/timeFilters",
"additionalProperties": {
"$ref": "#/definitions/objectFilters",
"additionalProperties": false
}
}
无法正常工作
答案 0 :(得分:1)
组合模式时additionalProperties
的行为会让人感到困惑。我建议采用不同的方法。可以更容易地预先声明所有属性,然后单独声明所需的属性约束。
在这种情况下,dependencies
关键字效果很好。
http://json-schema.org/latest/json-schema-validation.html#anchor70
{
"$schema": "http://json-schema.org/schema#",
"id": "http://toto/filter-schema#",
"title": "filter schema definition",
"description": "filter data",
"type": "object",
"properties": {
"since": {
"type": "string",
"format": "date-time",
"description": "sends only items that have been modified AFTER this"
},
"from": {
"type": "string",
"format": "date-time",
"description": "return only data that have a validity date AFTER this"
},
"to": {
"type": "string",
"format": "date-time",
"description": "return only data that have a validity date BEFORE this"
},
"objectFilters": {
"type": "array",
"description": "the array of object filter",
"minItems": 1,
"uniqueItems": true,
"items": [
{
"type": "object",
"additionalProperties": false,
"properties": {
"targetClass": {
"type": "string",
"description": "the target class"
},
"targetAttribute": {
"type": "string",
"description": "the target attribute"
},
"test": {
"type": "string",
"description": "the test on the attribute value"
}
}
}
]
}
},
"additionalProperties": false,
"dependencies": {
"since": {
"not": { "required": ["from"] }
},
"from": ["to"],
"to": ["from"]
}
}