python - 检测 json schema 是否具有 oneOf 类型 schema

标签 python json jsonschema json-schema-validator

我想检查一个模式中是否只有一个模式,或者是否有多个具有 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 中执行此操作?

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

最佳答案

这里是一个示例,展示了一种递归检查所提供的 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

关于python - 检测 json schema 是否具有 oneOf 类型 schema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47761883/

相关文章:

python - Bokeh 悬停工具提示可显示来自不同列的数据

python - 填充 Spark 中日期缺失值的优雅方法

java - JSONArray 类型的 add(String) 方法未定义

Json 架构 - 不需要则不起作用

jsonschema - 如何使用 JSON Schema (ajv) 验证数字中的位数?

c++ - 如何获取 RapidJSON 模式来处理默认属性

python - InternalError_ : Spectrum Scan Error. S3 到 Redshift 复制命令

python - Keras - 摘要直方图 LSTM 中的 Nan

Jquery json 响应变量值

android - 如何在 Android 布局中显示 Html 内容?