Validating a yaml document in python

前端 未结 10 2210
谎友^
谎友^ 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:06

    Given that JSON and YAML are pretty similar beasts, you could make use of JSON-Schema to validate a sizable subset of YAML. Here's a code snippet (you'll need PyYAML and jsonschema installed):

    from jsonschema import validate
    import yaml
    
    schema = """
    type: object
    properties:
      testing:
        type: array
        items:
          enum:
            - this
            - is
            - a
            - test
    """
    
    good_instance = """
    testing: ['this', 'is', 'a', 'test']
    """
    
    validate(yaml.load(good_instance), yaml.load(schema)) # passes
    
    # Now let's try a bad instance...
    
    bad_instance = """
    testing: ['this', 'is', 'a', 'bad', 'test']
    """
    
    validate(yaml.load(bad_instance), yaml.load(schema))
    
    # Fails with:
    # ValidationError: 'bad' is not one of ['this', 'is', 'a', 'test']
    #
    # Failed validating 'enum' in schema['properties']['testing']['items']:
    #     {'enum': ['this', 'is', 'a', 'test']}
    #
    # On instance['testing'][3]:
    #     'bad'
    

    One problem with this is that if your schema spans multiple files and you use "$ref" to reference the other files then those other files will need to be JSON, I think. But there are probably ways around that. In my own project, I'm playing with specifying the schema using JSON files whilst the instances are YAML.

提交回复
热议问题