Sequelize 允许用户键入 "null" 即使使用 allowNull: false

Sequelize is allowing user to type "null" even with allowNull: false

我有一个行程模型

var Itinerary = sequelize.define('Itinerary', {
  title: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      notEmpty: true
    }
  }
})

这可以防止空输入,但如果我输入 "null",它仍然会添加到我的数据库中。阅读文档,看起来应该可以防止这种情况发生?我做错了什么吗?

Sequelize(和数据库)将 "null" 解释为有效字符串。

itinerary.title ='null'; 

不同于:

itinerary.title = null;

如果您愿意,可以向模型添加额外的验证方法来查找此字符串 null。

var Itinerary = sequelize.define('Itinerary', {
  title: {
    type: DataTypes.STRING,
    allowNull: false,
    nullorEmpty: function() {
      if (!this.title || (this.title === 'null') {
        throw new Error('title must be set')
      }
    }
  }
})