使用模式列表

时间:2015-07-29 14:42:41

标签: python json jsonschema

我正在尝试使用python和jsonschema模块针对模式验证json文件。我的模式由模式列表组成,其中一个模式具有基本元素的定义,其余模式是这些元素和其他对象的集合。

我找不到加载模式列表的函数文档,以便我可以使用它进行验证。我尝试将模式分离到字典中并在jsonObject上调用相应的模式,但这不起作用,因为它们相互交叉引用。

如何将所有模式加载/组装到一个模式中进行验证?

我正在尝试加载的部分模式:

[{
    "definitions": {
     "location": {
       "required": [
         "name",
         "country"
       ],
       "additionalProperties": false,
       "properties": {
         "country": {
           "pattern": "^[A-Z]{2}$",
           "type": "string"
         },
         "name": {
           "type": "string"
         }
       },
       "type": "object"
      }
    },
    "required": [
       "type",
       "content"
    ],
    "additionalProperties": false,
    "properties": {
     "content": {
      "additionalProperties": false,
      "type": "object"
     },
     "type": {
      "type": "string"
     }
    },
    "type": "object",
    "title": "base",
    "$schema": "http://json-schema.org/draft-04/schema#"
  },
  {
    "properties": {
      "content": {
        "required": [
          "address"
        ],
        "properties": {
          "address": {
            "$ref": "#/definitions/location"
        }
      },
      "type": {
        "pattern": "^person$"
      }
    }
  }]

json对象看起来像这样:

{
 "type":"person",
 "content":{
  "address": {
   "country": "US",
   "name" : "1,Street,City,State,US"
  }
 }
}

2 个答案:

答案 0 :(得分:2)

您一次只能对一个架构进行验证,但该架构可以引用($ref)外部架构。这些引用通常是可用于获取模式的URI。如果您的模式不公开,则文件路径也可能有效。使用您的示例的固定版本,这看起来像这样......

http://your-domain.com/schema/person

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Person",
  "allOf": [{ "$ref": "http://your-domain.com/schema/base#" }],
  "properties": {
    "type": { "enum": ["person"] },
    "content": {
      "properties": {
        "address": { "$ref": "http://your-domain.com/schema/base#/definitions/location" }
      },
      "required": ["address"],
      "additionalProperties": false
    }
  }
}

http://your-domain.com/schema/base

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "base",
  "type": "object",
  "properties": {
    "content": { "type": "object" },
    "type": { "type": "string" }
  },
  "required": ["type", "content"],
  "additionalProperties": false,
  "definitions": {
    "location": {
      "type": "object",
      "properties": {
        "country": {
          "type": "string",
          "pattern": "^[A-Z]{2}$"
        },
        "name": { "type": "string" }
      },
      "required": ["name", "country"],
      "additionalProperties": false
    }
  }
}

一些可能有用的文档

答案 1 :(得分:0)

您可以创建一个引用到其他模式文件的小模式,而不是从所有模式中手动编写单个模式。这样,您可以使用多个现有的JSONschema文件并对它们进行组合验证:

import yaml
import jsonschema

A_yaml = """
id: http://foo/a.json
type: object
properties: 
    prop: 
        $ref: "./num_string.json"
"""

num_string_yaml = """
id: http://foo/num_string.json
type: string
pattern: ^[0-9]*$
"""

A = yaml.load(A_yaml)
num_string = yaml.load(num_string_yaml)

resolver = jsonschema.RefResolver("",None,
        store={
            "http://foo/A.json":A,
            "http://foo/num_string.json":num_string,
            })

validator = jsonschema.Draft4Validator(
        A, resolver=resolver)

try:
    validator.validate({"prop":"1234"})
    print "Properly accepted object"
except jsonschema.exceptions.ValidationError: 
    print "Failed to accept object"

try:
    validator.validate({"prop":"12d34"})
    print "Failed to reject object"
except jsonschema.exceptions.ValidationError: 
    print "Properly rejected object"

请注意,您可能希望使用其中一个架构参与者oneOfallOfanyOf组合外部,以组合您的架构,如下所示:

[A.yaml]
oneOf:
     - $ref: "sub-schema1.json"
     - $ref: "sub-schema2.json"
     - $ref: "sub-schema3.json"