Javascript JSON模式是否可以使用引用外部属性的if/then/else
我想根据其他属性的值有条件地添加required。 仅当“isInexperienced”时,才需要“companyName”和“companyAddress” 值为false 模式 数据Javascript JSON模式是否可以使用引用外部属性的if/then/else,javascript,json,object,ajv,Javascript,Json,Object,Ajv,我想根据其他属性的值有条件地添加required。 仅当“isInexperienced”时,才需要“companyName”和“companyAddress” 值为false 模式 数据 我不完全理解您原始模式的意图,但是这个如何 { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "previous_employment_se
我不完全理解您原始模式的意图,但是这个如何
{
"$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的方法
{
"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
}
}
}
}