{"57ecf565817bc3932d8de349": {
"prices": {
"2017-07-01": {
"summer": -1,
"winter": -1,
"xmas": -1},
"2017-08-05": {
"summer": -1,
"winter": -1,
"xmas": -1}
}
}
}
我如何编写一个JSON模式,其中每个键是不同的(这里:2017-07-01,2017-08-05),所以我不能在这个例子中使用items
http://json-schema.org/example1.html#definitions(产品模式集)。
这是我的schema:
{
"type": "object",
"properties": {
"57ecf565817bc3932d8de349": {
"type": "object",
"properties": {
"prices": {
"type": "object",
"properties": {
"2017-07-01": {
"type": "object",
"properties": {
"summer": {
"type": "integer"
},
"winter": {
"type": "integer"
},
"xmas": {
"type": "integer"
}
},
"required": [
"summer",
"winter",
"xmas"
]
},
"2017-08-05": {
"type": "object",
"properties": {
"summer": {
"type": "integer"
},
"winter": {
"type": "integer"
},
"xmas": {
"type": "integer"
}
},
"required": [
"summer",
"winter",
"xmas"
]
}
},
"required": [
"2017-07-01",
"2017-08-05"
]
}
},
"required": [
"prices"
]
}
},
"required": [
"57ecf565817bc3932d8de349"
]
}
在我的原始JSON中,我有很多这样的日期:2017-07-01,我想避免重复每个日期的模式。
您可以使用additionalProperties
关键字来完成此操作。在这个例子中,对象中的每个属性都必须根据给定的模式进行验证。
{
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"summer": { "type": "integer" },
"winter": { "type": "integer" },
"xmas": { "type": "integer" }
},
"required": ["summer", "winter", "xmas"]
}
}
use patternProperties
https://json-schema.org/understanding-json-schema/reference/object.html pattern-properties
之类的{
"type": "object",
"patternProperties": {
"^d{4}-d{2}-d{2}": {
"type": "object",
"properties": {
"summer": {
"type": "integer"
},
"winter": {
"type": "integer"
etc...
虽然被接受了,但我的答案是错误的。相反,请参阅Jason的回答:JSON模式中键有不同的名称