2016-12-02 103 views
2

我有python字典和schema.yaml。有沒有一種方法來驗證兩者?如果我將字典轉儲爲yaml文件作爲data.yaml,我可以使用下面的代碼進行驗證。 有沒有辦法用字典驗證模式文件?Pykwalify:根據yaml文件模式驗證字典中的數據

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

回答

2

我自己找到了答案。如果沒有指定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"]) 
+1

你有沒有在你的最後一行代碼的意思'source_data',而不是'source_file'? – dougli1sqrd

+0

Right @ dougli1sqrd。糾正。謝謝。 – Rilwan