Reputation: 85
I am working with Nodejs, sequelize and mysql. I have below user model with username and password required fields. I have declared some validation rules.
During my email validation function when I try to update verified field to true password field validation performs and function failed with below error.
Please anyone can guide me what I am doing wrong.
const User = sequelize.define("user", {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
email: {
type: DataTypes.STRING,
unique: true,
allowNull: false,
validate: {
isEmail: { args: true, msg: 'email format is not correct' },
notNull: { args: true, msg: 'email can\'t be empty' },
notEmpty: { args: true, msg: 'email can\'t be empty string' },
}
},
password: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notNull: { args: true, msg: 'password can\'t be empty' },
len: { args: [5, 50], msg: 'password length must be more than 5 characters' },
}
},
verified: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
token: {
type: DataTypes.STRING,
},
expiresIn: {
type: DataTypes.DATE
},
User model fields are being updated here.
const user = await User.findOne({ where: { id } });
if (!user) res.redirect('/signup');
if (user.expiresIn.getTime() > Date.now()) {
console.log('time check');
if (user.token === token) {
console.log('verified');
user.token = null;
user.verified=true,
user.expiresIn=null;
await user.save();
res.redirect('/login');
} else {
console.log('token failed');
res.redirect('/signup');
}
} else {
console.log('token expired');
await user.update({token: null, expiresIn:null});
res.redirect('/login');
}
(node:3328) UnhandledPromiseRejectionWarning: SequelizeValidationError: Validation error: password length must be more than 5 characters
at InstanceValidator._validate (/home/amir/tuts/web/myApp/node_modules/sequelize/lib/instance-validator.js:50:13)
at async InstanceValidator._validateAndRunHooks (/home/amir/tuts/web/myApp/node_modules/sequelize/lib/instance-validator.js:60:7)
at async InstanceValidator.validate (/home/amir/tuts/web/myApp/node_modules/sequelize/lib/instance-validator.js:54:12)
at async model.save (/home/amir/tuts/web/myApp/node_modules/sequelize/lib/model.js:2377:11)
at async module.exports.email_verification (/home/amir/tuts/web/myApp/controllers/authController.js:152:13)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3328) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:3328) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Upvotes: 1
Views: 519
Reputation: 85
I figure out the reason why password validation was running.
Password field in my user model I should use min validation instead of len
Revised password field validation.
password: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notNull: { args: true, msg: 'password can\'t be empty' },
min: { args: 5, msg: 'password length must be more than 5 characters' },
}
},
Upvotes: 1