I want to save a hashed password. I'm using a setterMethod for this:
module.exports = (sequelize, DataTypes) ->
sequelize.define 'User',
# other model fields [...]
password:
type: DataTypes.STRING
validate: notEmpty: msg: 'You need to set a password.'
set: (pw) ->
salt = bcrypt.genSaltSync(10)
hash = bcrypt.hashSync(pw, salt)
@setDataValue('password', hash)
The setter runs first. An empty string password (''
) is hashed into a non-empty one (say $2a$10$pDDIGnV.r47i9YOv0Fls/euQ0yYvfyq8T1SyP9VRQsTUAqptNmxXO
).
When the validator validates, the password is not empty anymore.
How can I validate the password before the setter?
I looked into hooks but they don't mention setters either.
I'm using sequelize@2.1.3
.