如何根据包含所有定义的模式验证单个JSON对象?

时间:2014-03-10 16:10:52

标签: json jsonschema

我构建了我的JSON模式,每个对象需要验证一个模式,并在继续下一个之前测试每个组件。单独地,它们都按设计工作。

接下来,我将所有模式粘贴到一个大模式文件中,并尝试验证其中一个测试对象。它通过但应该失败。它似乎没有应用任何模式,一切都在通过。具体来说,在下面包含的架构代码段中,我的对象的DataInterfaceID-1,但它仍然通过了验证。 Evertyhing一直工作,直到我将各个模式合并到一个文件中。

这是架构,减少到重现问题所需的最小值:

{
    '$schema': 'http: //json-schema.org/draft-04/schema',
    'type': 'object',
    'id': 'http: //sampleURI/',
    'required': false,
    'properties': {
        'EquipmentSchedule': {
            'title': 'EquipmentSchedule',
            'required': false,
            'description': 'EquipmentSchedule',
            'type': 'object',
            'properties': {
                'DataInterfaceID': {
                    'type': 'integer',
                    'minimum': 1,
                    'required': true
                }

            }
        }
    }
}

即使缺少必需的元素,验证也会通过。为什么这不起作用,我该如何解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:0)

对不起,这是一个迟到的回复,但草案4的要求将按如下方式实施:

{
    "$schema": "http: //json-schema.org/draft-04/schema",
    "type": "object",
    "id": "http: //sampleURI/",
    "properties": {
        "EquipmentSchedule": {
            "title": "EquipmentSchedule",
            "description": "EquipmentSchedule",
            "type": "object",
            "properties": {
                "DataInterfaceID": {
                    "type": "integer",
                    "minimum": 1
                }
            },
            "required": [ "DataInterfaceID" ]
        }
    }
}

Petru最好地解释了推理,但我决定向你展示一个例子。