@awesomeness-js/utils 1.1.6 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/utils/clean.js +240 -97
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@awesomeness-js/utils",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "description": "Awesomeness - Utils",
5
5
  "repository": {
6
6
  "type": "git",
@@ -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
- function cleanArray(arr, schema = {}){
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 = {}){
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
- const itemType = thingType(item);
34
-
35
-
36
- if(itemType !== supposedToBeType){
37
-
38
- throw {
39
- message: 'type invalid',
40
- itemType,
41
- supposedToBeType
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
- cleanedItem = cleanBoolean(item);
67
+ if(!knownTypesToClean.includes(supposedToBeType)){
51
68
 
52
- }
69
+ throw {
70
+ message: 'Unknown type to clean in schema',
71
+ supposedToBeType
72
+ };
53
73
 
54
- if(supposedToBeType === 'integer'){
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
- cleanedItem = cleanNumber(item);
80
+ if(supposedToBeType === 'boolean'){
63
81
 
64
- }
82
+ cleanedItem = cleanBoolean(item);
65
83
 
66
- if(supposedToBeType === 'string'){
84
+ }
67
85
 
68
- cleanedItem = cleanString(item);
86
+ if(supposedToBeType === 'integer'){
69
87
 
70
- }
88
+ cleanedItem = cleanInteger(item);
71
89
 
72
- if(supposedToBeType === 'timestamp'){
90
+ }
73
91
 
74
- cleanedItem = cleanTimestamp(item);
92
+ if(supposedToBeType === 'number'){
75
93
 
76
- }
94
+ cleanedItem = cleanNumber(item);
77
95
 
78
- if(supposedToBeType === 'uuid'){
96
+ }
79
97
 
80
- cleanedItem = cleanUUID(item);
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
- if(supposedToBeType === 'object'){
91
-
92
- cleanedItem = cleanObject(item, schema.items);
93
-
94
- }
102
+ }
95
103
 
96
- if(cleanedItem === null){
104
+ if(supposedToBeType === 'timestamp'){
97
105
 
98
- if(schema.required === true){
106
+ cleanedItem = cleanTimestamp(item);
99
107
 
100
- throw {
101
- message: 'required item is null',
102
- item,
103
- key
104
- };
108
+ }
109
+
110
+ if(supposedToBeType === 'uuid'){
111
+
112
+ cleanedItem = cleanUUID(item);
113
+
114
+ }
105
115
 
106
- } else {
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
- return; // skip this item if it's not required and is null
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 = {}){
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
 
@@ -179,79 +254,147 @@ function cleanObject(obj, schema){
179
254
 
180
255
  }
181
256
 
257
+ if(testMode){
258
+ console.log('cleanObject keysPassed:', keysPassed);
259
+ }
260
+
182
261
  const cleanObj = {};
262
+ const errors = {};
183
263
 
184
264
  // Iterate over the schema keys
185
265
  each(obj, (value, key) => {
186
266
 
187
- const valType = thingType(value);
188
- const supposedToBeType = schema.properties[key].type;
267
+ let cleanedValue;
189
268
 
190
- if(valType !== supposedToBeType){
269
+ try {
191
270
 
192
- throw {
193
- message: 'type invalid',
194
- valType,
195
- supposedToBeType,
196
- key
197
- };
271
+ const valType = thingType(value);
272
+ const supposedToBeType = schema.properties[key].type;
198
273
 
199
- }
274
+ if(testMode){
275
+ console.log(`cleaning ${key}`,{
276
+ valType,
277
+ supposedToBeType,
278
+ value,
279
+ });
280
+ }
200
281
 
201
- let cleanedValue;
282
+ if(valType !== supposedToBeType){
202
283
 
203
- if(supposedToBeType === 'boolean'){
284
+ throw {
285
+ message: 'type invalid',
286
+ valType,
287
+ supposedToBeType,
288
+ key
289
+ };
204
290
 
205
- cleanedValue = cleanBoolean(value);
291
+ }
206
292
 
207
- }
208
293
 
209
- if(supposedToBeType === 'integer'){
294
+ if(!knownTypesToClean.includes(supposedToBeType)){
210
295
 
211
- cleanedValue = cleanInteger(value);
296
+ throw {
297
+ message: 'Unknown type to clean in schema',
298
+ supposedToBeType,
299
+ key
300
+ };
212
301
 
213
- }
302
+ }
214
303
 
215
- if(supposedToBeType === 'number'){
304
+
305
+ if(testMode){ console.log(`cleaning ${supposedToBeType}`, value, schema.properties[key]); }
216
306
 
217
- cleanedValue = cleanNumber(value);
218
307
 
219
- }
308
+ if(supposedToBeType === 'boolean'){
220
309
 
221
- if(supposedToBeType === 'string'){
310
+ cleanedValue = cleanBoolean(value, schema.properties[key]);
222
311
 
223
- cleanedValue = cleanString(value);
312
+ }
224
313
 
225
- }
314
+ if(supposedToBeType === 'integer'){
226
315
 
227
- if(supposedToBeType === 'timestamp'){
316
+ cleanedValue = cleanInteger(value, schema.properties[key]);
228
317
 
229
- cleanedValue = cleanTimestamp(value);
318
+ }
230
319
 
231
- }
320
+ if(supposedToBeType === 'number'){
232
321
 
233
- if(supposedToBeType === 'uuid'){
322
+ cleanedValue = cleanNumber(value, schema.properties[key]);
234
323
 
235
- cleanedValue = cleanUUID(value);
324
+ }
236
325
 
237
- }
326
+ if(supposedToBeType === 'string'){
327
+
328
+ cleanedValue = cleanString(value, schema.properties[key]);
329
+
330
+ }
331
+
332
+ if(supposedToBeType === 'timestamp'){
333
+
334
+ cleanedValue = cleanTimestamp(value, schema.properties[key]);
335
+
336
+ }
337
+
338
+ if(supposedToBeType === 'uuid'){
339
+
340
+ cleanedValue = cleanUUID(value, schema.properties[key]);
341
+
342
+ }
343
+
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
+ }
238
381
 
239
- if(supposedToBeType === 'object'){
240
-
241
- cleanedValue = cleanObject(value, schema.properties[key]);
242
-
243
- }
244
382
 
245
- if(supposedToBeType === 'array'){
246
-
247
- cleanedValue = cleanArray(value, schema.properties[key]);
248
-
249
383
  }
250
384
 
251
385
  cleanObj[key] = cleanedValue;
252
386
 
253
387
  });
254
388
 
389
+ if(Object.keys(errors).length > 0){
390
+
391
+ throw {
392
+ message: 'Object not clean',
393
+ errors
394
+ };
395
+
396
+ }
397
+
255
398
  return cleanObj;
256
399
 
257
400
  }