Skip to content

Instantly share code, notes, and snippets.

@richzw
Created February 1, 2016 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richzw/46215594763863ba5520 to your computer and use it in GitHub Desktop.
Save richzw/46215594763863ba5520 to your computer and use it in GitHub Desktop.
var peopleSchema = new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 3,
maxlength: 25
},
age: Number
});
peopleSchema.statics.testValidate = function(person) {
return new Promise((res, rej) => {
const pObj = new this(person);
pObj.validate(err => {
if (err) {
return rej(err);
} else
return res('Success');
});
});
}
var People = mongoose.model('People', peopleSchema);
function insertData() {
var p = new People({
name: '',
age: 23
});
People.testValidate(p)
.then(data => {
console.log('OK', data);
})
.catch( err => {
console.error('FAILED:',err)
})
.finally(() => mongoose.connection.close())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment