-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
290 lines (249 loc) · 8.67 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
let validator = {};
class ForkObject {
constructor() {
}
min(val) {
this._min = val;
return this;
}
max(val) {
this._max = val;
return this;
}
limit(val) {
this._limit = val;
return this;
}
required() {
this._required = true;
return this;
}
validate(value, path) {
let _errors = {};
if (this.isArray && value && _type(value) != "array")
return { status: false, _errors: [new ForkValidationError(this.key,path,`${this.key} must be an "array"`, "ERR_ARRAY" )] };
if (this._min && value.length < this._min)
return { status: false, _errors: [new ForkValidationError(this.key,path, `${this.key} must be of minimum length ${this._min}`, "ERR_MIN_LENGTH")] };
if (this._max && value.length > this._max)
return { status: false, _errors: [new ForkValidationError(this.key,path, `${this.key} must be of maximum length ${this._max}`, "ERR_MAX_LENGTH")] };
if (this._required && !value)
return { status: false, _errors: [new ForkValidationError(this.key,path,`${this.key} is Required`, "ERR_REQUIRED")] };
if (!value) {
}
let _result;
let showIndex = Array.isArray(value) ? true : false;
value = Array.isArray(value) ? value : [value];
for (let i = 0; i < value.length; i++) {
let valInstance = value[i];
Object.keys(this.keys).forEach(key => {
let _path = path+"."+(showIndex?i+".":"")+key;
if (_type(this.keys[key]) == "ForkInstance") {
_result = this.keys[key].validate(valInstance && valInstance[key] ? valInstance[key] : "", key, _path);
}
if (_type(this.keys[key]) == "ForkObject") {
this.keys[key].key = key;
_result = this.keys[key].validate(valInstance && valInstance[key] ? valInstance[key] : "", _path);
}
if (!_result.status) {
if (showIndex) {
if (!_errors[i])
_errors[i] = {};
_errors[i][key] = _result._errors;
}
else
_errors[key] = _result._errors;
}
});
}
let status = Object.keys(_errors).length > 0 ? false : true;
return { status: status, _errors: _errors };
}
}
class ForkInstance {
constructor() {
this.validations = [];
}
string() {
this.validations.push("string");
return this;
}
number() {
this.validations.push("number");
return this;
}
email() {
this.validations.push("email");
return this;
}
regex(_regex){
this._regex = _regex;
return this;
}
getAll() {
return this.validations;
}
validate(value, key, path) {
let keyValidators = this.getAll();
let _errors = [];
if(this.isArray && !Array.isArray(value))
_errors.push({ key: key,path:path, message: `${key} must be an Array`, errorCode: "ERR_ARRAY" });
else{
// if(this.isArray){
if (this._min && value.length < this._min)
return { status: false, _errors: [new ForkValidationError(key,path, `${key} must be of minimum length ${this._min}`, "ERR_MIN_LENGTH")] };
if (this._max && value.length > this._max)
return { status: false, _errors: [new ForkValidationError(key,path, `${key} must be of maximum length ${this._max}`, "ERR_MAX_LENGTH")] };
// }
value = Array.isArray(value)?value:[value];
for(let i =0;i< value.length; i++){
let valInstance = value[i];
let _path = path+(this.isArray?"."+i:"");
if (keyValidators.indexOf("required") > -1 && !valInstance) {
_errors.push({ key: key,path:_path, message: `${key} is required`, errorCode: "ERR_REQUIRED" });
}
else {
if (this._in && this._in.indexOf(valInstance) == -1)
_errors.push(new ForkValidationError(key,_path, `${key} must be one of [ ${this._in.join(" , ")} ]`, "ERR_VALUES"));
if (valInstance && keyValidators.indexOf("string") > -1 && typeof valInstance != "string") {
_errors.push(new ForkValidationError(key,_path, `${key} must be a String`, "ERR_STRING" ));
}
if (valInstance && keyValidators.indexOf("number") > -1 && typeof valInstance != "number") {
_errors.push(new ForkValidationError(key,_path, `${key} must be a Number`, "ERR_NUMBER" ));
}
if (valInstance && keyValidators.indexOf("email") > -1 && !/^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$/.test(valInstance)) {
_errors.push(new ForkValidationError(key,_path, `${key} must be an Email`, "ERR_EMAIL" ));
}
if (valInstance && this._regex && !this._regex.test(valInstance)) {
_errors.push(new ForkValidationError(key,_path, `${key} failed validation against ${this._regex}`, "ERR_REGEX" ));
}
}
}
}
return (_errors.length == 0) ? { status: true } : { status: false, _errors: _errors };
}
min(val) {
this._min = val;
return this;
}
in(val) {
this._in = val;
return this;
}
max(val) {
this._max = val;
return this;
}
limit(val) {
this._limit = val;
return this;
}
required() {
this._required = true;
return this;
}
}
class ForkValidationError{
constructor(key, path,message,errorCode){
this.key = key;
this.path = path;
this.message = message;
this.errorCode = errorCode;
}
getErrorCode(){
return this.errorCode;
}
getMessage(){
return errorCode
}
}
validator.instance = () => {
return new ForkInstance();
}
validator.string = () => {
let _ = new ForkInstance();
_.validations.push("string");
return _;
}
validator.number = () => {
let _ = new ForkInstance();
_.validations.push("number");
return _;
}
validator.email = () => {
let _ = new ForkInstance();
_.validations.push("email");
return _;
}
validator.regex = (_regex) => {
let _ = new ForkInstance();
_.regex(_regex);
return _;
}
validator.required = () => {
let _ = new ForkInstance();
_.validations.push("r");
return _;
}
validator.object = (object) => {
let _ = new ForkObject();
_.keys = [];
Object.keys(object).forEach(key => {
_.keys[key] = object[key];
});
return _;
}
validator.array = (object) => {
if(!object)
object = new ForkInstance();
if(_type(object) == "ForkInstance"){
let _ = object;
_.isArray = true;
return _;
}
let _ = new ForkObject();
_.isArray = true;
_.keys = [];
Object.keys(object).forEach(key => {
_.keys[key] = object[key];
});
return _;
}
validator.validate = function (schema, object, callback) {
let errors = {};
Object.keys(schema).forEach(key => {
if (schema[key]) {
let type = _type(schema[key])
let _result;
switch (type) {
case "ForkInstance":
_result = schema[key].validate(object[key], key, key);
if (!_result.status)
errors[key] = _result._errors;
break;
case "ForkObject":
schema[key].key = key;
_result = schema[key].validate(object[key], key);
if (!_result.status)
errors[key] = _result._errors;
break;
}
}
});
let status = Object.keys(errors).length > 0 ? false : true;
if (callback) {
callback({ status: status, errors: errors });
}
else
return { status: status, errors: errors };
}
let _type = function (key) {
if (Array.isArray(key))
return "array";
if (key instanceof ForkInstance)
return "ForkInstance";
if (key instanceof ForkObject)
return "ForkObject";
else
return typeof key;
}
module.exports = validator;