forked from Empeeric/jest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongoose_validation.js
36 lines (31 loc) · 1.15 KB
/
mongoose_validation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var _ = require("underscore"),
Class = require('sji'),
Validation = require('./validation');
var MongooseValidation = module.exports = Validation.extend({
init:function (model) {
this._super();
this.model = model;
},
_get_error:function (field, error) {
var errors = {
required:'this field is required',
min:'must be equal or greater than ' + field.options.min,
max:'must be equal or lower than ' + field.options.max,
enum:'must be one of the following ' + field.options.enum
};
return errors[error];
},
elaborate_default_errors:function (field, error) {
return _.has(field.options, error) ? this._get_error(field, error) : error;
},
elaborate_mongoose_error:function (fieldname, error) {
var field = this.model.schema.paths[fieldname];
return _.has(field.options, error.type) ? this._get_error(field, error.type) : error;
},
is_valid:function (object, callback) {
var errors = {};
_.each(this.model.schema.paths, function (field, name, paths) {
});
callback(null, errors);
}
});