question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validating values of dicts with arbitrary/unknown names of keys - is it possible..?

See original GitHub issue

Having 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:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
xor-xorcommented, May 21, 2015

Yep, keyschema did the trick:

schema = {
    'aaa': {
        'type': 'dict',
        'keyschema': {
            'type': 'list',
            'schema': {
                'type': 'dict',
                'schema': {
                    'ddd': {
                        'type': 'dict',
                        'schema': {
                            'xxx': {'type': 'integer'},
                            'yyy': {'type': 'string'},
                        },
                    },
                    'eee': {
                        'type': 'dict',
                        'schema': {
                            'zzz': {'type': 'integer'},
                        },
                    }
                }
            }
        }
    }
}

Thanks! 😃

And by the way - I think that renaming keyschema to valueschema is a good idea! 👍

0reactions
funkyfuturecommented, May 20, 2015

and you may have a look at #83.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found