JSON模式:外部JSON模式文件未验证JSON

时间:2018-07-05 14:14:10

标签: java json jsonschema

我正在尝试使用json模式验证json,问题是我为复杂对象创建了不同的json模式文件。我需要使用ref标签包含到主模式中。并尝试使用everit lib验证我的json。架构已加载,但是当我尝试验证示例json时,未验证内部架构对象。

InnerObject.json

   {
    "$id": "http://example.com/example.json",
    "type": "object",
    "definitions": {},
    "$schema": "http://json-schema.org/draft-07/schema#",
    "properties": {
        "typeCode": {
            "$id": "/properties/typeCode",
            "type": "string"
        },
        "description": {
            "$id": "/properties/description",
            "type": "string"
        },
        "expDate": {
            "$id": "/properties/expDate",
            "type": "string"
        },
        "issuingCountry": {
            "$id": "/properties/issuingCountry",
            "type": "string"
        }
    },
    "required": [
        "typeCode",
        "description",
        "expDate",
        "issuingCountry"
    ]   
}

OuterObject.JSON

    {
    "$id": "http://example.com/example.json",
    "type": "object",
    "definitions": {
    },
    "$schema": "http://json-schema.org/draft-07/schema#",
    "properties": {
        "firstName": {
            "$id": "/properties/firstName",
            "type": "string"
        },
        "lastName": {
            "$id": "/properties/lastName",
            "type": "string"
        },
        "innerObject": {
            "$id": "#item",
            "type": "object",
            "$ref": "file://com/mypack/innerObject.json"
        }
    },
    "required": [
        "firstName",
        "lastName",
        "innerObject"
    ]
}

两个文件都在我的项目src / main / resource中

我有用于验证我的架构的json验证器类。

import org.everit.json.schema.Schema;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import org.json.JSONTokener;

public class JSONSchemaValidator {
    public static void main(String[] args) {
        JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaValidator.class
                .getResourceAsStream("/com/schema/outer.schema.json")));
        JSONObject outerJson = new JSONObject();

        JSONObject innerJson = new JSONObject();

        innerJson.put("expDate", "expDate");
        innerJson.put("typeCode", "typeCode");

        outerJson.put("firstName", "firstName");
        outerJson.put("lastName", "lastName");

        outerJson.put("innerObject", innerJson);

        Schema schema = SchemaLoader.load(jsonSchema);

        System.out.println(schema.getDescription());
        schema.validate(outerJson);

        System.out.println("done");

    }
}

它使用模式验证第一级,但不用于内部级别。任何人都可以建议做错了什么,以便不验证json。

我要验证的示例JSON是:

{"firstName":"firstName","lastName":"lastName","innerObject":{"expDate":"expDate","typeCode":"typeCode"}}

它应该抛出错误,例如“ typeCode”,“ description”,“ expDate”,issuingCountry“这四个字段是强制性的,在输入中我只传递了两个。因此,对于其余两个字段,应该抛出一个错误。

1 个答案:

答案 0 :(得分:0)

只需在外部json中使用ref标签提供内部json文件。

例如:

{
    "$id": "http://example.com/example.json",
    "type": "object",
    "definitions": {
    },
    "$schema": "http://json-schema.org/draft-07/schema#",
    "properties": {
        "firstName": {
            "$id": "/properties/firstName",
            "type": "string"
        },
        "lastName": {
            "$id": "/properties/lastName",
            "type": "string"
        },
        "innerObject": {                
            "$ref": "innerObject.json"
        }
    },
    "required": [
        "firstName",
        "lastName",
        "innerObject"
    ]
}

在Java代码中,您需要设置resolutionScope,您需要提供内部json的路径。

String path="file:" + Paths.get("").toUri().getPath() + "PATH_OF_YOUR_JSONSchema";
        System.out.println("older"+ path); 
        if (path.contains(" ")) {
             path=path.replaceAll(" ", "%20");           
         }
SchemaLoader sl=SchemaLoader.builder().schemaJson(jsonSchema).resolutionScope(path ).build();
Schema schema=sl.load().build();
schema.validate(yourJsonObject);
相关问题