23andMe / Yamale

A schema and validator for YAML.
MIT License
679 stars 88 forks source link

Issue validating numerical keys #157

Closed pgodzin closed 3 years ago

pgodzin commented 3 years ago

I am getting issues with numerical keys that I cannot reproduce with the same schema but with alphanumerical keys.

import yamale

def test_yamale_validate():
    schema = \
        '123: \n' \
        '   name: str()'

    schema = yamale.make_schema(content=schema)
    data = yamale.make_data(content='''
        "123":
            name: "Test"
        ''')
    assert yamale.validate(schema, data)

results in

 schema = <yamale.schema.schema.Schema object at 0x7f063322f910>
 data = [({'123': {'name': 'Test'}}, None)], strict = True, _raise_error = True

     def validate(schema, data, strict=True, _raise_error=True):
         results = []
         is_valid = True
         for d, path in data:
             result = schema.validate(d, path, strict)
             results.append(result)
            is_valid = is_valid and result.isValid()
        if _raise_error and not is_valid:
>           raise YamaleError(results)
E           yamale.yamale_error.YamaleError: Error validating data
E               123: Unexpected element
E               123: Required field missing

However, if 123 is replaced with ABC123 the test case passes

mildebrandt commented 3 years ago

Hi, thanks for using Yamale!

In your schema, you're expecting an integer as the key....but your data has a string "123". Try changing this:

    data = yamale.make_data(content='''
        "123":
            name: "Test"
        ''')

to this:

    data = yamale.make_data(content='''
        123:
            name: "Test"
        ''')
pgodzin commented 3 years ago

I see, thanks for the help!