【问题标题】:How do I do a nested list (array) of schema references in json schema that isn't a dictionary如何在不是字典的 json 模式中执行模式引用的嵌套列表(数组)
【发布时间】:2016-03-08 19:23:58
【问题描述】:

所以我有一个类似的问题(请参阅: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'

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    问题在于对上一个问题的同样误解。在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 问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-16
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 2017-06-19
      • 2012-03-07
      相关资源
      最近更新 更多