node.jssequelize.js

How can we add String Validation Like Enum In Nodejs and Sequelize


 type: {
      type: DataTypes.STRING,
      defaultValue: "Normal"
    }

In this type I want to add validation only the type can be "Normal" or "Installment"

I don't want to use enum so how can I do custom validation in sequelize model?


Solution

  • You can add custom validations as mentioned in sequelize docs

    You can try like:

    type: {
      type: DataTypes.STRING,
      defaultValue: "Normal",
      validate: {
        isIn: [['Normal', 'Installment']]
      }
    
    }
    

    You can make the column an enum one if your enum values wont change.