简体   繁体   中英

Sequelize, Validate length of field with blank value

I am using Sequelize as ORM with express.

In Sequelize Model , their is field that accepts the null value. I want to validate this field by defining the length of the input with len if any value is provided.

Code:

field: {
  type: DataTypes.TEXT,
  allowNull: true,
  validate: {
    len: {
      args: [50, 200],
      msg: 'Please provide field within 50 to 200 characters.'
    }
  }
}

But, Sequelize throw error when field is empty. So, How do i allow the empty value, and just validate only when value is provided.

Have you tried this notEmpty: false ?

field: {
  type: DataTypes.TEXT,
  allowNull: true,
  validate: {
    notEmpty: false,
    len: {
      args: [50, 200],
      msg: 'Please provide field within 50 to 200 characters.'
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM