Sequelize is not throwing error for invalid inputs in findAll and findOne methods

Viewed 180

I have built a nodejs express app that uses sequelize as ORM to handle MySQL transactions. There are no issues in validations for create, update and delete requests however if I try to fetch data using the findOne and findAll method sequelize seems to escape to validate data.

I have tried writing implicit validations for sequelize using validate but it still doesn't work.

This is the code :


  getTradeOrder: async (params) => {
    try {

// for params: {amount: '500jha'}
// tradeorder is fetched as if the amount's value was 500 

let data = await TradeOrder.findOne({ where: params }); 
// returns data:{id:2, user:"gfa7834djksfyui32", amount:500, ...}

      return { status: 200, error: null, data: data };
    } catch (err) {
      console.log(err);
      return {
        status: 400,
        error: err.message,
      };
    }
  },


  listAllTradeOrders: async (params) => {
    try {

// for params: {amount: '500jha'}
// tradeorders are fetched as if the amount's value was 500 

      let tradeOrders = await TradeOrder.findAll({
        where: params,
      });
 
// returns tradeorders:[{id:2, user:"gfa7834djksfyui32", amount:500, ...}, ...]
   
   return { status: 200, error: null, data: tradeOrders };
    } catch (err) {
      console.log(err);
      return {
        status: 400,
        error: err.message,
      };
    }
  },
};

This is the model :

const tradeOrderSchema = {
  id: DataTypes.NUMBER,
  user: DataTypes.STRING,
  amount: DataTypes.NUMBER,
  paymentMode: DataTypes.STRING,
  units: DataTypes.NUMBER,
  bankName: DataTypes.STRING
 ...

Ideally, sequelize should capture the invalid value and throw an error for invalid value of amount. If anyone knows why it is not happening or how to add implicit validation except for validate please let me know.

0 Answers
Related