Pykwalify:根据 yaml 文件模式验证字典中的数据

Pykwalify: Validate data in a dictionary against a yaml file schema

我有 python 字典和一本 schema.yaml。有没有办法验证两者?如果我将字典作为 data.yaml 转储到 yaml 文件中,我可以使用下面的代码进行验证。 有没有办法用字典验证模式文件?

from pykwalify.core import Core
c = Core(source_file="data.yaml", schema_files=["schema.yaml"])
c.validate(raise_exception=True)

我自己找到了答案。来自 pyKwalify class 的来源 Core 如果未指定 source_file,class 接受 source_data

class Core(object):
""" Core class of pyKwalify """

  def __init__(self, source_file=None, schema_files=[], source_data=None, schema_data=None, extensions=[]):
    ...
    ...
    if self.source is None:
        log.debug(u"No source file loaded, trying source data variable")
        self.source = source_data

所以我可以用作-

c = Core(source_data=data_dict, schema_files=["schema.yaml"])