-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
124 lines (102 loc) · 3.23 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env node
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
//----------- CORE MODULES ----------//
var util = require('util');
//----------- LOCAL MODULES ----------//
var standardValidations = require('./validation');
var chain = require('./chain');
// Validator Base Class
class Validator {
// constructor
constructor(mandatoryMap) {
this.validations = {};
this.mandatoryMap = mandatoryMap || {};
this.DEFAULT_FAILURE_MSG = 'Validation Failure.';
this.addValidations(standardValidations);
}
// main function which check if data is valid or not
// response
// if valid => {valid: true}
// if not valid => {valid: false, error: 'Validation Failure.', failure_key: 'key'}
isValid(data, validationMap, mode) {
var mandatoryMap = this.mandatoryMap,
missing = [],
response = {};
// If no mode Specified or mode is not present in mandotory map
// we are not throwing any error just validating the data
if (!mode || !mandatoryMap[mode]) {
return this.validate(data, validationMap);
}
mandatoryMap[mode].forEach(param => {
if (data[param] === undefined || data[param] === null) {
missing.push(param);
}
if (data[param] && typeof data[param] === 'string' && !data[param].trim()) {
missing.push(param);
}
});
// missing parameters
if (missing.length) {
response.valid = false;
response.error = util.format('Missing mandatory parameter(s): %s', missing.join(','));
return response;
}
return this.validate(data, validationMap);
}
// handler to add custom validations
addValidations(customValidations) {
for (var key in customValidations) {
if (customValidations.hasOwnProperty(key)) {
this.validations[key] = customValidations[key];
}
}
}
// Core validate function
validate(data, validationMap) {
validationMap = validationMap || {};
var isValid = true,
response = {
valid: true
};
for (var key in data) {
var actualValue = data[key],
rules = validationMap[key];
if (rules) {
if(rules instanceof chain) {
for (var i in rules.validations) {
isValid = isValid && rules.validations[i](actualValue);
if (!isValid) {
response.valid = false;
response.error = rules.errMsg || this.DEFAULT_FAILURE_MSG;
response.invalid_key = key;
return response;
}
}
}
else {
for (var rule in rules) {
if (this.validations[rule]) {
var targetValue = rules[rule];
isValid = isValid && this.validations[rule](targetValue)(actualValue);
if (!isValid) {
response.valid = false;
response.error = this.DEFAULT_FAILURE_MSG;
response.invalid_key = key;
return response;
}
}
}
}
}
}
return response;
}
check(errMsg) {
return new chain(errMsg);
}
}
exports.default = Validator;
module.exports = exports['default'];