DEV Community

amdiamond107
amdiamond107

Posted on

Constraints & Validations in Python Flask

As a relative newbie in the coding world, there are checkpoints throughout the full-stack developer learning journey where your emotions rapidly shift from feeling overwhelmed and perplexed to even-keeled and as if though everything's beginning to click.

For me, learning the intricacies of Python Flask in the early-going felt daunting, but I reached that familiar checkpoint I'd encountered in my previous coding language journeys where things began to seemingly click in an instant, and that's when I broached the topics of constraints & validations. These tools began to contextualize some of the outcomes we were aiming to reach via our data manipulations in a more real-world setting, which would in turn help me apply my newfound knowledge.

By definition...

  • Constraints: are rules enforced on the data columns of a table, and they ensure that only appropriate data is saved to the database.

  • Validations: are automatic checks to ensure that data entered is sensible and feasible.

Using a constraint, we can delineate certain data points from others, so that, for instance, undesirable pieces of data can be deflected away from ever reaching our database. Our database is vital, it needs protection, and these constraints can act like password-protected vaults, wherein the example pictured below, the password is essentially defined by the "CheckConstraint" usage...

Image description

Here we have a column CheckConstraint which makes sure we cannot add a "birth_year" that is greater than 2023.

Using SQLAlchemy constraints enables us to control input at a database level, and allows us to use any valid SQL and numeric comparisons to validate input.

  • And, with validations, we're able customize and implement specific stipulations/rules that can confine the eventual web/app user's interaction with our website in a fashion we so desire them to(i.e. how they create, read, update, or delete new and/or pre-existing information displayed). Thus, validations play a similar, protective role for our database, as they can invalidate undesirable data inputs to steer them away from entering our database (and, in turn, serve to validate and confirm desirable data meets our requirements before entering our database). Take the pictured example below:

Image description

In this situation, the user of our website/app will receive an error popup message if they enter an email address for themselves in our email address entry form without an "@" included within (i.e. "@" as in the @ in my email address: amdiamond107@gmail.com). Albeit, we could use a better, and more indicative error message than the one that appears above -- "Failed simple email validation" which in my opinion reads as a little robotic, and perhaps even a tad condescending towards our web/app user...

To better help our potentially less-than-tech-savvy user understand what's amiss with their previously attempted data input, we could instead program an error message that reads something more constructive and user-friendly "not a valid email address. Must include an @ symbol i.e. "john.doe@aol.com".

Resources:

Top comments (0)