-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
32 lines (29 loc) · 979 Bytes
/
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
"use strict";
var Promise = require('bluebird');
var Validator = require('validator');
var _ = require('lodash');
var validators = Promise.promisifyAll(Validator, {
suffix: 'Async',
filter(name, func, target, passesDefaultFilter) {
return passesDefaultFilter && (_.startsWith(name, 'is') || _.includes(['contains', 'equals', 'matches']));
},
promisifier(originalMethod, defaultPromisifier) {
return function(errorMsg) {
let args = Array.from(arguments).slice(1);
let self = this;
return new Promise(function(resolve, reject) {
let result = originalMethod.apply(self, args);
return (result) ? resolve(result) : reject(new Error(errorMsg));
});
};
}
});
validators.custom = function(errorMsg, func) {
let args = Array.from(arguments).slice(2);
let self = this;
return new Promise((resolve, reject) => {
let result = func.apply(self, args);
return (result) ? resolve(result) : reject(new Error(errorMsg));
});
}
module.exports = validators;