Reputation: 215
Given a dictionary where the top level keys can be any value, but there is a strict schema within the values of those keys:
{"rand_value": ["key1": "val1", "key2": "val2"], "another_rand_value": ["key1": "val1", "key2": "val2"]}
Can I create a Cerberus schema which will enforce this?
Upvotes: 1
Views: 915
Reputation: 184221
Cerberus must know the field name so it can determine which validation rule applies to it, so you can't do exactly what you're asking. There are no "top level" rules that apply to the whole document, and Cerberus doesn't support wildcards for field names.
You can, however, build a schema "on the fly" based on the actual field names present in the document, then validate against that.
v = cerberus.Validator()
document = {"rand_value": {"key1": "val1", "key2": "val2"},
"another_rand_value": {"key1": "val1", "key2": "val2"}}
fieldrule = {"type": "dict", "keysrules": {"type": "string"}} # etc
v.validate(document, {field: fieldrule for field in document})
Upvotes: 1