简体   繁体   English

如何在json模式中执行非字典的模式引用的嵌套列表(数组)

[英]How do I do a nested list (array) of schema references in json schema that isn't a dictionary

So I have a similiar question (see: How do I do a nested list (array) of schema references in json schema ), but now my structure is changed a little and can't seem to get it to validate. 所以我有一个类似的问题(请参阅: 如何在json模式中执行模式引用的嵌套列表(数组) ),但现在我的结构发生了一些变化,似乎无法让它进行验证。

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'
 }

I'm using the same schema as listed in the previous link here: 我使用的是与上一个链接中列出的相同的模式:

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"]
}

I've tried making the definition use array instead of object but no luck I get the following error when trying to validate: 我已经尝试使用数组而不是对象,但没有运气我在尝试验证时遇到以下错误:

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'

The problem lies in the same misunderstanding of the previous question. 问题在于对前一个问题的误解。 In the specification of the inspections you have: inspections规范中,您有:

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

This means that inspections must be an array, and its items must be objects with a single property. 这意味着inspections必须是一个数组,其项必须是具有单个属性的objects That property must comply with #/definitions/inspection schema. 该属性必须符合#/definitions/inspection模式。

According to your current schema, inspections items should be like: 根据您当前的架构, inspections项目应如下:

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

Hence, in this case, reversely than in your previous question, your inspections item should simply be like this: 因此,在这种情况下,与上一个问题相反,您的inspections项目应该是这样的:

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

A final advice. 最后的建议。 Try to build the schemas step by step, ensuring that each desired constraint is properly enforced. 尝试逐步构建模式,确保正确实施每个所需的约束。 That also helps to make more focused SO questions. 这也有助于提出更集中的SO问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM