enter image description here我有一个如下所示的json模式,并且我想根据我已经写过allOf条件的B和C的值将定义加载到D和E中。 并且我正在使用 json-schema-validator 在应用程序中进行json模式验证。
i)下面的模式始终以有效方式通过,因为allOf条件从未评估过,并且不是 从定义中加载验证器属性,例如maxLenth,multipleOf。
ii)我怀疑我在错误的地方(根架构或子架构)进行了条件处理,并且尝试了 将 all 的所有逻辑移到 subschema 级别(在B,C和D,E内部)
iii)我尝试执行https://json-schema.org/understanding-json-schema/reference/conditionals.html上提到的 allOf 示例,该示例也作为有效传递。为此,我确实在在线josn模式验证器http://json-schema-validator.herokuapp.com/上进行了验证,该验证器也使用相同的库 json-schema-validator。
iv)JsonSchemaFactory是否需要任何ValidationConfiguration来验证Draft7 jsonSchema条件,因为此 json-schema-validator 上的Defaultlibrary是DRAFT-4。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": [
"A",
"B",
"C",
"D",
"E"
],
"properties": {
"A": {
"type": "string"
},
"B": {
"type": "string",
"enum": ["TEST1","TEST2"]
},
"C": {
"type": "string",
"enum": ["TEST3","TEST4"]
},
"D": {
"type": "object"
},
"E": {
"type": "object"
}
},
"allOf": [
{
"if": {
"properties": { "B": { "const": "TEST1" } }
},
"then": {
"properties": { "D": { "$ref": "#/definitions/test" } }
}
},
{
"if": {
"properties": { "B": { "const": "TEST2" } }
},
"then": {
"properties": { "D": { "$ref": "#/definitions/testTwo" } }
}
},
{
"if": {
"properties": { "C": { "const": "TEST3" } }
},
"then": {
"properties": { "E": { "$ref": "#/definitions/testThree" } }
}
},
{
"if": {
"properties": { "C": { "const": "TEST4" } }
},
"then": {
"properties": { "E": { "$ref": "#/definitions/test4" } }
}
}
],
"definitions": {
"testOne":{"type":"object"},
"testTwo":{"type":"object"},
"testThree":{"type":"object"},
"testFour":{"type":"object"}
}
}
javaCode看起来像
@PostMapping("/sendMessage")
public ProcessingReport sendMessage(@RequestBody SampleRequest request) throws IOException, ProcessingException {
//step-1 writing request object into String
String requestJson = objectMapper.writeValueAsString(request);
//Step-2 getting jsonNode for requested json
JsonNode dataNode = JsonLoader.fromString(requestJson);
//step -3 creating jsonSchema factory(default)
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
//validating requested jsonNode(dataNode) against SchemaNode(schema of request json,which is loaded from resources)
ProcessingReport report = factory.getJsonSchema(schemaNode).validate(dataNode);
//Processing report resulting the given json validation is successful or not
if(!report.isSuccess()) {
System.out.println(report);
}
return report;
}
答案 0 :(得分:1)
json-schema-validator仅支持草稿03和草稿04。在以后的草稿中添加了if
/ then
/ const
。这些关键字会被忽略,从而导致您遇到无操作行为。
您有两种选择