python - 皮克瓦利 : Validate data in a dictionary against a yaml file schema

标签 python json yaml pyyaml

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

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

最佳答案

我自己找到了答案。如果未指定 source_file,则从 pyKwalify 类的源 Core 类接受 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

所以我可以使用 as-

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

关于python - 皮克瓦利 : Validate data in a dictionary against a yaml file schema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40926862/

相关文章:

mysql - 如何使用Python最有效的方式将列表中可变数量的元素复制到统一列表并存储在MySQL中?

python - 提取不同表中2列之间的常用词python

javascript - 在node.js中获取JSON数组

symfony - 在 symfony2 config.yml 文件中导入 yml 配置

Java-读取 YAML 文件 - 线程 "main"中出现异常无法在读取器中创建 property=Envs

grails - 使用SpringBoot的Grails应用程序针对不同的环境使用不同的yml文件

python - 将 .profo 文件的文件夹编译为多种语言

python - 错误 :document must be an instance of dict, bson.son.SON、bson.raw_bson.RawBSONDocument 或继承自 collections.MutableMapping 的类型

jquery - Select2 不起作用,始终显示 "No results found"

javascript - 阅读 jQuery JSON 结构 - 无法让它工作