john
john

Reputation: 58

customize validation error null error unique error

i am using sequelize.js nodejs as backend. when there is a validation error/ unique error/ null error it is sequelize validation error. Basically i want to get error as node js error. It is displaying everything in the error. I would like to show only the path and message .

current error showing

{
"name": "SequelizeValidationError",
"message": "Validation error: Validation notEmpty failed",
"errors": [
{
  "message": "Validation notEmpty failed",
  "type": "Validation error",
  "path": "name",
  "value": {},
  "__raw": {}
  }
]
 }

i want this

{
"name":"The field cannot be empty",
"other_field":"custom error message"
 }

Upvotes: 0

Views: 854

Answers (1)

joedavis
joedavis

Reputation: 86

You can add custom error messages to validate in Sequelize. Please refer this

class User extends Model {}
    User.init({
      name: {
        type: DataTypes.STRING,
        allowNull: false,
        validate: {
          notNull: {
            msg: 'Please enter your name'
          }
        }
      }
    }, { sequelize });

Upvotes: 2

Related Questions