如何递归验证JSON模式?

时间:2014-07-09 12:31:39

标签: json validation recursion jsonschema

我有一个JSON架构:

{
    "$schema": "http://myschema#",
    "type": "object",
    "properties": {
        "RBox": {
            "type": "array",
            "items": { "$ref": "#/definitions/rboxItem" },
            "uniqueItems": true
        },
        "TBox": {
            "type": "array",
            "items": { "$ref": "#/definitions/tboxItem" },
            "uniqueItems": true
        },
        "ABox": {
            "type": "array",
            "minItems": 1,
            "items": { "$ref": "#/definitions/aboxItem" },
            "uniqueItems": true
        }
    },
    "additionalProperties": false,
    "required": [ "ABox" ],
    "definitions": {
        "rboxItem": {
            "oneOf": [
                { "$ref": "#/definitions/inclusionRoleItem" },
                { "$ref": "#/definitions/transitiveRoleItem" }
            ]
        },
        "tboxItem": {
            "oneOf": [
                { "$ref": "#/definitions/inclusionConceptItem" },
                { "$ref": "#/definitions/equalConceptItem" }
            ]
        },
        "aboxItem": {
            "oneOf": [
                { "$ref": "#/definitions/ofTypeItem" },
                { "$ref": "#/definitions/inRelationItem" },
                { "$ref": "#/definitions/unequalItem" }
            ]
        },
        "inclusionRoleItem": {
            "type": "object",
            "properties": {
                "op": { "enum": [ "inclusionR" ] },
                "included": { "$ref": "#/definitions/roleItem" },
                "including": { "$ref": "#/definitions/roleItem" }
            },
            "additionalProperties": false,
            "required": [ "op", "included", "including" ]
        },
        "transitiveRoleItem": {
            "type": "object",
            "properties": {
                "op": { "enum": [ "transitive" ] },
                "arg": { "$ref": "#/definitions/roleItem" }
            },
            "additionalProperties": false,
            "required": [ "op", "arg" ]
        },
        "inclusionConceptItem": {
            "type": "object",
            "properties": {
                "op": { "enum": [ "inclusionT" ] },
                "included": { "$ref": "#/definitions/concept" },
                "including": { "$ref": "#/definitions/concept" }
            },
            "additionalProperties": false,
            "required": [ "op", "included", "including" ]
        },
        "equalConceptItem": {
            "type": "object",
            "properties": {
                "op": { "enum": [ "equal" ] },
                "arg1": { "$ref": "#/definitions/concept" },
                "arg2": { "$ref": "#/definitions/concept" }
            },
            "additionalProperties": false,
            "required": [ "op", "arg1", "arg2" ]
        },
        "ofTypeItem": {
            "type": "object",
            "properties": {
                "op": { "enum": [ "oftype" ] },
                "individual": { "$ref": "#/definitions/identifier" },
                "concept": { "$ref": "#/definitions/concept" }
            },
            "additionalProperties": false,
            "required": [ "op", "individual", "concept" ]
        },
        "inRelationItem": {
            "type": "object",
            "properties": {
                "op": { "enum": [ "inrelation" ] },
                "start": { "$ref": "#/definitions/identifier" },
                "end": { "$ref": "#/definitions/identifier" }
            },
            "additionalProperties": false,
            "required": [ "op", "start", "end" ]
        },
        "unequalItem": {
            "type": "object",
            "properties": {
                "op": { "enum": [ "unequal" ] },
                "arg1": { "$ref": "#/definitions/identifier" },
                "arg2": { "$ref": "#/definitions/identifier" }
            },
            "additionalProperties": false,
            "required": [ "op", "arg1", "arg2" ]
        },
        "roleItem": {
            "oneOf": [
                { "$ref": "#/definitions/roleName" },
                { "$ref": "#/definitions/inverseRoleName" }
            ]
        },
        "roleName": {
            "$ref": "#/definitions/identifier"
        },
        "inverseRoleName": {
            "type": "object",
            "properties": {
                "op": { "enum": [ "inverse" ] },
                "arg": { "$ref": "#/definitions/roleName" }
            },
            "additionalProperties": false,
            "required": [ "op", "arg" ]
        },
        "concept": {
            "oneOf": [
                { "$ref": "#/definitions/top" },
                { "$ref": "#/definitions/bottom" },
                { "$ref": "#/definitions/conceptName" },
                { "$ref": "#/definitions/negativeConceptName" },
                { "$ref": "#/definitions/andItem" },
                { "$ref": "#/definitions/orItem" },
                { "$ref": "#/definitions/existsItem" },
                { "$ref": "#/definitions/forallItem" },
                { "$ref": "#/definitions/atleastItem" },
                { "$ref": "#/definitions/atmostItem" }
            ]
        },
        "top": {
            "type": "object",
            "properties": {
                "op": { "enum": [ "top" ] }
            },
            "additionalProperties": false,
            "required": [ "op" ]
        },
        "bottom": {
            "type": "object",
            "properties": {
                "op": { "enum": [ "bottom" ] }
            },
            "additionalProperties": false,
            "required": [ "op" ]
        },
        "conceptName": {
            "$ref": "#/definitions/identifier"
        },
        "negativeConceptName": {
            "type": "object",
            "properties": {
                "op": { "enum": [ "negation" ] },
                "arg": { "$ref": "#/definitions/concept" }
            },
            "additionalProperties": false,
            "required": [ "op", "arg" ]
        },
        "andItem": {
            "type": "object",
            "properties": {
                "op": { "enum": [ "and" ] },
                "arg1": { "$ref": "#/definitions/concept" },
                "arg2": { "$ref": "#/definitions/concept" }
            },
            "additionalProperties": false,
            "required": [ "op", "arg1", "arg2" ]
        },
        "orItem": {
            "type": "object",
            "properties": {
                "op": { "enum": [ "or" ] },
                "arg1": { "$ref": "#/definitions/concept" },
                "arg2": { "$ref": "#/definitions/concept" }
            },
            "additionalProperties": false,
            "required": [ "op", "arg1", "arg2" ]
        },
        "existsItem": {
            "type": "object",
            "properties": {
                "op": { "enum": [ "exists" ] },
                "role": { "$ref": "#/definitions/roleItem" },
                "concept": { "$ref": "#/definitions/concept" }
            },
            "additionalProperties": false,
            "required": [ "op", "role", "concept" ]
        },
        "forallItem": {
            "type": "object",
            "properties": {
                "op": { "enum": [ "forall" ] },
                "role": { "$ref": "#/definitions/roleItem" },
                "concept": { "$ref": "#/definitions/concept" }
            },
            "additionalProperties": false,
            "required": [ "op", "role", "concept" ]
        },
        "atleastItem": {
            "type": "object",
            "properties": {
                "op": { "enum": [ "atleast" ] },
                "number": {
                    "type": "integer",
                    "minimum": 1
                },
                "role": { "$ref": "#/definitions/roleItem" },
                "concept": { "$ref": "#/definitions/concept" }
            },
            "additionalProperties": false,
            "required": [ "op", "number", "role", "concept" ]
        },
        "atmostItem": {
            "type": "object",
            "properties": {
                "op": { "enum": [ "atmost" ] },
                "number": {
                    "type": "integer",
                    "minimum": 1
                },
                "role": { "$ref": "#/definitions/roleItem" },
                "concept": { "$ref": "#/definitions/concept" }
            },
            "additionalProperties": false,
            "required": [ "op", "number", "role", "concept" ]
        },
        "identifier": {
            "type": "string",
            "pattern": "^[a-zA-Z][a-zA-Z0-9]*$"
        }
    }
}

模式具有递归引用(例如概念可以是 orItem ,而后者又包含两个概念。当我尝试时要使用json-schema-validator验证以下有效的json,我得到fatal: validation loop: schema "#/definitions/concept/oneOf/0" visited twice for pointer "/ABox/0/concept/concept" of validated instance

json我试图验证:

{
    "RBox": [],
    "TBox": [],
    "ABox": [{"op": "oftype", "individual": "a", "concept": {"op": "exists", "role": "r", "concept": "A"}}]
}

我做错了什么?是否可以使用具有递归定义的模式?如果是这样,为什么?

1 个答案:

答案 0 :(得分:1)

使用tv4代替json-schema-validator