Enforce Data Validation on Models in Sequelize

7 min read·Jan 1, 2025

In addition to constraints, Sequelize provides a list of built-in model validators implemented by the validator library, that allow you check and validate the data format of columns through the validate property:

{
  <column>: {
    type: <type>,
    validate: {
      <validator>: <value>,
      ...
    }
  }
}

Note: These validators are only executed upon CREATE and UPDATE operations.

Validate numbers

To check numbers, you can use:

  • isNumeric: true: to only allow numbers.
  • isInt: true: to check for valid integers.
  • isFloat: true: to check for floating point numbers.
  • isDecimal: true: to check for any numbers.
  • max: <number>: to only allow values <= number.
  • min: <number>: to only allow values >= number.

Example

For example:

{
  latitude: {
    type: DataTypes.FLOAT,
    validate: {
      isDecimal: true,
      min: -90,
      max: 90
    }
  }
}

Validate strings

To check strings, you can use:

  • isAlpha: true: to only allow letters.
  • isAlphanumeric: true: to only allow letters and digits.
  • isLowercase: true: to check for lowercase letters.
  • isUppercase: true: to check for uppercase letters.
  • notEmpty: true: to not allow empty strings.
  • contains: <string>: to check for a substring.
  • notContains: <string>: to not allow a substring.
  • len: [<number>,<number>]: to only allow length between values.

Example

For example:

{
  pseudo: {
    type: DataTypes.STRING(20),
    validate: {
      isAlphanumeric: true,
      isLowercase: true,
      notEmpty: true,
      len: [6, 20]
    }
  }
}

Validate regular expressions

To match a field against a regular expression, you can use:

  • is: /pattern/: to check if the field matches a pattern.
  • not: /pattern/: to check if the field doesn't match a pattern.

Example

For example:

{
  name: {
    type: DataTypes.STRING(50),
    validate: {
      is: /^([a-zA-Z-]+\s)?$/i
    }
  }
}

Validate common expressions

To check common expressions, such as email addresses or URLs, you can use:

  • isEmail: true: to check email addresses.
  • isUrl: true: to check URLs.
  • isIP: true: to check IPv4 or IPv6 addresses.

Example

For example:

{
  email: {
    type: DataTypes.STRING(320),
    validate: {
      isEmail: true
    }
  }
}

Validate dates

To check dates, you can use:

  • isDate: true: to only allow date strings.
  • isAfter: "2011-11-05": to only allow date strings after a specific date.
  • isBefore: "2011-11-05": to only allow date strings before a specific date.

Example

For example:

{
  start_date: {
    type: DataTypes.DATEONLY,
    validate: {
      isAfter: '2025-01-01'
    }
  }
}

Validate exact values

To check exact values, you can use:

  • isIn: [['foo', 'bar']]: to check if the value is one of these.
  • notIn: [['foo', 'bar']]: to check the value is not one of these.
  • equals: 'value': to only allow a specific value.

Example

For example:

{
  genre: {
    type: DataTypes.STRING,
    validate: {
      isIn: [['Action', 'Comedy', 'Drama', 'Horror']]
    }
  }
}

Enjoying the courses?

I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:

Support BackendBrewery
Enforce Data Validation on Models With Sequelize | Backend Brewery