-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
48 lines (39 loc) · 1.37 KB
/
index.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
37
38
39
40
41
42
43
44
45
46
47
48
var Q = require('q');
module.exports = function(result, requirements) {
var def = Q.defer();
if (!result) { result = {} }
function endsWith(string, endString) {
return new RegExp(endString + '$').test(string);
}
function startsWith(string, startString) {
return new RegExp("^" + startString).test(string);
}
if (!requirements) {
def.resolve(result);
} else {
for (key in requirements) {
if (!result[key]) {
def.reject(new Error('Validate expected result to include: ' + key));
} else {
if (requirements[key].endsWith) {
if (!endsWith(result[key], requirements[key].endsWith)) {
def.reject(new Error('Invalid: ' + result[key] + ' does not end with: ' + requirements[key].endsWith))
}
}
if (requirements[key].endsWithout) {
if (endsWith(result[key], requirements[key].endsWithout)) {
def.reject(new Error('Invalid: ' + result[key] + ' ends with: ' + requirements[key].endsWithout))
}
}
if (requirements[key].startsWith) {
if (!startsWith(result[key], requirements[key].startsWith)) {
def.reject(new Error('Invalid: ' + result[key] + ' does not start with: ' + requirements[key].startsWith))
}
}
}
}
//gotten this far without invalidating
def.resolve(result)
}
return def.promise;
}