如何在JSON schema中进行嵌套的模式引用列表(数组),而不是字典。

11

我有一个类似的问题(请参见:如何在json schema中嵌套列表(数组)的模式引用),但现在我的结构有些改变,似乎无法进行验证。

data = {
  'VIN': '1234567',
  'Vehicle color': blue,
  'inspections': [
      {'expected': 'MVA',
      'found': 0.0,
      'inspection': 'Fascia',
      'location': 'rear_left',
      'state': None},
      {'expected': 'MVA',
      'found': 0.0,
      'inspection': 'Fascia',
      'location': 'rear_right',
      'state': None},
      {'expected': 'UNKNOWN',
      'found': 'CPW7',
      'inspection': 'liftGateHandle',
      'location': 'center_bottom',
      'state': True},
      {'expected': 'tinted',
      'found': 'tinted',
      'inspection': 'rearWindowtint',
      'location': 'center_top',
      'state': True},
  ],
  'model': 'racecar',
  'timestamp': '2016-03-03 01:44:00.616000'
 }

我正在使用与此前链接中列出的相同模式:

schema = {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "inspection": {
            "type": "object",
            "properties": {
                "expected" : { "type" : "string" },
                "found": { "type" : "string"},
                "state" : { "type" : "string" },
                "image" : { "type" : "string"}
            },
            "required": ["state", "image","expected"]
        },
    },
    "type" : "object",
    "properties" : {
        "VIN" : { "type" : "string" },
        "timestamp" : { "type" : "string"},
        "model" : { "type" : "string"},
        "inspections" : { 
            "type" : "array",
            "items" : {
                "type" : "object",
                "maxProperties": 1,
                "minProperties": 1,
                "additionalProperties" : {
                    "$ref" : "#/definitions/inspection"
                }
            }
        }
    },
    "required": ["VIN", "timestamp", "model", "inspections"]
}

我尝试使用数组而不是对象来定义,但没有成功。当我尝试验证时,出现以下错误:

ValidationError: 'black' is not of type 'object'

Failed validating 'type' in schema['properties']['inspections']['items']['additionalProperties']:
    {'properties': {'expected': {'type': 'string'},
                    'found': {'type': 'string'},
                    'image': {'type': 'string'},
                    'state': {'enum': [0, 1]}},
     'required': ['state', 'image', 'expected'],
     'type': 'object'}

On instance['inspections'][0]['expected']:
    'black'
1个回答

17

问题在于对上一个问题的相同误解。在检查的规范中,您有:

 "inspections" : { 
            "type" : "array",
            "items" : {
                "type" : "object",
                "maxProperties": 1,
                "minProperties": 1,
                "additionalProperties" : {
                    "$ref" : "#/definitions/inspection"
                }
            }
        }
这意味着检查必须是一个数组,其项目必须是具有单个属性的对象。该属性必须符合#/definitions/inspection模式。
根据您当前的模式,检查项目应如下所示:
"inspections" : [{
        "anyKeyIsValidHere" : {
            "expected" : "MVA",
            "found" : 0.0,
            "inspection" : "Fascia",
            "location" : "rear_left",
            "state" : 0
        }
    }
]
因此,在这种情况下与您之前的问题相反,您的 inspections 项目应该是这样的:
"inspections" : {
    "type" : "array",
    "items" : {
        "$ref" : "#/definitions/inspection"
    }
}
一个最后的建议。尝试逐步构建模式,确保每个所需的约束得到正确实施。这也有助于提出更专注的SO问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接