sequelize: customizable allowNull error messages

something like this:

  var User = sequelize.define('User', {
    'email': {
      type: DataTypes.STRING,
      unique: true,
      allowNull: { args: false, msg: 'Email is required.' },
      validate: { isEmail: { msg: 'Invalid email.' } },
    },

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 10
  • Comments: 65 (21 by maintainers)

Most upvoted comments

Can we reopen this? As of 4.2.0, @sushantdhiman 's solution does not work.

      firstName: {
        type: Sequelize.STRING,
        allowNull: false,
        validate: {
          notNull: { msg: 'null is not valid' },
        },
      },

leads to

Validation error: Warning "notNull" validation has been deprecated in favor of Schema based "allowNull"
    at Promise.all.then (/.../sequelize/lib/instance-validator.js:77:15)

if firstName value is not null.

Thanks @timscott. The workaround worked as expected:

    name: {
        type: Sequelize.STRING(50),
        allowNull: false,
        defaultValue: '',
        validate: {
            notEmpty: {
                msg: 'The name is required.'
            }
        }
    }

How does an ORM have a 3+ year old issue for customizing the validation error message when a required field is missing? This shouldn’t be labeled as a feature request…

Building off what @boulosda provided above, you can monkeypatch this from the bottom of models/index.js 😭

/**
 * Monkey patch issue causing deprecation warning when customizing allowNull validation error
 *
 * See https://github.com/sequelize/sequelize/issues/1500
 */
Sequelize.Validator.notNull = function (item) {
    return !this.isNull(item);
};

Getting this error

{
 "message": 'Warning "notNull" validation has been deprecated in favor of Schema based "allowNull"',
}

So what’s the proper way to set custom message on notNull ?

My workaround right now is

        lastname: {
            type: DataTypes.STRING,
            defaultValue: '',
            validate: {
                notEmpty: { msg: 'Last Name is required' }
            }
        }

var User = sequelize.define('User', {
    email: {
      type: DataTypes.STRING,
      unique: true,
      allowNull: { args: false, msg: 'Email is required.' }, // not work
      validate: { isEmail: { msg: 'Invalid email.' } },
    },

// modify
email:{
  type:Data.Types.STRING,
 unique:true,
 allowNull:false,
 validate:{
   notNull:{
      msg:'this is custom allowNull error message'
   }
 }
}

So any workaround to set custom validate error message when it’s null? The @timscott solution didn’t work for me

It looks like there’s isn’t a way at the moment, possibly an oversight of deprecating notNull?

https://github.com/sequelize/sequelize/blob/d00f75df89a2e94178740d7893ce2418e0db5f61/lib/instance-validator.js#L325-L327

@PauloLira this has already been implemented, you need to use >=5.0.0-beta.9 for the deprecated message to go away. See this comment above.

The fix we are using replaces the notNull function in validator-extras with, validator.notNull = function(item){ return !validator.isNull(item); };

@samkelleher I just realize that even in master this message shows up. Just need send the field with value and the message appears. I saw that in master, file validator-extras.js, that we have this piece of code: ` // Deprecate this. validator.notNull = function() { throw new Error(‘Warning “notNull” validation has been deprecated in favor of Schema based “allowNull”’); };

In some situations this function is been called and cause this.

`