constraints sequelize code example

Example: Sequelize type email

sequelize.define('foo', {
  bar: {
    type: DataTypes.STRING,
    validate: {
      
      isEmail: true,            // checks for email format ([email protected])
      
      

      // Examples of custom validators:
      isEven(value) {
        if (parseInt(value) % 2 !== 0) {
          throw new Error('Only even values are allowed!');
        }
      }
      isGreaterThanOtherField(value) {
        if (parseInt(value) <= parseInt(this.otherField)) {
          throw new Error('Bar must be greater than otherField.');
        }
      }
    }
  }
});

Tags:

Misc Example