Skip to content

Instantly share code, notes, and snippets.

@swaj
Created November 9, 2011 01:41
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save swaj/1350041 to your computer and use it in GitHub Desktop.
Save swaj/1350041 to your computer and use it in GitHub Desktop.
A mongoose model that takes advantage of the handy invalidate method to run multiple validations
var mongoose = require('./db-connect'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId,
uuid = require('node-uuid'),
Validator = require('validator').Validator,
val = new Validator(),
bcrypt = require('bcrypt');
Validator.prototype.error = function(msg) { return false; };
var CustomerSchema = new Schema({
firstName: { type: String, required: true },
lastName: { type: String, required: true },
emailAddress: { type: String, lowercase: true, unique: true },
passwordHash: { type: String, required: true },
active: { type: Boolean, default: true, required: true },
lastUpdatedBy: { type: String, required: true, default: 'System' },
lastUpdatedDate: { type: Date, required: true, default: new Date() },
passwordResetToken: String,
passwordResetExpiration: Date,
emailConfirmationToken: { type: String, default: uuid() },
lastIPAddress: String,
role: { type: String, enum: ['user', 'admin'], default: 'user', required: true },
_parent: Schema.ObjectId
});
CustomerSchema.virtual('password')
.get(function() {
return this._password;
})
.set(function(value) {
this._password = value;
var salt = bcrypt.gen_salt_sync(12);
this.passwordHash = bcrypt.encrypt_sync(value, salt);
});
CustomerSchema.virtual('passwordConfirmation')
.get(function() {
return this._passwordConfirmation;
})
.set(function(value) {
this._passwordConfirmation = value;
});
CustomerSchema.path('passwordHash').validate(function(v) {
if (this._password || this._passwordConfirmation) {
if (!val.check(this._password).min(6)) {
this.invalidate('password', 'must be at least 6 characters.');
}
if (this._password !== this._passwordConfirmation) {
this.invalidate('passwordConfirmation', 'must match confirmation.');
}
}
if (this.isNew && !this._password) {
this.invalidate('password', 'required');
}
}, null);
CustomerSchema.path('firstName').validate(function(v) {
if (!val.check(v).max(100)) {
this.invalidate('firstName', 'must be less than 100 characters');
}
}, null);
CustomerSchema.path('lastName').validate(function(v) {
if (!val.check(v).max(100)) {
this.invalidate('lastName', 'must be less than 100 characters');
}
}, null);
CustomerSchema.path('emailAddress').validate(function(v) {
if (!val.check(v).isEmail()) {
this.invalidate('emailAddress', 'must be a valid email address');
}
}, null);
module.exports = mongoose.model('Customer', CustomerSchema);
@codepunkt
Copy link

i'd replace

  emailConfirmationToken: { type: String, default: uuid() }, 

with

  emailConfirmationToken: { type: String, default: function() { return uuid(); } }, 

so your customer documents receive different uuids on initialization.

@meeDamian
Copy link

@gonsfx: or just:

emailConfirmationToken: { type: String, default: uuid }, 

@panhachea
Copy link

i don't know .I need only change password word .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment