DEV Community

Brandon Macer
Brandon Macer

Posted on

1

TIL: from cerberus import Validator

Today I learned about Validator, a class in the Python third-party module cerberus.

Here we go.

Let's import this bugger.

from cerberus import Validator

Dang that felt good. Obviously we would need to have installed it first so let me back up and get my pip on.

py -m pip install cerberus

Oh yeah, that's the stuff. Not sure why I call my Python py, but don't hate. Or wait, this is the internet. Proceed.

Alright so we're going, we're rolling, what's next. Instantiate that Cool Hand Luke.

>>> v = Validator()
>>> v
<cerberus.validator.Validator object at 0x7fa7df93f760>

Boom, we got the goods. Whatcha hiding in there, kitten?

>>> dir(v)
['_BareValidator__get_rule_handler', '_BareValidator__init_error_handler', '_BareValidator__init_processing', ... plus a billion more

Dang, you're not gonna make me read the documentation are you? You, sonofa...

>>> list(filter(lambda i: not i.startswith("_"), dir(v)))
['allow_unknown', 'checkers', 'clear_caches', 'coercers', 'default_setters', 'document', 'document_error_tree', 'document_path', 'error_handler', 'errors', 'ignore_none_values', 'is_child', 'mandatory_validations', 'normalization_rules', 'normalized', 'priority_validations', 'purge_readonly', 'purge_unknown', 'recent_error', 'require_all', 'root_allow_unknown', 'root_document', 'root_require_all', 'root_schema', 'rules', 'rules_set_registry', 'schema', 'schema_error_tree', 'schema_path', 'schema_registry', 'types', 'types_mapping', 'update', 'validate', 'validated', 'validation_rules']

Aha, sucka! What have we here, what have we here...

We're gonna validate some shtuff. Let's do this. I'm ready.

>>> shtuff_to_validate = {
...     "awesome": "effyeah",
...     "animal": "tiny turtle"
... }

Heck yeah I love turtles.

Let's validate that shtuff.

>>> v.validate(shtuff_to_validate)

False
False? Get that out of my face. False. Turtles are awesome.

>>> v.errors
{'animal': ['unknown field'], 'awesome': ['unknown field']}

Unknown field? Well dang, I'm sorry, let me introduce you. Field, turtle. Turtle, field.

Validator McGee likes a schema, so we'll give 'em a schemadema.

>>> schemadema = {"awesome": {}, "animal": {}}

Let's try this again.

Oh wait I gotta tell it the schema is the schema. Lemme do that right quick here.

>>> v.schema = schemadema

Here goes. Turtles, your time to shine.

>>> v.validate(shtuff_to_validate)

True

Boom. Turtles are awesome.

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️