【发布时间】:2017-11-08 01:46:57
【问题描述】:
函数返回如下所示的对象:
{
"answer": {
"vehicle_type": 1,
"message": "Car"
},
"model": "VW",
"color": "red"
}
'Answer' 对象始终存在。其他字段基于“vehicle_type”。
例如
如果vehicle_type = 1,则有“型号”和“颜色”。
如果vehicle_type = 2,则有“engine_count”、“seat_count”和“wing_count”。
我正在尝试编写 JSON 模式,我将使用它来验证返回的对象。
如果 'vehicle_type' 为 1,我想将 'model' 和 'color' 设置为必需属性。 如果“vehicle_type”为 2,则需要“engine_count”、“seat_count”和“wing_count”。
我正在使用 AJV (https://github.com/epoberezkin/ajv) 架构验证器。
对我来说,这是有问题的,因为vehicle_type 嵌套在'answer' 中,而我想根据需要标记的属性位于父对象上。 换言之,“validation_type”与“model”或“engine_count”不在同一级别。
我已经尝试了几种不同的方法...我也尝试过使用 ajv-keywords (switch, if/else/then) 但我没有任何运气
有什么想法吗?
【问题讨论】:
标签: javascript json node.js jsonschema ajv