我有一个json模式,如下所示,我想根据B和C的值将定义加载到D和E中,因为我已经编写了allOf条件。我正在使用json-schema-validator在应用程序中进行json模式验证。
i)下面的模式始终以有效方式通过,因为allOf条件从未评估过,并且不是从定义中加载验证器属性,例如maxLenth,multipleOf。
ii)我怀疑我在错误的地方(根模式或子模式)进行了条件处理,然后尝试将此allof逻辑移到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; }
if
/then
/ const
已在以后的草案中添加。这些关键字会被忽略,从而导致您遇到无操作行为。您有两个选择