簡體   English   中英

使用模式列表的python jsonschema驗證

[英]python jsonschema validation using schema list

我正在嘗試使用 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"
  }
 }
}

您一次只能針對一個架構進行驗證,但該架構可以引用 ( $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
    }
  }
}

一些可能有用的文檔

您可以創建一個引用其他模式文件的小模式,而不是從所有模式中手動編碼單個模式。 通過這種方式,您可以使用多個現有的 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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM