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

时间:2016-12-02 07:22:42

标签: python json yaml pyyaml

我有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)

1 个答案:

答案 0 :(得分:2)

我自己找到了答案。如果没有指定Core,则pyKwalify类的源source_data类接受source_file

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

所以我可以使用as-

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