验证 json 模式中的 2 种可能的数据类型
validate 2 possible types of data in jsonchema
我花了一整天的时间试图让它发挥作用,请 post 列出参考资料和我在问题后尝试过的事情。
这是我的 jsonschema:
{
"data": [{
"required": "effort",
"decisive": "maybe",
"field1": 7
},
{
"required": "effort",
"decisive": "no",
"field1": 6
}],
"schema": {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"type": "string",
"pattern": "[A-Z]",
"title": "field1"
},
"required": {
"type": "string",
"title": "required",
"readonly": true
},
"decisive": {
"type": "string",
"title": "Decisive",
"enum": ["yes", "no", "maybe", "not now"]
}
}
}
}
}
考虑 jsonschema 的确切部分,但使用 field1
元素如下:
"field1": {
"type": "integer",
"minimum": 5,
"maximum": 10,
"title": "field1"
}
- 第一个示例仅验证其字段 1 中的大写字母
- 第二个想要一个 5 到 10 之间的整数。
如何让它验证其中任何一个,以便两者都被接受 -
- 或者只有大写字母
- 或5到10之间的整数?
哦 - 上面数据部分的 field1 不是那么重要,它是一个需要的默认值。
我尝试了各种想法-
与 oneOf - here, ,
参数 - here
其他属性 - here
需要 - here
直观的做法是使用 oneOf on 模式,但是正如许多问题中提到的那样,oneOf 不会在属性部分内部执行任何操作,只会在其外部执行任何操作。所以我试图在 oneOf 中拥有完全相同的属性,只有一个区别,如上所述。那也不起作用,并且包含很多必须以某种方式避免的重复。
有谁知道如何解决这个问题?我没有想法..
你是 oneOf
的正确轨道之一,除了你真正想要的是 anyOf
。几乎每次你认为你想要 oneOf
,你真的想要 anyOf
。请记住,properties
的值与其他任何值一样都是模式。您可以像在其他任何地方一样使用布尔关键字。
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"title": "field1"
"anyOf": [
{
"type": "string",
"pattern": "[A-Z]"
},
{
"type": "integer",
"minimum": 5,
"maximum": 10
}
]
},
"required": {
"type": "string",
"title": "required",
"readonly": true
},
"decisive": {
"type": "string",
"title": "Decisive",
"enum": ["yes", "no", "maybe", "not now"]
}
}
}
}
编辑 1
当您听说 oneOf
不能在属性内部使用时,这就是他们在谈论的事情。
{
"type": "object",
"properties": {
"anyOf": [
{
"field1": { ... }
},
{
"field1": { ... }
}
],
"required": { ... },
"decisive": { ... }
}
}
编辑 2
因为它出现在评论中,这里有一个更好的解释为什么 oneOf
几乎从来不是正确的选择。需要明确的是,oneOf
将始终代替 anyOf
。如果 anyOf
不存在,JSON Schema 不会失去任何表达能力。
但是,anyOf
是更精确的工具。在 anyOf
可以的情况下使用 oneOf
就像在工具箱中有一把简单的羊角锤时使用大锤打钉子一样。
anyOf
是布尔或操作。 oneOf
是布尔运算 "exclusive OR" (XOR)。 "XOR" 没有什么用处,现代语言甚至不支持它。 OR 通常用运算符 ||
表示。 XOR没有模拟。
anyOf
表示 任何 项都可以为真。 oneOf
表示 一项且只有一项 可以为真。当您使用 oneOf
时,验证器需要测试 所有 模式以确保一个模式验证为真而其余的验证为假。当您使用 anyOf
时,验证器可以在找到验证为真的架构后立即停止。这称为 "short circuiting",所有现代编程语言在评估 OR 运算时都会这样做。当模式相互排斥(几乎总是如此)时,在找到一个模式后继续验证模式纯粹是浪费,因此应该避免。
我认为 oneOf
被过度使用了,因为从自然语言的角度来看,它听起来是对的。
我花了一整天的时间试图让它发挥作用,请 post 列出参考资料和我在问题后尝试过的事情。
这是我的 jsonschema:
{
"data": [{
"required": "effort",
"decisive": "maybe",
"field1": 7
},
{
"required": "effort",
"decisive": "no",
"field1": 6
}],
"schema": {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"type": "string",
"pattern": "[A-Z]",
"title": "field1"
},
"required": {
"type": "string",
"title": "required",
"readonly": true
},
"decisive": {
"type": "string",
"title": "Decisive",
"enum": ["yes", "no", "maybe", "not now"]
}
}
}
}
}
考虑 jsonschema 的确切部分,但使用 field1
元素如下:
"field1": {
"type": "integer",
"minimum": 5,
"maximum": 10,
"title": "field1"
}
- 第一个示例仅验证其字段 1 中的大写字母
- 第二个想要一个 5 到 10 之间的整数。
如何让它验证其中任何一个,以便两者都被接受 -
- 或者只有大写字母
- 或5到10之间的整数?
哦 - 上面数据部分的 field1 不是那么重要,它是一个需要的默认值。
我尝试了各种想法-
与 oneOf - here,
参数 - here
其他属性 - here
需要 - here
直观的做法是使用 oneOf on 模式,但是正如许多问题中提到的那样,oneOf 不会在属性部分内部执行任何操作,只会在其外部执行任何操作。所以我试图在 oneOf 中拥有完全相同的属性,只有一个区别,如上所述。那也不起作用,并且包含很多必须以某种方式避免的重复。
有谁知道如何解决这个问题?我没有想法..
你是 oneOf
的正确轨道之一,除了你真正想要的是 anyOf
。几乎每次你认为你想要 oneOf
,你真的想要 anyOf
。请记住,properties
的值与其他任何值一样都是模式。您可以像在其他任何地方一样使用布尔关键字。
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"title": "field1"
"anyOf": [
{
"type": "string",
"pattern": "[A-Z]"
},
{
"type": "integer",
"minimum": 5,
"maximum": 10
}
]
},
"required": {
"type": "string",
"title": "required",
"readonly": true
},
"decisive": {
"type": "string",
"title": "Decisive",
"enum": ["yes", "no", "maybe", "not now"]
}
}
}
}
编辑 1
当您听说 oneOf
不能在属性内部使用时,这就是他们在谈论的事情。
{
"type": "object",
"properties": {
"anyOf": [
{
"field1": { ... }
},
{
"field1": { ... }
}
],
"required": { ... },
"decisive": { ... }
}
}
编辑 2
因为它出现在评论中,这里有一个更好的解释为什么 oneOf
几乎从来不是正确的选择。需要明确的是,oneOf
将始终代替 anyOf
。如果 anyOf
不存在,JSON Schema 不会失去任何表达能力。
但是,anyOf
是更精确的工具。在 anyOf
可以的情况下使用 oneOf
就像在工具箱中有一把简单的羊角锤时使用大锤打钉子一样。
anyOf
是布尔或操作。 oneOf
是布尔运算 "exclusive OR" (XOR)。 "XOR" 没有什么用处,现代语言甚至不支持它。 OR 通常用运算符 ||
表示。 XOR没有模拟。
anyOf
表示 任何 项都可以为真。 oneOf
表示 一项且只有一项 可以为真。当您使用 oneOf
时,验证器需要测试 所有 模式以确保一个模式验证为真而其余的验证为假。当您使用 anyOf
时,验证器可以在找到验证为真的架构后立即停止。这称为 "short circuiting",所有现代编程语言在评估 OR 运算时都会这样做。当模式相互排斥(几乎总是如此)时,在找到一个模式后继续验证模式纯粹是浪费,因此应该避免。
我认为 oneOf
被过度使用了,因为从自然语言的角度来看,它听起来是对的。