Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set model validation with sequelize in nodejs?

I’m new at nodejs. I have used sequelize for nodejs orm. But I can not set validate to an attribute;

models/farmer.js

'use strict';
module.exports = function(sequelize, DataTypes) {
  var Farmer = sequelize.define('Farmer', {
    username:{
            type: DataTypes.STRING,
            allowNull: false,
    },
    address: DataTypes.STRING,
    email: {
        type: DataTypes.STRING,
        validate: {
           isEmail: true
        }
    },
    phone:{
            type: DataTypes.STRING,
            allowNull: false,
    },
  }, {
    classMethods: {
      associate: function(models) {
          Farmer.hasMany(models.Task);
        // associations can be defined here
      }
    }
  });
  return Farmer;
};

Error:

Possibly unhandled SequelizeValidationError: Validation error at /Users/esmrkbr/Desktop/nodejs/sequelize-express-demo/node_modules/sequelize/lib/instance-validator.js:149:14 at tryCatch1 (/Users/esmrkbr/Desktop/nodejs/sequelize-express-demo/node_modules/sequelize/node_modules/sequelize-bluebird/js/main/util.js:43:21) at Promise$_callHandler [as _callHandler] (/Users/esmrkbr/Desktop/nodejs/sequelize-express-demo/node_modules/sequelize/node_modules/sequelize-bluebird/js/main/promise.js:639:13) at Promise$_settlePromiseFromHandler [as _settlePromiseFromHandler] (/Users/esmrkbr/Desktop/nodejs/sequelize-express-demo/node_modules/sequelize/node_modules/sequelize-bluebird/js/main/promise.js:653:18) at Promise$_settlePromiseAt [as _settlePromiseAt] (/Users/esmrkbr/Desktop/nodejs/sequelize-express-demo/node_modules/sequelize/node_modules/sequelize-bluebird/js/main/promise.js:817:14) at Promise$_settlePromises [as _settlePromises] (/Users/esmrkbr/Desktop/nodejs/sequelize-express-demo/node_modules/sequelize/node_modules/sequelize-bluebird/js/main/promise.js:951:14) at Async$_consumeFunctionBuffer [as _consumeFunctionBuffer] (/Users/esmrkbr/Desktop/nodejs/sequelize-express-demo/node_modules/sequelize/node_modules/sequelize-bluebird/js/main/async.js:75:12) at Async$consumeFunctionBuffer (/Users/esmrkbr/Desktop/nodejs/sequelize-express-demo/node_modules/sequelize/node_modules/sequelize-bluebird/js/main/async.js:38:14) at doNTCallback0 (node.js:419:9) at process._tickCallback (node.js:348:13)

How can i set validate model?

like image 437
esmrkbr Avatar asked Sep 02 '25 10:09

esmrkbr


1 Answers

This :

Possibly unhandled SequelizeValidationError: Validation error at

means that your sequelize validation works. It checks validation and when validation fails throw error (SequelizeValidationError).

But this :

Possibly unhandled

means that you do nothing with this error. You should catch this error and do something (for example send response with appropriate error code).

For example when you create new 'Farmer' your code could look like:

Farmer.create({
  //your data
}).then(function(){
  //do something when Farmer is created
}).catch(function(err){
  //do something when you get error
  //you could check if this is validation error or other error
});

Sometimes it is good idea to validate errors outside sequelize (but don't remove validation in sequelize, let both validations work together) (I wrote some arguments here).

like image 52
Krzysztof Sztompka Avatar answered Sep 04 '25 07:09

Krzysztof Sztompka