[Python] How to validate structure (or schema) of dictionary in Python?

Discussão em 'Python' iniciado por Stack, Fevereiro 16, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dictionary with config info:

    my_conf = {
    'version': 1,

    'info': {
    'conf_one': 2.5,
    'conf_two': 'foo',
    'conf_three': False,
    'optional_conf': 'bar'
    }
    }


    I want to check if the dictionary follows the structure I need.

    I'm looking for something like this:

    conf_structure = {
    'version': int,

    'info': {
    'conf_one': float,
    'conf_two': str,
    'conf_three': bool
    }
    }

    is_ok = check_structure(conf_structure, my_conf)


    Is there any solution done to this problem or any library that could make implementing check_structure more easy?

    Continue reading...

Compartilhe esta Página