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.

question: validate a list

See original GitHub issue

Maybe I’m just approaching this all wrong, but I was wondering if the following is possible:

item = {
    'appid': {'type': 'integer', 'min': 1, 'required': True},
    'rank': {'type': 'integer', 'min': 1, 'required': True},
    'weight': {'type': 'float', 'required': True},
}

schema = {
    'type': 'list',
    'minlength': 1,
    'required': True,
    'schema': {
        'type': 'dict', 'schema': item
    }
}

I want to use the above to assert that a list I have, contains at least one element, and that each element of the list adheres to the item schema.

However, the above does not work since schema can only be applied to a dict.

So, as I understand it, I would have to wrap my list in a dict (e.g. {'list': list}), and then change my schema definition to:

schema = {
    'list': {
        'type': 'list',
        'minlength': 1,
        'required': True,
        'schema': {
            'type': 'dict', 'schema': item
        }
    }
}

I would really like to avoid this redundant nesting though but still use cerberus for the list validation, not “plain native” python (e.g. if isinstance(list, collections.Iterable) and len(list) > 0:).

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

19reactions
funkyfuturecommented, May 4, 2016
0reactions
dynnamittcommented, Feb 16, 2022

Did you end up with wrapping in object or not?

  doc_ = dict(wrapper=doc) if isinstance(doc, list) else doc
Read more comments on GitHub >

github_iconTop Results From Across the Web

python - validate list of lists with a unique and common element
I need to validate this list and it will be valid if only one element is repeated in all the lists and the...
Read more >
Validate text answer against a list of words - LimeSurvey forums
Hi guys, I have a kill list of words that I'm trying to use to validate a particular short text answer. The question...
Read more >
Advanced Concepts — Questionary 1.9.0 documentation
Many of the prompts support a validate argument, which allows the answer to be validated before being ... A question to select an...
Read more >
Solved For this discussion, you are given a list of | Chegg.com
For this discussion, you are given a list of questions related to form validation. Simply answer the questions in your own words, using...
Read more >
Take in 10 numbers, validate each input, append then in a list ...
Please be sure to answer the question. Provide details and share your research! ... Asking for help, clarification, or responding to other answers ......
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