Skip to content

Instantly share code, notes, and snippets.

@Jxck
Forked from aheckmann/invalidate.js
Created February 2, 2012 05:26
Show Gist options
  • Save Jxck/1721671 to your computer and use it in GitHub Desktop.
Save Jxck/1721671 to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose');
mongoose.connect('localhost', 'testing_invalidate');
var schema = new mongoose.Schema({
name: { type: String, required: true },
pass: { type: String, required: true },
});
schema.methods.setPassword = function setPassword (pwd, confirm) {
if (pwd === confirm) {
this.pass = pwd;
return true;
}
this.invalidate('password', new Error('Password mismatch'));
return false;
}
var A = mongoose.model('A', schema);
mongoose.connection.on('open', function () {
var a = new A({ name: 'jxck' });
a.setPassword('xxx', 'xxx');
a.save(function (err) {
if (err) console.log(err);
// { message: 'Validation failed',
// name: 'ValidationError',
// errors:
// { password: [Error: Password mismatch],
// pass:
// { message: 'Validator "required" failed for path pass',
// name: 'ValidatorError',
// path: 'pass',
// type: 'required' } } }
mongoose.connection.db.dropDatabase(function () {
mongoose.connection.close();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment