-
-
Save Jxck/1721671 to your computer and use it in GitHub Desktop.
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 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