【发布时间】:2019-03-30 06:44:08
【问题描述】:
https://json-schema.org/understanding-json-schema/reference/conditionals.html URL 中提到的所有示例都具有对象属性之外的“if、then 和 else”条件。我只是想知道它是否可以在属性中设置并仍然遵循标准?
{
"type": "object",
"properties": {
"street_address": {
"type": "string"
},
"country": {
"enum": ["United States of America", "Canada"]
}
},
"if": {
"properties": { "country": { "const": "United States of America" } }
},
"then": {
"properties": { "postal_code": { "pattern": "[0-9]{5}(-[0-9]{4})?" } }
},
"else": {
"properties": { "postal_code": { "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" } }
}
}
这样可以吗?
{
"type": "object",
"properties": {
"street_address": {
"type": "string"
},
"country": {
"enum": ["United States of America", "Canada"]
},
"if": {
"properties": { "country": { "const": "United States of America" } }
},
"then": {
"properties": { "postal_code": { "pattern": "[0-9]{5}(-[0-9]{4})?" } }
},
"else": {
"properties": { "postal_code": { "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" } }
}
}
}
我在网上尝试了 JSON-schema lint,它没有引发任何错误,但是我不知道它的实现,因此不太确定语法是否与建议的 draft-7 匹配。
在对象的属性中这样做的动机是: 尤其是在大型模式文件中,如果我可以定义一个属性,其值有条件地依赖于它旁边的一些其他属性,那感觉会更好。而不是先完成所有属性,然后在属性之外单独应用条件。
对编程非常陌生 - 如果这是一个非常不正确的问题,请原谅我。 谢谢。
【问题讨论】:
标签: jsonschema