如何在不是字典的 json 模式中创建模式引用的嵌套列表(数组)

2024-04-07

所以我有一个类似的问题(参见:如何在 json 模式中创建模式引用的嵌套列表(数组) https://stackoverflow.com/questions/35545812/how-do-i-do-a-nested-list-array-of-schema-references-in-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'

问题出在对上一个问题的同样的误解上。在规范中inspections你有:

 "inspections" : { 
            "type" : "array",
            "items" : {
                "type" : "object",
                "maxProperties": 1,
                "minProperties": 1,
                "additionalProperties" : {
                    "$ref" : "#/definitions/inspection"
                }
            }
        }

这意味着inspections必须是一个数组,并且它的项必须是objects具有单一财产。该财产必须符合#/definitions/inspection架构。

根据您当前的架构,inspections项目应该是这样的:

"inspections" : [{
        "anyKeyIsValidHere" : {
            "expected" : "MVA",
            "found" : 0.0,
            "inspection" : "Fascia",
            "location" : "rear_left",
            "state" : 0
        }
    }
]

因此,在这种情况下,与您之前的问题相反,您的inspections项目应该是这样的:

"inspections" : {
    "type" : "array",
    "items" : {
        "$ref" : "#/definitions/inspection"
    }
}

最后的建议。尝试逐步构建模式,确保正确执行每个所需的约束。这也有助于提出更有针对性的 SO 问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在不是字典的 json 模式中创建模式引用的嵌套列表(数组) 的相关文章

随机推荐