Search code examples
pythonjsonyamlpyyaml

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


I have python dictionary and a schema.yaml. Is there a way to validate both ? If i dump the dictionary into a yaml file as data.yaml, i can use below code for validation. Is there a way to validate schema file with dictionary?

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

Solution

  • I have found an answer myself. From the pyKwalify class's source Core class accepts source_data if no source_file is specified.

    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
    

    So i can use as-

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