Validating values of dicts with arbitrary/unknown names of keys - is it possible..?
See original GitHub issueHaving a document like this one:
document = {
'aaa': {
'bbb': [
{'ddd': {'xxx': 123, 'yyy': 'some string'}},
{'eee': {'zzz': 555}},
],
'ccc': [
{'ddd': {'xxx': 789, 'yyy': 'some other string'}},
]
}
}
…I can validate it with the following schema:
schema = {
'aaa': {
'type': 'dict',
'schema': {
'bbb': {
'type': 'list',
'schema': {
'type': 'dict',
'schema': {
'ddd': {
'type': 'dict',
'schema': {
'xxx': {'type': 'integer'},
'yyy': {'type': 'string'},
},
},
'eee': {
'type': 'dict',
'schema': {
'zzz': {'type': 'integer'},
},
}
}
}
},
'ccc': {
'type': 'list',
'schema': {
'type': 'dict',
'schema': {
'ddd': {
'type': 'dict',
'schema': {
'xxx': {'type': 'integer'},
'yyy': {'type': 'string'},
},
}
}
}
} # /ccc
}
} # /aaa
}
…but the thing is, I need the possibility to use arbitrary names for keys bbb
and ccc
(and yes, there may be an arbitrary number of them on this level).
In other words, how can I validate their values (which are lists of ddd
and eee
dicts, and those dicts always have the same structure), without knowing their names…? Is something like that possible with Cerberus…?
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Validate dictionary values - python - Stack Overflow
But my dic has some rules: haver 3 keys: 'name', 'pass' and 'rule'. 'name' & 'pass' values should né strings and have more...
Read more >Restrictions on Dictionary Keys and Values - Real Python
Duplicate keys are not allowed. A dictionary maps each key to a corresponding value, so it doesn't make sense to map a particular...
Read more >Check if a Key (or Value) Exists in a Dictionary (5 Easy Ways)
Learn how to use Python to check if a key (or a value) exists in a dictionary in a safe way, using the...
Read more >Collections - Robot Framework
Documentation. Returns values of the given dictionary as a list. Uses Get Dictionary Keys to get keys and then returns corresponding values. By ......
Read more >Python - Access Dictionary Items - W3Schools
You can access the items of a dictionary by referring to its key name, ... The list of the values is a view...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yep,
keyschema
did the trick:Thanks! 😃
And by the way - I think that renaming
keyschema
tovalueschema
is a good idea! 👍and you may have a look at #83.