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

xam*_*mox 4 json jsonschema json-schema-validator

所以我有一个类似的问题(请参阅:如何在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'
 }
Run Code Online (Sandbox Code Playgroud)

我使用的是与上一个链接中列出的相同的模式:

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"]
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试使用数组而不是对象,但没有运气我在尝试验证时遇到以下错误:

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'
Run Code Online (Sandbox Code Playgroud)

jru*_*ren 11

问题在于对前一个问题的误解.在您的规范中inspections:

 "inspections" : { 
            "type" : "array",
            "items" : {
                "type" : "object",
                "maxProperties": 1,
                "minProperties": 1,
                "additionalProperties" : {
                    "$ref" : "#/definitions/inspection"
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

这意味着inspections必须是一个数组,并且其项必须objects具有单个属性.该属性必须符合#/definitions/inspection架构.

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

"inspections" : [{
        "anyKeyIsValidHere" : {
            "expected" : "MVA",
            "found" : 0.0,
            "inspection" : "Fascia",
            "location" : "rear_left",
            "state" : 0
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

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

"inspections" : {
    "type" : "array",
    "items" : {
        "$ref" : "#/definitions/inspection"
    }
}
Run Code Online (Sandbox Code Playgroud)

最后的建议.尝试逐步构建模式,确保正确实施每个所需的约束.这也有助于提出更集中的SO问题.