Created
October 29, 2013 18:24
-
-
Save sdepold/7220001 to your computer and use it in GitHub Desktop.
virtual attributes with sequelize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Sequelize = require('sequelize') | |
, sequelize = new Sequelize('sequelize_test', 'root') | |
var User = sequelize.define('User', { | |
username: Sequelize.STRING, | |
password_hash: Sequelize.STRING | |
}, { | |
validate: { | |
password: function(next) { | |
if (((this._password || "").trim() !== '') && (this._password === this._password_confirmation)) { | |
next() | |
} else { | |
next('Password not matching!') | |
} | |
} | |
}, | |
getterMethods: { | |
password: function() { return this._password }, | |
password_confirmation: function() { return this._password_confirmation } | |
}, | |
setterMethods: { | |
password: function(v) { this._password = v }, | |
password_confirmation: function(v) { this._password_confirmation = v } | |
} | |
}) | |
User.sync({ force: true }).success(function() { | |
User | |
.create({ username: 'foo', password: 'something', password_confirmation: 'different' }) | |
.complete(function(err, user) { | |
console.log(err) | |
User | |
.create({ username: 'foo', password: 'something', password_confirmation: 'something' }) | |
.complete(function(err, user) { | |
// should be empty | |
console.log(err) | |
console.log(user.values) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment