-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
385 lines (359 loc) · 13.5 KB
/
lib.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
Example data structure for a validation object
validationObj = {
validationInput: {
validationProp: {
isValid: function() {
return true;
},
message: function() {
return 'is required';
}
}
},
username: {
required: {
isValid: function(username) {
if(typeof(username) === 'undefined')
return false;
if(username === null)
return false;
if(username.trim().length <= 0)
return false;
return true;
},
message: 'Username is required'
}
}
};
*/
/*
This object has methods for working with a validation
data structure as seen above.
*/
// Reduces an array of booleans with the and
// operation.
// ie. item[0] && item[1] && item[n] ...
// Returns true if all items are true
function functionalAnd(result, current) {
if(result) {
return current;
} else {
return result;
}
}
// Reduces an array of objects with a key and message
// property into a single object
function reduceToObject(result, current) {
result[current.key] = current.message;
return result;
}
// Reduces an array of keys to the resulting object
// ie. result[nestedKey[0]][nestedKey[1]][nestedKey[n]] ...
// Initial value should be the target object
function nestedKeyReduce(result, nestedKey) {
if(typeof(result) !== 'undefined') {
if(typeof(result[nestedKey]) !== 'undefined') {
return result[nestedKey];
}
}
return undefined;
}
// Returns a property from a target object using
// a key. If the key is nested it will resolve
// the nesting before returning the object
function resolveNestedObject(targetObj, key) {
let nestedKeys = key.split('.');
let isNestedKey = nestedKeys.length > 1;
let resultObj;
if(isNestedKey) {
resultObj = nestedKeys.reduce(nestedKeyReduce, targetObj);
} else {
resultObj = targetObj[key];
}
return resultObj;
}
// Returns true if the provided item is not
// null or undefined
function isNotUndefined(item) {
return (typeof(item) !== 'undefined' && item !== null);
}
// Validates parameters
function validateParams(targetObj, validationObj) {
if(typeof(targetObj) !== 'object') {
throw new Error('Target object must be an object');
}
if(typeof(validationObj) !== 'object') {
throw new Error('Validation object must be an object');
}
}
// Validates inputKey parameter
function validateInputKey(validationObj, inputKey) {
if(typeof(inputKey) !== 'string') {
throw new Error('Validation input must be a property string');
} else if(typeof(validationObj[inputKey]) === 'undefined') {
throw new Error('Validation input property must exist on validation object');
}
}
// Returns true if all validations are valid
export function isValid(targetObj, validationObj) {
validateParams(targetObj, validationObj);
let validationKeys = Object.keys(validationObj);
let toReturn = false;
if(targetObj instanceof Array) {
toReturn = targetObj.map(function(object) {
return isValid(object, validationObj);
});
} else {
toReturn = validationKeys.reduce(function(result, validationKey) {
if(result) {
let validationInputObj = validationObj[validationKey];
let validationInputKeys = Object.keys(validationInputObj);
return validationInputKeys.reduce(function(result, inputKey) {
if(result) {
let valdationProp = validationInputObj[inputKey];
let resultObj = resolveNestedObject(targetObj, validationKey);
if(valdationProp.isValid instanceof Function) {
return valdationProp.isValid(resultObj);
} else {
return true;
}
} else {
return result;
}
}, true);
} else {
return result;
}
}, true);
}
return toReturn;
}
// Returns a promise resolving to true if all validations are valid
// and resolving to false if a validation failed
export function isValidAsync(targetObj, validationObj) {
try {
validateParams(targetObj, validationObj);
} catch(err) {
return Promise.reject(err);
}
let validationKeys = Object.keys(validationObj);
let toReturn;
if(targetObj instanceof Array) {
toReturn = Promise.all(targetObj.map(function(object) {
return isValidAsync(object, validationObj);
}));
} else {
if(validationKeys.length > 0) {
let validationPromises = validationKeys.map(function(validationKey) {
let validationInputObj = validationObj[validationKey];
let validationInputKeys = Object.keys(validationInputObj);
let validationInputPromises = validationInputKeys.map(function(inputKey) {
let valdationProp = validationInputObj[inputKey];
let resultObj = resolveNestedObject(targetObj, validationKey);
if(valdationProp.isValid instanceof Function) {
return Promise.resolve(valdationProp.isValid(resultObj));
} else {
return Promise.resolve(true);
}
});
return Promise.all(validationInputPromises).then(function(validationInputResults) {
return validationInputResults.reduce(functionalAnd);
});
}, true);
toReturn = Promise.all(validationPromises).then(function(validationResults) {
return validationResults.reduce(functionalAnd);
});
} else {
// If there are no constraints every object is valid
toReturn = Promise.resolve(true);
}
}
return toReturn;
}
// Returns an array of validation messages for each validation prop
// that fails for a single validation input
export function messages(targetObj, validationObj, validationInput) {
validateParams(targetObj, validationObj);
validateInputKey(validationObj, validationInput);
let keys = Object.keys(validationObj[validationInput]);
let toReturn;
if(targetObj instanceof Array) {
toReturn = targetObj.map(function(object) {
return messages(object, validationObj, validationInput);
});
} else {
toReturn = keys.map(function(key) {
let validationProp = validationObj[validationInput][key];
let resultObj = resolveNestedObject(targetObj, validationInput);
let message = (validationProp.message instanceof Function ? validationProp.message(resultObj) : validationProp.message);
if(!validationProp.isValid(resultObj)) {
return {
key: key,
message: message
};
}
})
.filter(isNotUndefined)
.reduce(reduceToObject, {});
}
return toReturn;
}
// Returns a promise resolving to an array of validation messages
// for each validation prop that fails for a single validation input
export function messagesAsync(targetObj, validationObj, validationInput) {
try {
validateParams(targetObj, validationObj);
validateInputKey(validationObj, validationInput);
} catch(err) {
return Promise.reject(err);
}
let keys = Object.keys(validationObj[validationInput]);
let toReturn;
if(targetObj instanceof Array) {
toReturn = Promise.all(targetObj.map(function(object) {
return messagesAsync(object, validationObj, validationInput);
}));
} else {
let validationPromises = keys.map(function(key) {
let validationProp = validationObj[validationInput][key];
let resultObj = resolveNestedObject(targetObj, validationInput);
let message = (validationProp.message instanceof Function ? validationProp.message(resultObj) : validationProp.message);
return Promise.resolve(validationProp.isValid(resultObj))
.then(function(validationPropKey, isValid) {
if(!isValid) {
return {
key: validationPropKey,
message: message
};
}
}.bind(null, key));
});
toReturn = Promise.all(validationPromises)
.then(function(message) {
return message
.filter(isNotUndefined)
.reduce(reduceToObject, {});
});
}
return toReturn;
}
// Returns an array of objects, one object for each validation
// input, with each key on that object being the failed validation
// prop, and the value is the validation message
export function allMessages(targetObj, validationObj) {
validateParams(targetObj, validationObj);
let keys = Object.keys(validationObj);
let toReturn;
if(targetObj instanceof Array) {
toReturn = targetObj.map(function(object) {
return allMessages(object, validationObj);
});
} else {
toReturn = keys.reduce(function(result, key) {
let validationInputObj = validationObj[key];
let validationInputKeys = Object.keys(validationInputObj);
let validationResult = {};
validationInputKeys.forEach(function(inputKey) {
let validationProp = validationInputObj[inputKey];
let resultObj = resolveNestedObject(targetObj, key);
let message = (validationProp.message instanceof Function ? validationProp.message(resultObj) : validationProp.message);
if(!validationProp.isValid(resultObj)) {
validationResult[inputKey] = message;
}
});
if(Object.keys(validationResult).length > 0) {
result[key] = validationResult;
}
return result;
}, {});
}
return toReturn
}
// Returns a promise resolving to an array of objects, one object for
// each validation input, with each key on that object being the failed
// validation prop, and the value is the validation message
export function allMessagesAsync(targetObj, validationObj) {
try {
validateParams(targetObj, validationObj);
} catch(err) {
return Promise.reject(err);
}
let keys = Object.keys(validationObj);
let toReturn;
if(targetObj instanceof Array) {
toReturn = Promise.all(targetObj.map(function(object) {
return allMessagesAsync(object, validationObj);
}));
} else {
let validationPromises = keys.map(function(key) {
let validationInputObj = validationObj[key];
let validationInputKeys = Object.keys(validationInputObj);
let validationInputPromises = validationInputKeys.map(function(inputKey) {
let validationProp = validationInputObj[inputKey];
let resultObj = resolveNestedObject(targetObj, key);
let message = (validationProp.message instanceof Function ? validationProp.message(resultObj) : validationProp.message);
return Promise.resolve(validationProp.isValid(resultObj))
.then(function(isValid) {
if(!isValid) {
return {
message: message,
key: inputKey
};
}
});
});
return Promise.all(validationInputPromises).then(function(validationInputResults) {
let resultObj = validationInputResults.reduce(function(result, current) {
result[current.key] = current.message;
return result;
}, {});
resultObj.inputKey = key;
return resultObj;
});
});
toReturn = Promise.all(validationPromises)
.then(function(allMessages) {
return allMessages.reduce(function(result, current) {
result[current.inputKey] = current;
delete current.inputKey;
return result;
}, {});
});
}
return toReturn;
}
// Returns an object that contains all the attributes of
// the target object that are being validated
export function cleanAttributes(targetObj, validationObj) {
validateParams(targetObj, validationObj);
let keys = Object.keys(validationObj);
let toReturn;
if(targetObj instanceof Array) {
toReturn = targetObj.map(function(object) {
return cleanAttributes(object, validationObj);
});
} else {
toReturn = {};
keys.forEach(function(key) {
let isNestedKey = (key.indexOf('.') > -1);
if(!isNestedKey) {
toReturn[key] = targetObj[key];
} else {
let rootKey = key.split('.')[0];
toReturn[rootKey] = targetObj[rootKey];
}
});
}
return toReturn;
}
// Default export object with all functions as properties
export default {
isValid: isValid,
isValidAsync: isValidAsync,
messages: messages,
messagesAsync: messagesAsync,
allMessages: allMessages,
allMessagesAsync: allMessagesAsync,
cleanAttributes: cleanAttributes
};