@awesomeness-js/utils 1.1.7 → 1.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/utils/clean.js +227 -121
package/package.json
CHANGED
package/src/utils/clean.js
CHANGED
|
@@ -9,7 +9,22 @@ import cleanString from '../clean/string.js';
|
|
|
9
9
|
import cleanTimestamp from '../clean/timestamp.js';
|
|
10
10
|
import cleanUUID from '../clean/uuid.js';
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
const knownTypesToClean = [
|
|
13
|
+
'boolean',
|
|
14
|
+
'integer',
|
|
15
|
+
'number',
|
|
16
|
+
'string',
|
|
17
|
+
'timestamp',
|
|
18
|
+
'uuid',
|
|
19
|
+
'object',
|
|
20
|
+
'array'
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
function cleanArray(arr, schema = {}, {
|
|
24
|
+
testMode = false,
|
|
25
|
+
allOrNothing = false,
|
|
26
|
+
path = ''
|
|
27
|
+
} = {}){
|
|
13
28
|
|
|
14
29
|
try {
|
|
15
30
|
|
|
@@ -25,97 +40,153 @@ function cleanArray(arr, schema = {}, testMode = false){
|
|
|
25
40
|
validateSchema(schema);
|
|
26
41
|
|
|
27
42
|
const cleanArrayItems = [];
|
|
28
|
-
|
|
43
|
+
const errors = {};
|
|
44
|
+
|
|
29
45
|
const supposedToBeType = schema.items.type;
|
|
30
46
|
|
|
31
47
|
arr.forEach( (item, key) => {
|
|
48
|
+
|
|
49
|
+
let cleanedItem;
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
|
|
53
|
+
const itemType = thingType(item);
|
|
32
54
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
let cleanedItem = item;
|
|
47
|
-
|
|
48
|
-
if(supposedToBeType === 'boolean'){
|
|
55
|
+
if(itemType !== supposedToBeType){
|
|
56
|
+
|
|
57
|
+
throw {
|
|
58
|
+
message: 'type invalid',
|
|
59
|
+
itemType,
|
|
60
|
+
supposedToBeType
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
cleanedItem = item;
|
|
49
66
|
|
|
50
|
-
|
|
67
|
+
if(!knownTypesToClean.includes(supposedToBeType)){
|
|
51
68
|
|
|
52
|
-
|
|
69
|
+
throw {
|
|
70
|
+
message: 'Unknown type to clean in schema',
|
|
71
|
+
supposedToBeType
|
|
72
|
+
};
|
|
53
73
|
|
|
54
|
-
|
|
74
|
+
}
|
|
55
75
|
|
|
56
|
-
cleanedItem = cleanInteger(item);
|
|
57
76
|
|
|
58
|
-
|
|
77
|
+
if(testMode){ console.log(`cleaning ${supposedToBeType}`, item, schema.items); }
|
|
59
78
|
|
|
60
|
-
if(supposedToBeType === 'number'){
|
|
61
79
|
|
|
62
|
-
|
|
80
|
+
if(supposedToBeType === 'boolean'){
|
|
63
81
|
|
|
64
|
-
|
|
82
|
+
cleanedItem = cleanBoolean(item);
|
|
65
83
|
|
|
66
|
-
|
|
84
|
+
}
|
|
67
85
|
|
|
68
|
-
|
|
86
|
+
if(supposedToBeType === 'integer'){
|
|
69
87
|
|
|
70
|
-
|
|
88
|
+
cleanedItem = cleanInteger(item);
|
|
71
89
|
|
|
72
|
-
|
|
90
|
+
}
|
|
73
91
|
|
|
74
|
-
|
|
92
|
+
if(supposedToBeType === 'number'){
|
|
75
93
|
|
|
76
|
-
|
|
94
|
+
cleanedItem = cleanNumber(item);
|
|
77
95
|
|
|
78
|
-
|
|
96
|
+
}
|
|
79
97
|
|
|
80
|
-
|
|
98
|
+
if(supposedToBeType === 'string'){
|
|
81
99
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if(supposedToBeType === 'array'){
|
|
85
|
-
|
|
86
|
-
cleanedItem = cleanArray(item, schema.items);
|
|
87
|
-
|
|
88
|
-
}
|
|
100
|
+
cleanedItem = cleanString(item);
|
|
89
101
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
cleanedItem = cleanObject(item, schema.items);
|
|
93
|
-
|
|
94
|
-
}
|
|
102
|
+
}
|
|
95
103
|
|
|
96
|
-
|
|
104
|
+
if(supposedToBeType === 'timestamp'){
|
|
97
105
|
|
|
98
|
-
|
|
106
|
+
cleanedItem = cleanTimestamp(item);
|
|
99
107
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if(supposedToBeType === 'uuid'){
|
|
111
|
+
|
|
112
|
+
cleanedItem = cleanUUID(item);
|
|
113
|
+
|
|
114
|
+
}
|
|
105
115
|
|
|
106
|
-
|
|
116
|
+
if(supposedToBeType === 'array'){
|
|
117
|
+
|
|
118
|
+
cleanedItem = cleanArray(item, schema.items, {
|
|
119
|
+
testMode,
|
|
120
|
+
allOrNothing,
|
|
121
|
+
path: path ? `${path}.${key}` : key
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
}
|
|
107
125
|
|
|
108
|
-
|
|
126
|
+
if(supposedToBeType === 'object'){
|
|
127
|
+
|
|
128
|
+
cleanedItem = cleanObject(item, schema.items, {
|
|
129
|
+
testMode,
|
|
130
|
+
allOrNothing,
|
|
131
|
+
path: path ? `${path}.${key}` : key
|
|
132
|
+
});
|
|
109
133
|
|
|
110
134
|
}
|
|
111
|
-
|
|
112
|
-
}
|
|
113
135
|
|
|
136
|
+
if(cleanedItem === null){
|
|
137
|
+
|
|
138
|
+
if(schema.required === true){
|
|
139
|
+
|
|
140
|
+
throw {
|
|
141
|
+
message: 'required item is null',
|
|
142
|
+
item,
|
|
143
|
+
key
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
} else {
|
|
147
|
+
|
|
148
|
+
return; // skip this item if it's not required and is null
|
|
149
|
+
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
} catch(err){
|
|
156
|
+
|
|
157
|
+
if(allOrNothing){
|
|
158
|
+
|
|
159
|
+
throw err;
|
|
160
|
+
|
|
161
|
+
} else {
|
|
162
|
+
|
|
163
|
+
const errorPath = path ? `${path}[${key}]` : `[${key}]`;
|
|
164
|
+
|
|
165
|
+
errors[errorPath] = {
|
|
166
|
+
error: err,
|
|
167
|
+
value: item,
|
|
168
|
+
requirements: schema.items
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return; // skip this item if there's an error
|
|
174
|
+
|
|
175
|
+
}
|
|
114
176
|
|
|
115
177
|
cleanArrayItems.push(cleanedItem);
|
|
116
178
|
|
|
117
179
|
});
|
|
118
|
-
|
|
180
|
+
|
|
181
|
+
if(Object.keys(errors).length > 0){
|
|
182
|
+
|
|
183
|
+
throw {
|
|
184
|
+
message: 'Array not clean',
|
|
185
|
+
errors
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
}
|
|
189
|
+
|
|
119
190
|
if(cleanArrayItems.length === 0){
|
|
120
191
|
|
|
121
192
|
throw {
|
|
@@ -146,7 +217,11 @@ function cleanArray(arr, schema = {}, testMode = false){
|
|
|
146
217
|
}
|
|
147
218
|
|
|
148
219
|
|
|
149
|
-
function cleanObject(obj, schema,
|
|
220
|
+
function cleanObject(obj, schema, {
|
|
221
|
+
testMode = false,
|
|
222
|
+
allOrNothing = false,
|
|
223
|
+
path = ''
|
|
224
|
+
} = {}){
|
|
150
225
|
|
|
151
226
|
validateSchema(schema);
|
|
152
227
|
|
|
@@ -184,111 +259,142 @@ function cleanObject(obj, schema, testMode = false){
|
|
|
184
259
|
}
|
|
185
260
|
|
|
186
261
|
const cleanObj = {};
|
|
262
|
+
const errors = {};
|
|
187
263
|
|
|
188
264
|
// Iterate over the schema keys
|
|
189
265
|
each(obj, (value, key) => {
|
|
190
266
|
|
|
191
|
-
|
|
192
|
-
const supposedToBeType = schema.properties[key].type;
|
|
267
|
+
let cleanedValue;
|
|
193
268
|
|
|
194
|
-
|
|
195
|
-
console.log(`cleaning ${key}`,{
|
|
196
|
-
valType,
|
|
197
|
-
supposedToBeType,
|
|
198
|
-
value,
|
|
199
|
-
});
|
|
200
|
-
}
|
|
269
|
+
try {
|
|
201
270
|
|
|
202
|
-
|
|
271
|
+
const valType = thingType(value);
|
|
272
|
+
const supposedToBeType = schema.properties[key].type;
|
|
203
273
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
274
|
+
if(testMode){
|
|
275
|
+
console.log(`cleaning ${key}`,{
|
|
276
|
+
valType,
|
|
277
|
+
supposedToBeType,
|
|
278
|
+
value,
|
|
279
|
+
});
|
|
280
|
+
}
|
|
210
281
|
|
|
211
|
-
|
|
282
|
+
if(valType !== supposedToBeType){
|
|
212
283
|
|
|
213
|
-
|
|
284
|
+
throw {
|
|
285
|
+
message: 'type invalid',
|
|
286
|
+
valType,
|
|
287
|
+
supposedToBeType,
|
|
288
|
+
key
|
|
289
|
+
};
|
|
214
290
|
|
|
215
|
-
|
|
216
|
-
'boolean',
|
|
217
|
-
'integer',
|
|
218
|
-
'number',
|
|
219
|
-
'string',
|
|
220
|
-
'timestamp',
|
|
221
|
-
'uuid',
|
|
222
|
-
'object',
|
|
223
|
-
'array'
|
|
224
|
-
];
|
|
291
|
+
}
|
|
225
292
|
|
|
226
|
-
if(!knownTypesToClean.includes(supposedToBeType)){
|
|
227
293
|
|
|
228
|
-
|
|
229
|
-
message: 'Unknown type to clean in schema',
|
|
230
|
-
supposedToBeType,
|
|
231
|
-
key
|
|
232
|
-
};
|
|
294
|
+
if(!knownTypesToClean.includes(supposedToBeType)){
|
|
233
295
|
|
|
234
|
-
|
|
296
|
+
throw {
|
|
297
|
+
message: 'Unknown type to clean in schema',
|
|
298
|
+
supposedToBeType,
|
|
299
|
+
key
|
|
300
|
+
};
|
|
235
301
|
|
|
236
|
-
|
|
237
|
-
if(testMode){ console.log('cleaning boolean', value); }
|
|
302
|
+
}
|
|
238
303
|
|
|
304
|
+
|
|
305
|
+
if(testMode){ console.log(`cleaning ${supposedToBeType}`, value, schema.properties[key]); }
|
|
239
306
|
|
|
240
|
-
if(supposedToBeType === 'boolean'){
|
|
241
307
|
|
|
242
|
-
|
|
308
|
+
if(supposedToBeType === 'boolean'){
|
|
243
309
|
|
|
244
|
-
|
|
310
|
+
cleanedValue = cleanBoolean(value, schema.properties[key]);
|
|
245
311
|
|
|
246
|
-
|
|
312
|
+
}
|
|
247
313
|
|
|
248
|
-
|
|
314
|
+
if(supposedToBeType === 'integer'){
|
|
249
315
|
|
|
250
|
-
|
|
316
|
+
cleanedValue = cleanInteger(value, schema.properties[key]);
|
|
251
317
|
|
|
252
|
-
|
|
318
|
+
}
|
|
253
319
|
|
|
254
|
-
|
|
320
|
+
if(supposedToBeType === 'number'){
|
|
255
321
|
|
|
256
|
-
|
|
322
|
+
cleanedValue = cleanNumber(value, schema.properties[key]);
|
|
257
323
|
|
|
258
|
-
|
|
324
|
+
}
|
|
259
325
|
|
|
260
|
-
|
|
326
|
+
if(supposedToBeType === 'string'){
|
|
261
327
|
|
|
262
|
-
|
|
328
|
+
cleanedValue = cleanString(value, schema.properties[key]);
|
|
263
329
|
|
|
264
|
-
|
|
330
|
+
}
|
|
265
331
|
|
|
266
|
-
|
|
332
|
+
if(supposedToBeType === 'timestamp'){
|
|
267
333
|
|
|
268
|
-
|
|
334
|
+
cleanedValue = cleanTimestamp(value, schema.properties[key]);
|
|
269
335
|
|
|
270
|
-
|
|
336
|
+
}
|
|
271
337
|
|
|
272
|
-
|
|
338
|
+
if(supposedToBeType === 'uuid'){
|
|
273
339
|
|
|
274
|
-
|
|
340
|
+
cleanedValue = cleanUUID(value, schema.properties[key]);
|
|
275
341
|
|
|
276
|
-
|
|
342
|
+
}
|
|
277
343
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
344
|
+
if(supposedToBeType === 'object'){
|
|
345
|
+
|
|
346
|
+
cleanedValue = cleanObject(value, schema.properties[key], {
|
|
347
|
+
testMode,
|
|
348
|
+
allOrNothing,
|
|
349
|
+
path: path ? `${path}.${key}` : key
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if(supposedToBeType === 'array'){
|
|
355
|
+
|
|
356
|
+
cleanedValue = cleanArray(value, schema.properties[key], {
|
|
357
|
+
testMode,
|
|
358
|
+
allOrNothing,
|
|
359
|
+
path: path ? `${path}.${key}` : key
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
} catch (err) {
|
|
365
|
+
|
|
366
|
+
if(allOrNothing){
|
|
367
|
+
|
|
368
|
+
throw err;
|
|
369
|
+
|
|
370
|
+
} else {
|
|
371
|
+
|
|
372
|
+
const errorPath = path ? `${path}.${key}` : key;
|
|
373
|
+
|
|
374
|
+
errors[errorPath] = {
|
|
375
|
+
error: err,
|
|
376
|
+
value: value,
|
|
377
|
+
requirements: schema.properties[key]
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
}
|
|
281
381
|
|
|
282
|
-
if(supposedToBeType === 'array'){
|
|
283
382
|
|
|
284
|
-
cleanedValue = cleanArray(value, schema.properties[key]);
|
|
285
|
-
|
|
286
383
|
}
|
|
287
384
|
|
|
288
385
|
cleanObj[key] = cleanedValue;
|
|
289
386
|
|
|
290
387
|
});
|
|
291
388
|
|
|
389
|
+
if(Object.keys(errors).length > 0){
|
|
390
|
+
|
|
391
|
+
throw {
|
|
392
|
+
message: 'Object not clean',
|
|
393
|
+
errors
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
}
|
|
397
|
+
|
|
292
398
|
return cleanObj;
|
|
293
399
|
|
|
294
400
|
}
|