JSON 基于其中一个属性的架构 anyOf 验证
JSON schema anyOf validation based on one of properties
我很难弄清楚如何根据其中一个属性的值来验证一组对象。所以我有一个 JSON 对象,例如:
{
"items": [
{
"name": "foo",
"otherProperty": "bar"
},
{
"name": "foo2",
"otherProperty2": "baz",
"otherProperty3": "baz2"
},
{
"name": "imInvalid"
}
]
}
我想说
- 项目可以包含名称可以是 "foo" 或 "foo2"
的任何对象
- 如果它是 "foo" 那么唯一有效的其他 属性(必需)是
"otherProperty"
- 如果名称是 "foo2" 那么唯一有效的其他名称
属性 "otherProperty2" 和 "otherProperty3" 都是必需的
- 除 "foo" 和 "foo2" 之外,"name" 没有其他有效值
- 项目数组中的对象本身是可选的,有些可能会重复。
我尝试了各种方法,但我似乎无法在验证时失败。例如,名称 "imInvalid" 应该会导致验证错误。这是我对架构的最新迭代。我错过了什么?
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["items"],
"properties": {
"items": {
"type": "array",
"minItems": 1,
"additionalProperties": false,
"properties": {
"name": {
"anyOf": [
{
"type": "object",
"required": ["name", "otherProperty"],
"additionalProperties": false,
"properties": {
"otherProperty": { "type": "string" },
"name": { "enum": [ "foo" ] }
}
},{
"type": "object",
"required": ["name", "otherProperty2", "otherProperty3" ],
"additionalProperties": false,
"properties": {
"otherProperty2": { "type": "string" },
"otherProperty3": { "type": "string" },
"name": { "enum": [ "foo2" ] }
}
}
]
}
}
}
}
}
您已经掌握了使用枚举来区分匹配内容的基本思路,但这里有几个错误:
- Json 模式数组没有
properties
,它们有 items
.
- 在这些属性中,您将
name
定义为一个属性,然后保存其他 json 架构对象。
- 在您的模式的第二个分支中您定义了
otherProperty3
但在您的示例中 属性 被称为 anotherProperty3
试试这个稍微修改过的版本:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["items"],
"properties": {
"items": {
"type": "array",
"minItems": 1,
"additionalProperties": false,
"items": {
"anyOf": [
{
"type": "object",
"required": ["name", "otherProperty"],
"additionalProperties": false,
"properties": {
"otherProperty": { "type": "string" },
"name": { "enum": [ "foo" ] }
}
},{
"type": "object",
"required": ["name", "otherProperty2", "anotherProperty3" ],
"additionalProperties": false,
"properties": {
"otherProperty2": { "type": "string" },
"anotherProperty3": { "type": "string" },
"name": { "enum": [ "foo2" ] }
}
}
]
}
}
}
}
我很难弄清楚如何根据其中一个属性的值来验证一组对象。所以我有一个 JSON 对象,例如:
{
"items": [
{
"name": "foo",
"otherProperty": "bar"
},
{
"name": "foo2",
"otherProperty2": "baz",
"otherProperty3": "baz2"
},
{
"name": "imInvalid"
}
]
}
我想说
- 项目可以包含名称可以是 "foo" 或 "foo2" 的任何对象
- 如果它是 "foo" 那么唯一有效的其他 属性(必需)是 "otherProperty"
- 如果名称是 "foo2" 那么唯一有效的其他名称 属性 "otherProperty2" 和 "otherProperty3" 都是必需的
- 除 "foo" 和 "foo2" 之外,"name" 没有其他有效值
- 项目数组中的对象本身是可选的,有些可能会重复。
我尝试了各种方法,但我似乎无法在验证时失败。例如,名称 "imInvalid" 应该会导致验证错误。这是我对架构的最新迭代。我错过了什么?
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["items"],
"properties": {
"items": {
"type": "array",
"minItems": 1,
"additionalProperties": false,
"properties": {
"name": {
"anyOf": [
{
"type": "object",
"required": ["name", "otherProperty"],
"additionalProperties": false,
"properties": {
"otherProperty": { "type": "string" },
"name": { "enum": [ "foo" ] }
}
},{
"type": "object",
"required": ["name", "otherProperty2", "otherProperty3" ],
"additionalProperties": false,
"properties": {
"otherProperty2": { "type": "string" },
"otherProperty3": { "type": "string" },
"name": { "enum": [ "foo2" ] }
}
}
]
}
}
}
}
}
您已经掌握了使用枚举来区分匹配内容的基本思路,但这里有几个错误:
- Json 模式数组没有
properties
,它们有items
. - 在这些属性中,您将
name
定义为一个属性,然后保存其他 json 架构对象。 - 在您的模式的第二个分支中您定义了
otherProperty3
但在您的示例中 属性 被称为anotherProperty3
试试这个稍微修改过的版本:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["items"],
"properties": {
"items": {
"type": "array",
"minItems": 1,
"additionalProperties": false,
"items": {
"anyOf": [
{
"type": "object",
"required": ["name", "otherProperty"],
"additionalProperties": false,
"properties": {
"otherProperty": { "type": "string" },
"name": { "enum": [ "foo" ] }
}
},{
"type": "object",
"required": ["name", "otherProperty2", "anotherProperty3" ],
"additionalProperties": false,
"properties": {
"otherProperty2": { "type": "string" },
"anotherProperty3": { "type": "string" },
"name": { "enum": [ "foo2" ] }
}
}
]
}
}
}
}