Skip to content
Advertisement

Sequelize: Cannot set a new unique constraint message

I am trying to setup a validation message for unique constraint violation for the fields username and email. However, whenever an already taken username is entered, it shows the message defined for the email property, but shows the input for the username property and also says that this input is for the username property. How would I fix this? Here is my code:

module.exports = function (sequelize, DataTypes) {
    var users = sequelize.define('users', {
        full_name: {
            type: DataTypes.STRING,
            allowNull: false,
            validate: {
                len: {
                    args: [5, 50],
                    msg: 'Your full name may be 5 to 50 characters only.'
                }
            }
        },
        email: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: {
                msg: 'This email is already taken.'
            },
            validate: {
                isEmail: {
                    msg: 'Email address must be valid.'
                }
            }
        },
        username: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: {
                msg: 'This username is already taken.'
            },
            validate: {
                len: {
                    args: [5, 50],
                    msg: 'Your username may be 5 to 50 characters only.'
                }
            }
        },
        password: {
            type: DataTypes.STRING,
            allowNull: false,
            validate: {
                len: {
                    args: [5, 72],
                    msg: 'Your password may be 5 to 72 characters only.'
                }
            }
        },
        rank: {
            type: DataTypes.INTEGER,
            allowNull: false,
            validate: {
                isInt: true
            }
        }
    }, {
        hooks: {
            beforeValidate: function (user, options) {
                if (typeof user.email === 'string') {
                    user.email = user.email.toLowerCase();
                }

                if (typeof user.username === 'string') {
                    user.username = user.username.toLowerCase();
                }
            }
        }
    });

    return users;
};

This is the output I get from input:

{
  "name": "SequelizeUniqueConstraintError",
  "message": "Validation error",
  "errors": [
    {
      "message": "This email is already taken.",
      "type": "unique violation",
      "path": "username",
      "value": "hassan"
    }
  ],
  "fields": {
    "username": "hassan"
  }
}

So as you can see, it says it is the username that is not unique, but uses the message defined for the email property.

Advertisement

Answer

This is not possible because it is a schema-validator, and not a general validator that falls inside the validate: { } object. The only work around is to include defaultValue: '' and then have a notNull object in the validate object. Otherwise, just removing allowNull disables most validate: {} checks.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement