使用allof和其他复杂代码进行json模式验证

问题描述 投票:0回答:1

我有一个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; }

json validation jsonschema json-schema-validator
1个回答
0
投票
json-schema-validator仅支持草稿03和草稿04。if/then/ const已在以后的草案中添加。这些关键字会被忽略,从而导致您遇到无操作行为。

您有两个选择

    选择支持草案07的其他implementation
  1. 改为使用Implication Pattern。它有点冗长,但结果是相同的。
© www.soinside.com 2019 - 2024. All rights reserved.