Skip to content

Instantly share code, notes, and snippets.

@kevinmeziere
Forked from hugdubois/gist:1014416
Created July 17, 2011 17:13
Show Gist options
  • Save kevinmeziere/1087811 to your computer and use it in GitHub Desktop.
Save kevinmeziere/1087811 to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose')
, Futures = require('futures')
, Schema = mongoose.Schema
, db = mongoose.connect('mongodb://localhost/mongoose-db');
var UserSchema = new Schema({
"name": {type: String},
"location": {
"latitude": {type: Number, required: true, min: -90, max: 90},
"longitude": {type: Number, required: true, min: -180, max: 180}
}
});
mongoose.model('User', UserSchema);
var User = mongoose.model('User')
, sequence = Futures.sequence();
var saveUser = function(o, cb) {
var user = new User(o);
user.save(function(err, u) {
if (err) {
console.log(err);
} else {
console.log(u);
}
cb();
});
};
sequence
.then(function(next) {
/**
* normal insert
* -------------
* { location: { longitude: 2.352, latitude: 48.867 },
* _id: 4def70666a9e10e842000001,
* name: 'hugues dubois' }
**/
saveUser({
"name": "hugues dubois",
"location": {
"latitude": 48.867,
"longitude": 2.352
}
}, next);
})
.then(function(next) {
/**
* normal insert
* -------------
* { location: { longitude: 2.352, latitude: 48.867 },
* _id: 4def70666a9e10e842000001,
* name: 'hugues dubois' }
**/
saveUser({
"name": "hugues dubois",
"location": {
"latitude": "48.867",
"longitude": "2.352"
}
}, next);
})
.then(function(next) {
/**
* normal error
* -------------
* { stack: [Getter/Setter],
* message: 'Validation failed',
* name: 'ValidationError',
* errors: { 'location.latitude': 'Validator "min" failed for path location.latitude' } }
**/
saveUser({
"name": "hugues dubois",
"location": {
"latitude": -91,
"longitude": -180
}
}, next);
})
.then(function(next) {
/**
* normal error
* -------------
* { stack: [Getter/Setter],
* message: 'Validation failed',
* name: 'ValidationError',
* errors:
* { 'location.longitude': 'Validator "required" failed for path location.longitude',
* 'location.latitude': 'Validator "required" failed for path location.latitude' } }
**/
saveUser({
"name": "hugues dubois",
"location": {
"latitude": "",
"longitude": ""
}
}, next);
})
.then(function(next) {
/**
* ERROR IN MONGOOSE THE NUMBER IS CASTED TO 0
* -------------
* { location: { longitude: 0, latitude: 0 },
* _id: 4def749a3ff4c38543000004,
* name: 'hugues dubois' }
**/
saveUser({
"name": "hugues dubois",
"location": {
"latitude": " ", /*ERROR IN MONGOOSE THIS IS CASTED TO 0*/
"longitude": " " /*ERROR IN MONGOOSE THIS IS CASTED TO 0*/
}
}, next);
})
.then(function() {
db.disconnect();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment