There Will Always Be More Opportunities

I was late in 2015. This is when I started blogging. Typing behind a keyboard and hitting publish. I thought the whole world was watching. In a way, it was like that. Although, I wasn’t thinking…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Validating Python Data with Cerberus

Applying a set of validation rules to your Python dictionary data

If you find yourself programming in Python, you’ll almost certainly find yourself working with dictionaries. Dictionaries consist of key-value pairs, where a key is the name attached to some data, and a value is the data itself. A value may be an integer, a string, or a more complex type like a list or a nested dictionary.

Dictionaries deliver relatively quick data access times. By directly referencing a key, rather than iterating over a list of values, you’ll often retrieve data faster using a dictionary compared to its list counterpart.

With dictionaries coming in all shapes and sizes, how do we validate them to make sure our data looks precisely as we would expect? How to we know we have all the keys that we require? What if a key for an integer actually referenced a string, or a key for an email address was actually storing a phone number?

Only in the past couple of weeks I have started experimenting with Cerberus on Python 3.7. Whilst Cerberus isn’t thoroughly tested on 3.7, I’ve had a perfectly painless experience working with this library thus far. I’m keen to deliver a short walk-through on how it works.

Imagine we’re receiving API requests which contain new customer details. When we receive a new payload from the request, we need to validate this before we proceed to instantiate the new customer.

Understanding precisely how our data should look is critical to defining a schema: without knowing what is considered to be valid data, it’s impossible to write the validation rules.

We’re off to a good start, but maybe we want to restrict the range of IDs that we’re prepared to accept? Let’s impose bounds on the ID to be, say, between 0 and 100. Furthermore, let’s mark the ID as a required attribute, since every customer must have an ID. We can append these additional constraints to our schema using the min, max and required keys.

The customer’s name requires similar restrictions. Since a string is an iterable type, where we can traverse it character-by-character, we apply length bounds using Cerberus’ minlength and maxlength properties.

For Customer accounts, we’re working with a list of values, as opposed to an individual number or string. Therefore we need to add a little further configuration to this attribute’s schema. For both lists and dictionaries, Cerberus accepts a schema key.

The schema key varies slightly between lists and dictionaries. For lists, this property should be treated precisely like constraining an individual attribute. For our customer accounts, we’ll accept a list of integers between 0 and 100.

As for dictionaries, we should consider schema as a collection of its own key-value pairs. For customer contact details, we’ll want to store both a phone number and an email address. Let’s implement each contact method as their own key phone and email, respectively.

If the Validator finds any unsatisfied constraints, they will be reported in the Validator’s errors property. In such circumstances the script terminates early and reports these errors. In the happy path scenario — if there’s no errors to report — the script proceeds to instantiate the new customer.

If this post was helpful, please click the clap 👏 button below a few times to show your support! ⬇

Add a comment

Related posts:

Montel Williams Talking Cannabis

A cannabis lifestyle and interview show, “Let’s Be Blunt with Montel” explores the intersection of cannabis, activism, health, and wellness. Hosted by Montel Williams, the podcast works to bridge the…

How To Make Money As A Football Tipster

Football is one of the most popular sports in the world, and betting on it is big business. But how do you make money as a football tipster? There are a few different routes you can take, and the…

Critiquing my First UI design

I started pushing pixels on Figma a year ago and continued learning on Coursera, which was my first free UX course. This was a project I took on concerning the digitalization of blood banks in order…