Validating a yaml document in python

前端 未结 10 2146
谎友^
谎友^ 2020-12-24 04:24

One of the benefits of XML is being able to validate a document against an XSD. YAML doesn\'t have this feature, so how can I validate that the YAML document I open is in th

相关标签:
10条回答
  • 2020-12-24 05:17

    I am into same situation. I need 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"
    

    Thanks

    0 讨论(0)
  • 2020-12-24 05:18

    Yes - having support for validation is vital for lots of important use cases. See e.g. YAML and the importance of Schema Validation « Stuart Gunter

    As already mentioned, there is Rx, available for various languages, and Kwalify for Ruby and Java.

    See also the PyYAML discussion: YAMLSchemaDiscussion.

    A related effort is JSON Schema, which even had some IETF standardization activity (draft-zyp-json-schema-03 - A JSON Media Type for Describing the Structure and Meaning of JSON Documents)

    0 讨论(0)
  • 2020-12-24 05:24

    I wrapped some existing json-related python libraries aiming for being able to use them with yaml as well.

    The resulting python library mainly wraps ...

    • jsonschema - a validator for json files against json-schema files, being wrapped to support validating yaml files against json-schema files in yaml-format as well.

    • jsonpath-ng - an implementation of JSONPath for python, being wrapped to support JSONPath selection directly on yaml files.

    ... and is available on github:

    https://github.com/yaccob/ytools

    It can be installed using pip:

    pip install ytools

    Validation example (from https://github.com/yaccob/ytools#validation):

    import ytools
    ytools.validate("test/sampleschema.yaml", ["test/sampledata.yaml"])
    

    What you don't get out of the box yet, is validating against external schemas that are in yaml format as well.

    ytools is not providing anything that hasn't existed before - it just makes the application of some existing solutions more flexible and more convenient.

    0 讨论(0)
  • 2020-12-24 05:30

    Try Rx, it has a Python implementation. It works on JSON and YAML.

    From the Rx site:

    "When adding an API to your web service, you have to choose how to encode the data you send across the line. XML is one common choice for this, but it can grow arcane and cumbersome pretty quickly. Lots of webservice authors want to avoid thinking about XML, and instead choose formats that provide a few simple data types that correspond to common data structures in modern programming languages. In other words, JSON and YAML.

    Unfortunately, while these formats make it easy to pass around complex data structures, they lack a system for validation. XML has XML Schemas and RELAX NG, but these are complicated and sometimes confusing standards. They're not very portable to the kind of data structure provided by JSON, and if you wanted to avoid XML as a data encoding, writing more XML to validate the first XML is probably even less appealing.

    Rx is meant to provide a system for data validation that matches up with JSON-style data structures and is as easy to work with as JSON itself."

    0 讨论(0)
提交回复
热议问题