Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Sequelize: Validating Models


    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.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    I've got equalize setup with an author model.

    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.

    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.

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

    When an invalid email is provided, an error is printed, but if the email is valid, the record creation works just fine.

    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.

    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.

    Now when the column does invalidate, my custom message is given instead of the generic one.

    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.

    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.

    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.

    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.

    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.

    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.