@awesomeness-js/utils 1.1.6 → 1.1.7
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 +47 -10
package/package.json
CHANGED
package/src/utils/clean.js
CHANGED
|
@@ -9,7 +9,7 @@ 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
|
+
function cleanArray(arr, schema = {}, testMode = false){
|
|
13
13
|
|
|
14
14
|
try {
|
|
15
15
|
|
|
@@ -146,7 +146,7 @@ function cleanArray(arr, schema = {}){
|
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
|
|
149
|
-
function cleanObject(obj, schema){
|
|
149
|
+
function cleanObject(obj, schema, testMode = false){
|
|
150
150
|
|
|
151
151
|
validateSchema(schema);
|
|
152
152
|
|
|
@@ -179,6 +179,10 @@ function cleanObject(obj, schema){
|
|
|
179
179
|
|
|
180
180
|
}
|
|
181
181
|
|
|
182
|
+
if(testMode){
|
|
183
|
+
console.log('cleanObject keysPassed:', keysPassed);
|
|
184
|
+
}
|
|
185
|
+
|
|
182
186
|
const cleanObj = {};
|
|
183
187
|
|
|
184
188
|
// Iterate over the schema keys
|
|
@@ -187,6 +191,14 @@ function cleanObject(obj, schema){
|
|
|
187
191
|
const valType = thingType(value);
|
|
188
192
|
const supposedToBeType = schema.properties[key].type;
|
|
189
193
|
|
|
194
|
+
if(testMode){
|
|
195
|
+
console.log(`cleaning ${key}`,{
|
|
196
|
+
valType,
|
|
197
|
+
supposedToBeType,
|
|
198
|
+
value,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
190
202
|
if(valType !== supposedToBeType){
|
|
191
203
|
|
|
192
204
|
throw {
|
|
@@ -200,50 +212,75 @@ function cleanObject(obj, schema){
|
|
|
200
212
|
|
|
201
213
|
let cleanedValue;
|
|
202
214
|
|
|
215
|
+
const knownTypesToClean = [
|
|
216
|
+
'boolean',
|
|
217
|
+
'integer',
|
|
218
|
+
'number',
|
|
219
|
+
'string',
|
|
220
|
+
'timestamp',
|
|
221
|
+
'uuid',
|
|
222
|
+
'object',
|
|
223
|
+
'array'
|
|
224
|
+
];
|
|
225
|
+
|
|
226
|
+
if(!knownTypesToClean.includes(supposedToBeType)){
|
|
227
|
+
|
|
228
|
+
throw {
|
|
229
|
+
message: 'Unknown type to clean in schema',
|
|
230
|
+
supposedToBeType,
|
|
231
|
+
key
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
if(testMode){ console.log('cleaning boolean', value); }
|
|
238
|
+
|
|
239
|
+
|
|
203
240
|
if(supposedToBeType === 'boolean'){
|
|
204
241
|
|
|
205
|
-
cleanedValue = cleanBoolean(value);
|
|
242
|
+
cleanedValue = cleanBoolean(value, schema.properties[key]);
|
|
206
243
|
|
|
207
244
|
}
|
|
208
245
|
|
|
209
246
|
if(supposedToBeType === 'integer'){
|
|
210
247
|
|
|
211
|
-
cleanedValue = cleanInteger(value);
|
|
248
|
+
cleanedValue = cleanInteger(value, schema.properties[key]);
|
|
212
249
|
|
|
213
250
|
}
|
|
214
251
|
|
|
215
252
|
if(supposedToBeType === 'number'){
|
|
216
253
|
|
|
217
|
-
cleanedValue = cleanNumber(value);
|
|
254
|
+
cleanedValue = cleanNumber(value, schema.properties[key]);
|
|
218
255
|
|
|
219
256
|
}
|
|
220
257
|
|
|
221
258
|
if(supposedToBeType === 'string'){
|
|
222
259
|
|
|
223
|
-
cleanedValue = cleanString(value);
|
|
260
|
+
cleanedValue = cleanString(value, schema.properties[key]);
|
|
224
261
|
|
|
225
262
|
}
|
|
226
263
|
|
|
227
264
|
if(supposedToBeType === 'timestamp'){
|
|
228
265
|
|
|
229
|
-
cleanedValue = cleanTimestamp(value);
|
|
266
|
+
cleanedValue = cleanTimestamp(value, schema.properties[key]);
|
|
230
267
|
|
|
231
268
|
}
|
|
232
269
|
|
|
233
270
|
if(supposedToBeType === 'uuid'){
|
|
234
271
|
|
|
235
|
-
cleanedValue = cleanUUID(value);
|
|
272
|
+
cleanedValue = cleanUUID(value, schema.properties[key]);
|
|
236
273
|
|
|
237
274
|
}
|
|
238
275
|
|
|
239
276
|
if(supposedToBeType === 'object'){
|
|
240
|
-
|
|
277
|
+
|
|
241
278
|
cleanedValue = cleanObject(value, schema.properties[key]);
|
|
242
279
|
|
|
243
280
|
}
|
|
244
281
|
|
|
245
282
|
if(supposedToBeType === 'array'){
|
|
246
|
-
|
|
283
|
+
|
|
247
284
|
cleanedValue = cleanArray(value, schema.properties[key]);
|
|
248
285
|
|
|
249
286
|
}
|