Feeling Validated with Validations

Hello, here I will be explaining validations the way I see them through example. I will be explaining validations in terms of python and creating/using a database. Validations the way I see them are the bouncers of a club named “database”. In this analogy, you’re the boss, you tell the bouncers who are allowed into database and what to look for in potential guests or what requirements they must meet. So validations in the same way will allow us or users to enter data in to the information but only if it meets our requirements. So lets commence with an example. Lets say we have a database table named “RunningLogs” and in this table it has columns of “temperature_fahrenheit”, “time_of_day”, and “mile_time” to show the amount of time it took for the user to run a mile.
So for instance when looking at these columns we can imagine what kind of data we would want. We would want temperature to probably display a number thats only 1 to 2 characters long, we’re not expecting for any of these runners to be running in 1000 degree temperature after all. We would probably not want users to leave the temperature empty spaced or add word as opposed to a number. If the user could enter whatever data they wanted too our data would probably look very inconsistent. That’s where our validation comes in. Lets start our example off with importing “validates”.
from sqlalchemy.orm import validates
Now we would typically want our validation to live in the model that we want to validate. So for our example as mentioned earlier our model is named “RunningLogs” and within this model we will define our validation. So within our code we will start with our decorator and within its parenthesis our property that we are validating.
@validates("temperature_fahrenheit")
Now right underneath it, we will define our validation like so.
@validates("temperature_fahrenheit")
def validate_temp(self, key, temperature):
Now lets break this down first like mentioned earlier we are defining it and naming convention is typically “validate_{insert column name}” but for lack of typing too much “temp” feels just fine here. Next in our parenthesis we pass in “self” because this is an instance method, so we always have to pass in self in instance methods. After self we pass in “key” and key will always be key, because it refers to the key in the string passed in @validates, not the value of the key but the key itself. Then we pass “temperature”, which is looking at the value that we want to validate so the value of “temperature_fahrenheit”. So now we have a validates that is checking a specific value from a specific key thanks to the decorator and the arguments that we passed. Next we give it the restrictions that we want it to look for in the value given.
@validates("temperature_fahrenheit")
def validate_temp(self, key, temperature):
if temperature.len > 2:
raise ValueError: Temp must be 1 to 2 characters long
return temperature
So now lets see what we’re saying here. We are saying if the value of “temperature_fahrenheit” which is “temperature” is more than 2 characters in length then we want to raise a value error. The message here is just something to give a clear message on what the validation is looking for. Then of course if this is not the case and we do have a “temperature” that meets this validation then we just return the value.
Now lets say that for some reason we would want to have our “time of day” to also have the same restrictions then we would simply pass that into the same validation like so.
@validates("temperature_fahrenheit", "time_of_day")
def validate_temp(self, key, temperature):
if temperature.len > 2:
raise ValueError: Temp must be 1 to 2 characters long
return temperature
Which will then validate both columns. That Is how I use and see validations when building a back end using python. Thank you for reading and happy coding!
Subscribe to my newsletter
Read articles from Adrian Garza directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
