标签: python-jsonschema

如何在 python-jsonschema 文档中设置本地文件引用?

我有一组符合jsonschema 的文档。某些文档包含对其他文档的引用(通过$ref属性)。我不希望托管这些文档,以便它们可以通过 HTTP URI 访问。因此,所有引用都是相对的。所有文档都位于本地文件夹结构中。

我怎样才能python-jsonschema理解正确使用我的本地文件系统来加载引用的文档?


例如,如果我有一个文件名defs.json包含一些定义的文档。我尝试加载引用它的不同文档,例如:

{
  "allOf": [
    {"$ref":"defs.json#/definitions/basic_event"},
    {
      "type": "object",
      "properties": {
        "action": {
          "type": "string",
          "enum": ["page_load"]
        }
      },
      "required": ["action"]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我收到一个错误 RefResolutionError: <urlopen error [Errno 2] No such file or directory: '/defs.json'>

我在 linux 机器上可能很重要。


(我写这个作为问答,因为我很难弄清楚这一点,并且观察到其他人 也遇到了麻烦。)

python json jsonschema python-jsonschema

14
推荐指数
3
解决办法
7288
查看次数

使用 python jsonschema 验证日期时间值

我正在使用jsonschema来验证我的 python 字典。我试图验证一个datetime值,但我不知道如何去做。

这是我到目前为止所拥有的,这会导致错误,因为 jsonschema 没有datetime类型:

order = {
    "name": "shirt",
    "order_datetime": datetime.datetime(2018, 1, 18)
}

schema = {
    "title": "Order",
    "type": "object",
    "required": ["name", "order_datetime"],
    "properties": {
        "name": {
            "type": "string"
        },
        "order_datetime": {
            "type": "datetime"
        }
    }
}

from jsonschema import validate
validate(order, schema)
Run Code Online (Sandbox Code Playgroud)

错误是jsonschema.exceptions.SchemaError: 'datetime' is not valid under any of the given schemas。我怎样才能正确地验证这一点?

python jsonschema python-jsonschema

6
推荐指数
1
解决办法
4703
查看次数

以 JSON 格式格式化气流日志

我需要记录Apache AirflowJSON 格式日志记录到标准输出。Airflow 似乎没有开箱即用地投射此功能。我找到了几个能够完成此任务的 python 模块,但我无法使实现工作。

目前,我正在应用一个类airflow/utils/logging.py来修改记录器,如下所示:

from pythonjsonlogger import jsonlogger

class StackdriverJsonFormatter(jsonlogger.JsonFormatter, object):
def __init__(self, fmt="%(levelname) %(asctime) %(nanotime) %(severity) %(message)", style='%', *args, **kwargs):
    jsonlogger.JsonFormatter.__init__(self, fmt=fmt, *args, **kwargs)

def process_log_record(self, log_record):
    if log_record.get('level'):
        log_record['severity'] = log_record['level']
        del log_record['level']
    else: 
        log_record['severity'] = log_record['levelname']
        del log_record['levelname']
    if log_record.get('asctime'):
        log_record['timestamp'] = log_record['asctime']
        del log_record['asctime']
    now = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
    log_record['nanotime'] = now
    return super(StackdriverJsonFormatter, self).process_log_record(log_record)
Run Code Online (Sandbox Code Playgroud)

我正在执行此代码,/airflow/settings.py如下所示:

from airflow.utils import logging as logconf

def configure_logging(log_format=LOG_FORMAT):
     handler = logconf.logging.StreamHandler(sys.stdout)
     formatter = …
Run Code Online (Sandbox Code Playgroud)

python logging airflow python-jsonschema

6
推荐指数
1
解决办法
2841
查看次数

如何确定一个 json 模式是否是另一个 json 模式的子集?

假设我有两个 json 模式 - 是否可以(在 python 或其他任何地方)确定一个模式是否是另一个模式的子集?

我正在寻找两个 json 模式上的函数,当且仅当第一个 json 模式接受的每个实例也被第二个 json 模式接受时,该函数才会返回 true。

对于一个超级简单的例子,假设我的模式是

int_schema = {'type': 'integer'}
num_schema = {'type': 'number'}
Run Code Online (Sandbox Code Playgroud)

那我就会

subset(int_schema, num_schema) = True
subset(num_schema, int_schema) = False
Run Code Online (Sandbox Code Playgroud)

不太关心这是 python,我更想知道 json-schema 是否支持这个。

python jsonschema python-jsonschema

5
推荐指数
1
解决办法
946
查看次数

jsonschema 扩展并且没有额外的属性

我正在使用 jsonschema 来验证描述条目(给定类型)如何显示的条目类型。这些条目可以有页面并被分成多个方面。

页面和方面都可以有条件,我想重用一个基本模式,即使方面的条件可以有页面条件没有的 2 个其他属性。

这是我经常碰到的普遍问题。我想扩展架构,同时能够在所有情况下将“additionalProperties”设置为 false。

我也不认为有可能用 anyOf、allOf 修复它而没有重复。

还是我应该放弃additionalProperties或接受重复项?


  {
    "$comment": "page condition",
    "type": "object",
    "properties": {
      "condition": {
        "type": "object",
        "properties": {
          "aspect": {
            "type": "string"
          },
          "value": {
            "type": "string"
          },
          "compare": {
            "type": "string"
          }
        },
        "required": [
          "aspect",
          "value"
        ],
        "additionalProperties": false
      }
    }
  }

...
  {
    "$comment": "aspect condition",
    "type": "object",
    "properties": {
      "condition": {
        "type": "object",
        "properties": {
          "aspect": {
            "type": "string"
          },
          "value": {
            "type": "string"
          },
          "compare": …
Run Code Online (Sandbox Code Playgroud)

jsonschema python-jsonschema

5
推荐指数
1
解决办法
1861
查看次数

如何在 Linux cli 中根据元模式验证 json 模式

我正在尝试验证我的复杂json 架构定义文件,以确保架构中没有拼写错误。\n我使用python jsonschema 库jsonschema提供的脚本。

\n

我使用从json 架构规范页面下载的元架构文件。

\n

我下载了“核心/验证方言元模式”文件和所有“单词汇元模式”,添加了“json”扩展名并将这些文件存储在以下结构中:

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 meta\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 applicator.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 content.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 core.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 format-annotation.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 format-assertion.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 meta-data.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 unevaluated.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 validation.json\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 schema.json\n
Run Code Online (Sandbox Code Playgroud)\n

如果我创建此test01.json文件(请注意第 5 行的“objectx”拼写错误):

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 meta\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 applicator.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 content.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 core.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 format-annotation.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 format-assertion.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 meta-data.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 unevaluated.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 validation.json\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 schema.json\n
Run Code Online (Sandbox Code Playgroud)\n

然后验证失败(如预期):

\n
$ jsonschema -i test01.json some/path/schema.json\nobjectx: \'objectx\' is not valid under any of the given schemas\n
Run Code Online (Sandbox Code Playgroud)\n …

json command-line-interface jsonschema python-jsonschema

5
推荐指数
1
解决办法
984
查看次数

如何在 JSON 模式中模拟 switch 语句 (switch-case)?

我正在使用 JSON 架构jsonschema来验证 JSON 记录。这是一个示例架构。这里只有两个案例,但想象一下类似的场景,其中有一百个像这样布置的案例。

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "oneOf": [
      {
        "type": "object",
        "required": ["a", "b", "c"],
        "properties": {
          "a": {"type": "integer", "enum": [0]},
          "b": {"type": "integer", "enum": [0, 2, 4, 6, 8]},
          "c": {"type": "string", "enum": ["always the same"]}
        }
      },
      {
        "type": "object",
        "required": ["a", "b", "c"],
        "properties": {
          "a": {"type": "integer", "enum": [1]},
          "b": {"type": "integer", "enum": [1, 3, 5, 7, 9]},
          "c": {"type": "string", "enum": ["always the same"]}
        }
      }
    ]
}
Run Code Online (Sandbox Code Playgroud)

关键问题是字段的重复"c" …

validation jsonschema switch-statement python-jsonschema

3
推荐指数
1
解决办法
2856
查看次数

python jsonschema删除其他并使用默认值

我正在使用python jsonschema https://python-jsonschema.readthedocs.io/en/latest/, 并且我试图找到如何使用默认值并在找到时删除其他字段。

有人知道我应该怎么做吗?还是有另一种解决方案来验证支持默认值的jsonschema并删除任何其他字段(例如js avj)?

python jsonschema python-jsonschema

2
推荐指数
1
解决办法
717
查看次数

如何通过 Python 使用 jsonschema 验证字典列表

我有一个这样的字典列表:

list_of_dictionaries = [{'key1': True}, {'key2': 0.2}]
Run Code Online (Sandbox Code Playgroud)

我想使用 jsonschema 包来验证它。

我创建了这样的架构:

schema = {
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "key1": {
                "type": "boolean"
            },
            "key2": {
                "type": "number"
            }
        },
        "required": ["enabled"]
    }
}
Run Code Online (Sandbox Code Playgroud)

但这对于我的列表来说是不正确的,因为要使其正常工作,我的列表应该是这样的:

list_dict = [{'key1': True, 'key2': 0.5}]
Run Code Online (Sandbox Code Playgroud)

如何创建正确的架构来验证我的列表?先感谢您。

python jsonschema python-jsonschema

2
推荐指数
1
解决办法
942
查看次数