0

I have a YAML config file and I want to validate it using Cerberus. The problem is my YAML file is a kind of 3 layered dictionaries and it seems the validation function does not work when we have more than 2 nestings. As an example when I run the following code:

a_config = {'dict1': {'dict11': {'dict111': 'foo111',
                                 'dict112': 'foo112'},
                      'dict12': {'dict121': 'foo121',
                                 'dict122': 'foo122'}},
            'dict2': 'foo2'}

a_simple_config = {'dict1': {'dict11': 'foo11'}, 'dict2': 'foo2'}

print(type(a_config))
print(type(a_simple_config))

simple_schema = {'dict1': {'type': 'dict', 'schema': {'dict11': {'type': 'string'}}}, 'dict2': {'type': 'string'}}
v_simple = Validator(simple_schema)

schema = {
    'dict1': {
        'type': 'dict',
        'schema': {
            'dict11': {
                'type': 'dict',
                'schema': {
                    'dict111': {'type': 'string'},
                    'dict112': {'type': 'string'}
                }
            }
        }
    },
    'dict2': {'type': 'string'}
}

v = Validator(schema)
print(v.validate(a_config, schema))
print(v.errors)

I get this:

True
{}
False
{'dict1': [{'dict12': ['unknown field']}]}

I think validating 3 layered files is not supported. So my only idea is to try to validate it from layer 2 and if all of them are valid then conclude my file is valid. I wish to know am I making some mistake with writing my schema when I have 3 layers? or, is there exists a better idea for validating such files?

Edit: @flyx claimed that the problem is in the definition of dict12, so I decided to replace it. AND NOTHING changed. I again have the same output!

Doralisa
  • 171
  • 4
  • The code you show contains no evidence that 3 layered data is not supported. It simply complains that `dict12` in `a_config` is unexpected according to the given schema (which only expects `dict11`). – flyx Mar 15 '21 at 12:27
  • @flyx dict12 is a dictionary by itself. I just didn't write it all, you just think of it as dict11. If I write it I again get the same error. – Doralisa Mar 15 '21 at 16:48
  • Firstly, `dict12` is not a dictionary, it is a set: `{'foo12'}` contains a single value, not a key-value pair. Then, it doesn't matter what I think of it, it matters what cerberus thinks of it. And cerberus doesn't expect it there because your schema doesn't define it. That results in the error you show. – flyx Mar 15 '21 at 17:04
  • @flyx of course dict12 is a set that contains a single value string! As I said if I replace it with a dictionary the error again appears. – Doralisa Mar 16 '21 at 10:05
  • Yes, because the schema only defines `dict11` and therefore, cerberus tells you „I found a `dict12` but it's not mentioned in the schema, hence it is an unknown field“. What is unclear about this? – flyx Mar 16 '21 at 10:14
  • @flyx I have changed the $a_config$ definition. Are you saying my schema is wrong? so how should it be for such files? – Doralisa Mar 16 '21 at 10:19
  • I don't know, schemas cannot be derived from example structures because those only show a single instance of what should be possible. But if you think `dict12` should be allowed and should contain a set, adding `'dict12': {'type': 'set'}` to the schema seems to be a good start. – flyx Mar 16 '21 at 10:43

0 Answers0