简体   繁体   中英

Is there a way to encode data before insert/update in sequelize?

All, Have questions in sequelize. We have a requirement to encode data in the model before insert/update. Below is a one of the columns in the model where some validations are being performed before inserting the data. Also, validateFields.encodeFormData is a function that uses javascript's encodeURIComponent to encode the passed string. So we wanted to know from the experts out there if this is the correct way to encode data or if there any better ways within sequelize to encode and decode?

fyi, SQL server is our db.

Thanks in advance for your help.

Name: {
  type: DataTypes.STRING,
  allowNull: false,
  validate: {
      min: {
          args: 3,
          msg: 'Name must be at least 3 characters'
      },
      max: {
          args: 40,
          msg: 'Name must start with a letter, and be at less than 40 characters.'
      },
      is: {
          args: /['~!@#$^*()_|+\=?;:",<>\{\}\[\]\\\/]/gi,
          msg: 'Invalid Characters detected. Please enter valid characters.'
      }
  },

  set:function(val) {
      var encodedVal = validateFields.encodeFormData(val);
      console.log("Encoded name : ", encodedVal);
      this.setDataValue('Name', encodedVal);
  },

  get:function() {
      var decodedVal = validateFields.decodeFormData(this.getDataValue('Name'));
      return decodedVal;
  }
},

我想你可能需要使用钩子

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