Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
xor-xor opened this issue May 20, 2015 · 5 comments

Comments

@xor-xor
Copy link

xor-xor commented May 20, 2015

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..?

@funkyfuture
Copy link
Member

my guess would be, that this helps you: https://cerberus.readthedocs.org/en/latest/#schema-dict

@xor-xor
Copy link
Author

xor-xor commented May 20, 2015

Yes, I'm aware of schema (dict). But from what I understand, it doesn't allow arbitrary names of keys (i.e., not declared in schema). And because of that, I need to state bbb and ccc explicitly, whereas otherwise I'd just go with a single, general rule covering both of them.

@funkyfuture
Copy link
Member

sorry, confused it: look up the paragraph about keyschema.

@funkyfuture
Copy link
Member

and you may have a look at #83.

@xor-xor
Copy link
Author

xor-xor commented 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! 👍

@xor-xor xor-xor closed this as completed May 21, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants