【问题标题】:Detect if a json schema has a oneOf type schema检测 json 模式是否具有 oneOf 类型模式
【发布时间】:2017-12-11 21:40:50
【问题描述】:

我想检查一个模式中是否只有一个模式,或者它是否有多个具有 oneOf 属性的模式。

python 代码应该是这样的

If schema1 has oneOf property:
    Some code1
If schema1 is just a single schema:
    Some code2

基本上我希望能够区分这两种模式

架构1

"schema1": {
    "definitions": {
        "schema": {
            "type": "object",
            "properties": {
                "name": {
                    "type": ["string", "null"]
                }
            }
        }
    }
}

架构2

"schema2": {
    "definitions": {
        "schema": {
            "oneOf": [
            {
                "type": ["null"]
            },
            {
                "type": ["string"],
                "enum": ["NONE"]
            }
            ]
        }
    }
}

如何在 Python 中做到这一点?

编辑:更正了我的示例架构

【问题讨论】:

  • 模式看起来“错误” - 它们是您正在使用的实际模式吗......如果是这样,对象本身明确说明它们是哪个模式。如果不是..请显示一些示例数据,您必须在其中确定哪个架构已到位。
  • @KeithJohnHutchison 我已经更正了问题中的示例架构。这不是我正在使用的确切架构,但与它相似。我的编辑现在是否纠正了架构,还是仍然错误?

标签: python json jsonschema json-schema-validator


【解决方案1】:

这是一个示例,展示了一种递归检查提供的 json 中是否存在 oneOf 属性的方法。如果您只想检查 json 的“模式”部分,则需要检查父属性。

#!/usr/bin/env python

import json


def objectHasKey(object_,key_):
    _result = False 
    if (type(object_)==dict):
        for _key in object_.keys():
            print _key
            if (type(object_[_key])==dict):
                _dict = object_[_key]
                _result = objectHasKey(_dict,key_)
            if _key == key_:
                _result = True
            if _result:
                break
    return _result

firstJSONText = '''
{
    "definitions": {
        "schema": {
            "type": "object",
            "properties": {
                "name": {
                    "type": [
                        "string",
                        "null"
                    ]
                }
            }
        }
    }
}
'''

first = json.loads(firstJSONText)

secondJSONText = '''
{
    "definitions": {
        "schema": {
            "oneOf": [
                {
                    "type": [
                        "null"
                    ]
                },
                {
                    "type": [
                        "string"
                    ],
                    "enum": [
                        "NONE"
                    ]
                }
            ]
        }
    }
}
'''

second = json.loads(secondJSONText)

target = first

if objectHasKey(target,'oneOf'):
    print "Handle oneOf with first"
else:
    print "Handle default with first"

target = second

if objectHasKey(target,'oneOf'):
    print "Handle oneOf with second"
else:
    print "Handle default with second"

带输出的示例调用

csmu-macbook-pro-2:detect-if-a-json-schema-has-a-oneof-type-schema admin$ ./test-for-schema.py 
definitions
schema
type
properties
name
type
Handle default with first
definitions
schema
oneOf
Handle oneOf with second

【讨论】:

  • 只是一个简短的说明。从我在代码中看到的任何“oneOf”都可以在树下找到。所以它可能不属于您正在查看的(子)模式。该代码不适用于 $ref。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
  • 2016-10-17
相关资源
最近更新 更多