Repository navigation
Expand file tree
/
Copy pathValidators.js
More file actions
59 lines (48 loc) · 1.37 KB
/
Copy pathValidators.js
File metadata and controls
59 lines (48 loc) · 1.37 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var enforce = require("enforce");
var validators = {
required : enforce.required,
notEmptyString : enforce.notEmptyString,
rangeNumber : enforce.ranges.number,
rangeLength : enforce.ranges.length,
insideList : enforce.lists.inside,
outsideList : enforce.lists.outside,
password : enforce.security.password,
patterns : enforce.patterns
};
/**
* Check if a value is the same as a value
* of another property (useful for password
* checking).
**/
validators.equalToProperty = function (name, msg) {
return function (v, next, ctx) {
if (v == this[name]) return next();
return next(msg || 'not-equal-to-property');
};
};
/**
* Check if a property is unique in the collection.
* This can take a while because a query has to be
* made against the Model, but if you use this
* always you should not have not unique values
* on this property so this should not worry you.
**/
validators.unique = function (msg) {
return function (v, next, ctx) {
var query = {};
query[ctx.property] = v;
ctx.model.find(query, function (err, records) {
if (err) {
return next();
}
if (!records || records.length === 0) {
return next();
}
if (records.length == 1 && records[0][ctx.model.id] === this[ctx.model.id]) {
return next();
}
return next(msg || 'not-unique');
}.bind(this));
};
};
module.exports = validators;