Wednesday, 15 March 2017

Yaml File validation with Pykwalify and Python






Pykwalify for YAML Schema or Validation of YAML file in python:


Heres a quick research on how to validate the elements of YAML.
First I thought 'PyYAML tags' is the best and simple way. But later decided to go with 'PyKwalify' which actually defines a schema for YAML.

PyYAML tags:

The YAML file has a tag support where we can enforce this basic checks by prefixing the data type. (e.g) For integer - !!int "123"
More on PyYAML: http://pyyaml.org/wiki/PyYAMLDocumentation#Tags This is good, but if you are going to expose this to the end user, then it might cause confusion. I did some research to define a schema of YAML. The idea is like we can validate the YAML with its corresponding schema for basic data type check. Also even our custom validations like IP address, random strings can be added in this. so we can have our schema separately leaving YAML simple and readable.
I am unable to post more links. Please 'google schema for YAM'L to view the schema discussions.

PyKwalify:

There is a package called PyKwalify which serves this purpose: https://pypi.python.org/pypi/pykwalify
This package best fits my requirements. I tried this with a small example in my local set up, and is working. Heres the sample schema file.
 
#sample schema

type: map
mapping:
    Emp:
        type:    map
        mapping:
            name:
                type:      str
                required:  yes
            email:
                type:      str
            age:
                type:      int
            birth:
                type:     str


Valid YAML file for this schema
---
Emp:
    name:   "abc"
    email:  "xyz@gmail.com"
    age:    yy
    birth:  "xx/xx/xxxx"

As you can see, Pykwalify makes it more simple.

Thanks
~Hari 


1 comment: