【问题标题】:How to apply a sub schema in the JSON Schema validator?如何在 JSON Schema 验证器中应用子模式?
【发布时间】:2015-10-01 22:46:42
【问题描述】:

您好,我在 2.2.6 版中使用 JSON Schema evaluator 来验证我的服务器响应。这些响应可以包含 A、B 或 C 类型的单个对象,也可以包含复合对象,例如包含 A 对象数组的 D。为了重用每个对象的模式定义,我开始在同一个文件中描述所有实体,如here 所述。现在我的问题是,在验证响应时我必须引用其中一个对象。

这是我的(不是)SWE。

JSON 模式文件:

{
    "id":"#root",
    "properties": {
        "objecta": {
            "type": "object",
            "id":"#objecta",
            "properties": {
                "attribute1": {"type": "integer"},
                "attribute2": {"type": "null"},
            },
            "required": ["attribute1", "attribute2"]
        },
        "objectb": {
            "type": "object",
            "id":"#objectb",
            "properties": {
                "attribute1": {"type": "integer"},
                "attribute2": {
                    "type": "array",
                        "items": {
                            "$ref": "#/objecta"
                        }
                    }
                }
            },
            "required": ["attribute1", "attribute2"]
        },
    }
}

现在我想验证包含对象 B 的服务器响应。为此,我尝试了以下方法:

public class SchemeValidator {

    public static void main(String[] args) {

        String jsonData = pseudoCodeFileLoad("exampleresponse/objectb.txt");
        final File jsonSchemaFile = new File("resources/jsonschemes/completescheme.json");
        final URI uri = jsonSchemaFile.toURI();

        ProcessingReport report = null;

        try {
            JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
            final JsonSchema schema = factory.getJsonSchema(uri.toString() + "#objectb");
            JsonNode data = JsonLoader.fromString(jsonData);
            report = schema.validate(data);
        } catch (JsonParseException jpex) {
            // ... handle parsing errors etc.
        }
    }
}

问题是方案没有正确加载。我要么没有错误(即使响应无效),要么我得到fatal: illegalJsonRef,因为该方案似乎是空的。如何在我的 Java 代码中使用对象 b 的模式?谢谢!!

【问题讨论】:

    标签: java json schema jsonschema


    【解决方案1】:

    您的 $ref 似乎不正确。它需要是来自 JSON Schema 文件基础的相对引用(请参阅here)。

    因此您的 JSON 架构将变为:

    {
    "id":"#root",
    "properties": {
        "objecta": {
            "type": "object",
            "id":"#objecta",
            "properties": {
                "attribute1": {"type": "integer"},
                "attribute2": {"type": "null"},
            },
            "required": ["attribute1", "attribute2"]
        },
        "objectb": {
            "type": "object",
            "id":"#objectb",
            "properties": {
                "attribute1": {"type": "integer"},
                "attribute2": {
                    "type": "array",
                        "items": {
                            "$ref": "#/properties/objecta"
                        }
                    }
                }
            },
            "required": ["attribute1", "attribute2"]
        },
    }
    

    }

    我已将“/properties”添加到您的 $ref。它像 XPath 一样操作模式文件中对象的定义。

    【讨论】:

      猜你喜欢
      • 2012-11-28
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 2021-04-18
      • 2020-11-17
      相关资源
      最近更新 更多