0

I am learning how to use sequelize, so far it is a great ORM but I am stuck with the custom validation messages.

I am using the sequelize-cli library to handle migration and I found an issue, custom validation messages don't work if you use the sequelize-cli to create tables, I tried the sequelize.sync method to create the tables and it worked.

Code

This is how I create a field with a custom validation message

Wallet.js

  userId: {
    type: DataTypes.UUID,
      unique: {
        name: 'Wallets_userId_unique',
        msg: 'This user already have a wallet'
      }
   },

WalletMigration.js (Not actual migration file name)

  userId: {
    allowNull: false,
    type: Sequelize.UUID,
    unique: true,
  },

When I tried to create a Wallet with the same userId, I get a Validation error but I should get This user already have a wallet.

The message I am getting is the default message provided by the database because I used unique: true in the migration, If I remove that option the model validation doesn't work.

I want to know what can I do to change this behavior or maybe I am missing something?

  • Fwir the cli does not refer to the model for errors, the one defined in the model are used when using model methods Like create update – Shivam Jul 06 '17 at 15:32
  • @Shivam Yes, I tried this: `Wallter.create({userId: 'xyz123'})` twice and the second time I got `Validation Error` and I should get `ready have a wallet`. – Jose Osorio Jul 07 '17 at 13:15

0 Answers0