Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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);
@gonsfx

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.

@chester1000

@gonsfx: or just:

emailConfirmationToken: { type: String, default: uuid }, 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.