Skip to content

onetop21/cerberus-kind

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cerberus-kind

Help to select a schema by "kind" key on cerberus.

Upload Python Package

Installation

$ pip install cerberus-kind

How to Use

from cerberus_kind import Validator

schema = {
    'item_default': {
        'schema': {
            'data': {
                'type': 'string', 
                'default': 'Hello World'
            }
        },
        'default': {}
    },
    'item_selector': {
        'selector': {
            'kind_string': {
                'data': {
                    'type': 'string',
                    'default': 'Hello Selector'
                }
            },
            'kind_integer': {
                'data': {
                    'type': 'integer',
                    'default': 12345
                }
            },
            'kind_list': {
                'data': {
                    'type': 'list',
                    'schema': {
                        'type': 'string',
                        'schema': {
                            'type': 'string'
                        },
                        'default': ['A', 'B', 'C']
                    }
                }
            }
        }
    }
}

document = {
    'item_selector': {
        'kind': 'Kind_String'
    }
}

v = Validator(schema)
if v.validate(document):
    print("[Verified]")
    pprint(v.normalized(document), sort_dicts=False)
else:
    print("[Errors]", v.errors)
    sys.exit(1)

# RESULT
'''
[Verified]
{'item_selector': {'kind': 'Kind_String', 'data': 'Hello Selector'},
 'item_default': {'data': 'Hello World'}}
'''

# Support sort items by schema.
schema = {
    ...
    'schema': {
        'order': 1,
        ...
    }
    ...
}
...
v.normalized_by_order(document)

# Support selector schema on root.
schema = {
    '__root__': {
        'selector': {
            ...
        }
    }
}

About

Help to select a schema by "kind" key on cerberus.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages