Home > Article > Backend Development > Validatorian
Validations are a method of ensuring that our databases only receive the type of information that is appropriate for each attribute. After all, we wouldn't want a surprise type of data to find it's way into our code and cause unexpected behavior. Fortunately, SQLAlchemy has a package that makes validations quick and easy!
Let's look at some simple examples. Imagine we have a simple model, Sandwich. Here we have already initialized our database and are importing it from a configuration file.
from config import db class Sandwich(db.Model): __tablename__ = 'sandwiches' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String) description = db.Column(db.String) price = db.Column(db.Float)
If we want to add validations to any of these attributes, we'll need to import the validations package first.
from sqlalchemy.orm import validates
And then, write our function with the '@validates' decorator inside the model.
@validates('name') def validate_name(self, key, value): if not value: raise ValueError('Name cannot be empty.') if type(value) != str: raise ValueError('Name must be a string.') return value
So what's going on here? Let's break it down. The @validates is a decorator that lets our ORM know to pass any values received with the key of 'name' through our validation function before adding them to the database. The value we return is what is finally given to our database. The "key" argument is the key that is being evaluated, in this case 'name', the value is the value of that key, so the actual name(hopefully in text) that we are trying to add. So here we are checking to make sure that the name attribute passed in is not empty, and that is in in fact a string. If not, we raise an error.
We can also run multiple attributes through the same decorator by adding them to it's arguments.
@validates('name', 'description') def validate_text(self, key, value): if not value: raise ValueError(f'{key} cannot be empty.') if type(value) != str: raise ValueError(f'{key} must be a string.') return value
This function validates our name and description attributes, but we won't usually have the same validations to perform on different attributes. Depending on how different and how many validations we have we can do it a couple of different ways. We can run a separate validator for our other attributes, let's add a length validation for our description and price validations too:
@validates('name') def validate_name(self, key, value): if not value: raise ValueError('Name cannot be empty.') if type(value) != str: raise ValueError('Name must be a string.') return value @validates('description') def validate_description(self, key, value): if not value: raise ValueError('Description cannot be empty.') if type(value) != str: raise ValueError('Description must be a string.') if not 10 <p>Or, we can keep the same validator for both and use the key argument passed in to adjust which validations are run for each attribute.<br> </p> <pre class="brush:php;toolbar:false"> @validates('name', 'description', 'price') def validate(self, key, value): if key != 'price: if not value: raise ValueError(f'{key} cannot be empty.') if type(value) != str: raise ValueError(f'{key} must be string.') if key == 'description': if not 10 <p>Hmm, this is a little messy, let's refactor into 2 separate validators.<br> </p> <pre class="brush:php;toolbar:false"> @validates('name', 'description') def validate_text(self, key, value): if not value: raise ValueError(f'{key} cannot be empty.') if type(value) != str: raise ValueError(f'{key} must be string.') if key == 'description': if not 10 <p>That's better! Here's our completed model:<br> </p> <pre class="brush:php;toolbar:false">from sqlalchemy.orm import validates from config import db class Sandwich(db.Model): __tablename__ = 'sandwiches' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String) description = db.Column(db.String) price = db.Column(db.Float) @validates('name', 'description') def validate_text(self, key, value): if not value: raise ValueError(f'{key} cannot be empty.') if type(value) != str: raise ValueError(f'{key} must be string.') if key == 'description': if not 10 <p>That's it! Validations are one easy tool to make sure your databases stay right and proper.</p>
The above is the detailed content of Validatorian. For more information, please follow other related articles on the PHP Chinese website!