java - Json 架构验证失败

标签 java jsonschema json-schema-validator

我的 pom.xml 中有以下依赖项

 <!-- https://github.com/everit-org/json-schema -->
               <dependency>
                   <groupId>com.github.everit-org.json-schema</groupId>
                   <artifactId>org.everit.json.schema</artifactId>
                   <version>1.11.1</version>
               </dependency>

               <!-- https://mvnrepository.com/artifact/org.json/json -->
               <dependency>
                   <groupId>org.json</groupId>
                   <artifactId>json</artifactId>
                   <version>20190722</version>
               </dependency>

这是我的 json 架构

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "id": "test",
    "title": "test-json validation",
    "description": "This schema should define the structure of the test json",
    "allOf": [
        {
            "$ref": "classpath:/jsonSchema/header/test.json#/definitions/test"
        },
        {
            "$ref": "classpath:/jsonSchema/rows/test.json#/definitions/test"
        }
    ],
    "properties": {
        "version": {
            "type": "array",
            "items": {
                "type": "string",
                "enum": [
                    "2.0",
                    "2.1"
                ]
            }
        }
    },
    "required": [
        "version"
    ]
}

这就是我想要实现的目标

public Schema createSchema(String schemaPath) throws IOException {

        Schema schema = null;
        try (InputStream inputStream = new ClassPathResource(schemaPath).getInputStream()) {
            JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
            schema = SchemaLoader.load(rawSchema);
        }
        return schema;
    }

我得到以下异常:

SchemaException: classpath:/jsonSchema/header/order_header.json#/definitions/order_header/properties/order_header/properties/origin/properties/locale: expected type is one of Boolean or JsonObject, found: String at org.everit.json.schema.loader.LoadingState.createSchemaException(LoadingState.java:142) at org.everit.json.schema.loader.JsonValue$Multiplexer.multiplexFailure(JsonValue.java:50) at org.everit.json.schema.loader.JsonValue$Multiplexer.lambda$requireAny$1(JsonValue.java:45) at java.util.Optional.orElseThrow(Optional.java:290) at org.everit.json.schema.loader.JsonValue$Multiplexer.requireAny(JsonValue.java:45) at org.everit.json.schema.loader.SchemaLoader.load(SchemaLoader.java:434) [6x]

最佳答案

properties 对象中的值必须是架构。

就您而言,您已将“string”作为值。

您看到的错误是 SCHEMA 无法验证,因为它期望看到 boolean 值或 JSON 对象,但获取的是 locale 属性值的字符串。

关于java - Json 架构验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57957794/

相关文章:

java - Jersey 测试相当于curl 请求

java - 用于 Java 的开源 3D 渲染引擎

json - 为两个几乎相同的实例定义一个 JSON 模式

python - 我的 ExclusiveMinimum 属性的实现是否正确?

javascript - 针对 JSON 模式验证 JS 测试

json 模式按模式验证数字键

java - 技术栈建议 : Google App Engine, FLEX、GraniteDS (Tide)、Java、Spring

java - 如何使用AOP拦截File、FileReader、FileWriter、FileInputStream、FileOutputStream的构造函数?

JSON 模式对数组中某些对象属性进行条件检查

regex - Json 模式模式排除字符串中的数字和特殊字符