简体   繁体   中英

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.

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.

 isUnique(value) { return User.findOne({where:{name:value}}) .then((name) => { if (name) { throw new Error('Validation error: name already exist'); } }) }

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