Sequelize Model Validation

Renzo Regio
6 min readMar 10, 2021

--

TIL 2021.02.25

Day 77: Treehouse Full Stack JavaScript Techdegree

As developers, we need to ensure that the data created and updated by users is reliable without irrelevant or duplicate entries.

We can use Sequelize’s validation and constraint capabilities.

Sequelize can run validation on a model to require specific values and define constraints to prevent incorrect, unexpected or potentially harmful data from being recorded into the database

Right now if we submit or post a new user, it will be created with null values. Because we haven’t made any validation/constraint configurations on the user model

This is our user model

As you can see, we can create a profile without any field or property in it because we haven’t set any validation or configuration

And if we get all users we will see null values set on the different fields

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

SET VALIDATIONS FOR A MODEL

Let’s start setting up validations and constraints on the user model to prevent invalid data to be added on to the database

Let’s configure that a null value is not allowed on all the fields

Using allowNull: false means that if any of these fields is null, the new user entry will not be stored and a validation error will be thrown.

Now let’s try to place a post request with an empty body {}

And we are getting an error now compared to actually creating it earlier

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

VALIDATORS AND CUSTOM ERROR MESSAGES

https://sequelize.org/master/manual/validations-and-constraints.html#validators

Note: notNull validator is only allowed with allowNull: false

If we set a blank string to each field, it would still go through.

This means we have to add another validator for this.

We can use the notEmpty validator to ensure users cannot create new accounts if they submit empty values or fields

For the email attribute we can use the isEmail validator to check the email format

And if we create a correct user

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

SET UNIQUE CONSTRAINTS

RULES FOR MORE IN-DEPTH CHECKS PERFORMED AT THE SQL LEVEL VERSUS AT THE SEQUELIZE LEVEL

For example, earlier we used the notEmpty validator to make sure the strings passed are not empty

And the isEmail validator to check the email format

If any of those validations fail, a SQL query will not be sent to the database

With constraint checks, a SQL query is performed. But if a constraint check fails, the database throws an error preventing the post or update and sequelize lets us know about it

This is how a constraint differs from sequelize validations.

A common example is checking that the email submitted is a unique email address — A user submits an entry and their email gets checked against other emails in the database to ensure that it is unique.

On the user model we can define a unique constraint on the email field by adding the unique property to the email object and setting it to true

Now back in postman, let’s try to create a user with an already existing email

Before posting

After posting

It provides this error — email must be unique

And in the log, it says SequelizeUniqueContraintError

An attempt to insert an email that already exists will throw a SequelizeUniqueConstraintError which prevents duplicate data from being inserted into the database

Like validators, we can customize the error messages provided by the unique constraints

Just set the value of unique to be an object

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

DATE AND LENGTH VALIDATORS

isDate validator

And when using the proper format, it works

len validator

For 8 characters, it works!

For more than 20 characters, the user cannot create the user!

When you set a validator to an object, you can pass values or arguments to it via the args property

--

--