Mongoose validate fail when using set
Take care, if you use both set and validate in your schema definition, the set will run first. If it's returning with an empty string for example, the validation will not run, and thus it cannot block the document creation.
So this is BAD, the validator will NOT run, because set returns with an empty string.
var mongoose = require('mongoose');
var schema = new mongoose.Schema({
phone: {
type: String,
validate: function(_phone) {
_phone = _phone.replace('+', '');
return !!(phoneValidator(_phone, 'HUN').length);
},
set: function(_phone) {
_phone = _phone.replace('+', '');
return phoneValidator(_phone, 'HUN')[0];
}
}
}
Written by Dénes Pál
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Node
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#