目前,我们正在使用一个包含oneOf的模式文件,其中包含两个模式:一个用于修补程序请求,一个用于POST请求。在Java代码中,我们检查请求中是否有id可用,然后检查oneOf部分中的第一个模式是否有错误消息。
就像这样:
processingReport.iterator().forEachRemaining(processingMessage -> {
JsonNode json = processingMessage.asJson();
JSONObject reports = new JSONObject(json.get("reports").toString());
logger.debug("Schema validation: {}", reports.toString());
//Seems always has 2 reports.
String reportIdentifier = isCreate ? "/properties/data/oneOf/0" : "/properties/data/oneOf/1";
JSONArray errorsArray = new JSONArray(reports.get(reportIdentifier).toString());
//Do something with the error here
});
但这对我来说似乎不对。是否有任何方法来管理模式本身,所以当id可用时,它会从oneOf中选择正确的模式,或者有更好的方法来做到这一点?
我知道有一种选择是有不同的json文件,但是我们的技术经理宁愿把它们保留在1位。
发布于 2016-08-03 23:38:09
oneOf
和anyOf
子句可用于建模条件约束。以下模式将根据id
属性的存在对修补程序或后模式进行验证:
{
"oneOf" : [{
"$ref" : "/post_request_schema#"
}, {
"allOf" : [{
"$ref" : "/patch_request_schema#"
}, {
"required" : ["id"]
}
]
}
]
}
https://stackoverflow.com/questions/38738116
复制