【发布时间】:2019-04-11 04:09:42
【问题描述】:
我花了一整天的时间试图让它发挥作用,将发布一个参考列表和我在提问后尝试过的事情。
这是我的 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 并不重要,它是所需的默认值。
我尝试过各种想法—— with oneOf - here, here, here
参数 - here
其他属性 - here
必填 - here
直观的做法是使用 oneOf on 模式,但正如许多问题中提到的那样,oneOf 在属性部分内不做任何事情,只是在它之外做任何事情。因此,我尝试在 oneOf 中拥有完全相同的属性,但只有一个区别,如上所述。这也不起作用,并且包含很多必须以某种方式避免的重复。
有谁知道如何解决这个问题?我没主意了..
【问题讨论】:
-
出于兴趣,这是某种任务还是工作?过去几天看到几个非常相似的问题?
-
它的工作。我认为每个人都在尝试将 jsonschema 用于表单,即使它不是为此而设计的。我现在有一个question with a bounty 试图将我们从羊驼中拯救出来,这样我们就可以更多地留在 Python 中。
标签: json validation jsonschema