Validating a yaml document in python

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

    I find Cerberus to be very reliable with great documentation and straightforward to use.

    Here is a basic implementation example:

    my_yaml.yaml:

    name: 'my_name'
    date: 2017-10-01
    metrics:
        percentage:
        value: 87
        trend: stable
    

    Defining the validation schema in schema.py:

    {
        'name': {
            'required': True,
            'type': 'string'
        },
        'date': {
            'required': True,
            'type': 'date'
        },
        'metrics': {
            'required': True,
            'type': 'dict',
            'schema': {
                'percentage': {
                    'required': True,
                    'type': 'dict',
                    'schema': {
                        'value': {
                            'required': True,
                            'type': 'number',
                            'min': 0,
                            'max': 100
                        },
                        'trend': {
                            'type': 'string',
                            'nullable': True,
                            'regex': '^(?i)(down|equal|up)$'
                        }
                    }
                }
            }
        }
    }
    

    Using the PyYaml to load a yaml document:

    import yaml
    def load_doc():
        with open('./my_yaml.yaml', 'r') as stream:
            try:
                return yaml.load(stream)
            except yaml.YAMLError as exception:
                raise exception
    
    ## Now, validating the yaml file is straightforward:
    from cerberus import Validator
    schema = eval(open('./schema.py', 'r').read())
        v = Validator(schema)
        doc = load_doc()
        print(v.validate(doc, schema))
        print(v.errors)
    

    Keep in mind that Cerberus is an agnostic data validation tool, which means that it can support formats other than YAML, such as JSON, XML and so on.

提交回复
热议问题