Sequelize: Validating Models

Mike Frey
InstructorMike Frey
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 5 years ago

The Node.js ORM, Sequelize, has a powerful system for validating both models and individual columns. This video covers a handful of the more than 30 built-in validators, as well as writing custom validators from scratch.

[00:00] I've got equalize setup with an author model.

[00:03] After the author's table has been synced with the database, I'll create a new record in the author's table and log a success message if the record was saved or an error if something goes wrong.

[00:14] Sequelize column definitions support a validate property which tells the ORM how to validate the column's value. Sequelize has over 30 built in validators, including regular expression validators, null and empty validators, type validators for arrays and numbers, range validators for numbers and dates, and a wide variety of string validators. Let's see a couple of these in action.

[00:37] My author model has an email column, luckily one of the built in validators is email will work perfectly here.

[00:44] When an invalid email is provided, an error is printed, but if the email is valid, the record creation works just fine.

[00:58] More than one validator can be added to each column, so if I want all authors to use a bar.com email address, I could use the contains validator to do that. Once again, when the email address is valid, a success message is printed and when invalid, an error is printed.

[01:13] The error message printed for the contains validator isn't really specific enough for my needs, so I'll customize it by setting the contains property to an object with two properties, args set to @bar.com and msg, or message, set to a string containing my custom message.

[01:33] Now when the column does invalidate, my custom message is given instead of the generic one.

[01:46] Sequelize also supports custom column validators. I'd like to add a validator to the birth day column that'll make sure it's in the valid range for the given birth month. I'll start by adding max and min validators to both birth day and birth month, then add a custom validator that I'll call max day.

[02:04] Custom validators are just functions with one argument, value, which is the value to validate. They execute with the model scope, so other column values, like birth month, can be read with dot notation.

[02:16] My validator just checks an array of day counts using the month specified in the birth month field. If the birth day specified by value is greater than the number pulled from the array, it'll throw an error, which is the desired behavior.

[02:32] Specifying a birth month of four for April and a birth day of 31 now causes a validation error, because there are only 30 days in April. Setting the birth day to 30 allows it to validate.

[02:46] Columns aren't the only thing that can have custom validators, models can have them as well. I don't want my authors to be completely anonymous, so on the third argument to the model define property, I'll add a not anonymous validator to the validate object.

[03:02] Like the custom column validators, the custom model validator is also just a function that executes under the model's context, but this one takes no arguments. If first name, last name, and email are all null, the validator will throw an error. If one or more of them is not null, the record is created successfully.

egghead
egghead
~ 42 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today