Created
June 8, 2011 13:31
-
-
Save hugdubois/1014416 to your computer and use it in GitHub Desktop.
This file contains hidden or 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') | |
, 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