JSON Schema 是否可以使用 if/then/else 引用外部 属性
JSON Schema Is it possible to use if/then/else which refer to outside property
我想根据其他 属性 的值添加有条件的要求。
只有在 'isInexperienced' 时才需要 'companyName' 和 'companyAddress'
值为假。
架构
{
"type": "object",
"properties": {
"previous_employment_section": {
"type": "array",
"items": {
"type": "object",
"properties": {
"companyAddress": {
"type": "string"
},
"companyName": {
"type": "string"
}
},
"if": {
"#/properties/isInexperienced": {
"const": false
}
},
"then": {
"required": [
"companyName",
"companyAddress"
]
}
}
},
"isInexperienced": {
"type": "boolean"
}
}
}
数据
{
"previous_employment_section": [],
"isInexperienced": true
}
我不完全理解你原来的架构的意图,但是这个怎么样?
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"previous_employment_section": {
"type": "array",
"items": {
"type": "object",
"properties": {
"companyAddress": {
"type": "string"
},
"companyName": {
"type": "string"
}
}
}
},
"isInexperienced": {
"type": "boolean"
}
},
"if": {
"properties": {
"isInexperienced": {
"const": false
}
}
},
"then": {
"properties": {
"previous_employment_section": {
"items": {
"required": [
"companyName",
"companyAddress"
]
},
"minItems": 1
}
}
}
}
这是不可能的。我们需要有更高层次的“if”,“properties”可以嵌套。可以用Leadpony的方法
我想根据其他 属性 的值添加有条件的要求。 只有在 'isInexperienced' 时才需要 'companyName' 和 'companyAddress' 值为假。
架构
{
"type": "object",
"properties": {
"previous_employment_section": {
"type": "array",
"items": {
"type": "object",
"properties": {
"companyAddress": {
"type": "string"
},
"companyName": {
"type": "string"
}
},
"if": {
"#/properties/isInexperienced": {
"const": false
}
},
"then": {
"required": [
"companyName",
"companyAddress"
]
}
}
},
"isInexperienced": {
"type": "boolean"
}
}
}
数据
{
"previous_employment_section": [],
"isInexperienced": true
}
我不完全理解你原来的架构的意图,但是这个怎么样?
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"previous_employment_section": {
"type": "array",
"items": {
"type": "object",
"properties": {
"companyAddress": {
"type": "string"
},
"companyName": {
"type": "string"
}
}
}
},
"isInexperienced": {
"type": "boolean"
}
},
"if": {
"properties": {
"isInexperienced": {
"const": false
}
}
},
"then": {
"properties": {
"previous_employment_section": {
"items": {
"required": [
"companyName",
"companyAddress"
]
},
"minItems": 1
}
}
}
}
这是不可能的。我们需要有更高层次的“if”,“properties”可以嵌套。可以用Leadpony的方法