Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Created May 22, 2013 18:37
Show Gist options
  • Save aheckmann/5629845 to your computer and use it in GitHub Desktop.
Save aheckmann/5629845 to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var assert = require('assert')
mongoose.connect('localhost', dbname);
mongoose.connection.on('error', function () {
console.error('connection error', arguments);
});
var schema = new Schema({
name: String
});
// custom validation during set
schema.path('name').set(function (v) {
if (Array.isArray(v)) {
this.invalidate('name', 'Cannot be an array', '['+v+']');
return this.name;
}
return v;
})
var A = mongoose.model('A', schema);
mongoose.connection.on('open', function () {
var a = new A({ name: ['setorder'] });
a.save(done) // Validator "Cannot be an array" failed for path name with value `[setorder]`
});
function done (err) {
if (err) console.error(err.stack);
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