@bluedot-innovation/models 10.5.0 → 10.6.0
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/CHANGELOG.md
CHANGED
|
@@ -239,4 +239,167 @@ describe('Invalid Destination ', () => {
|
|
|
239
239
|
assert.match(err.message, /Invalid state template id/)
|
|
240
240
|
}
|
|
241
241
|
})
|
|
242
|
+
|
|
243
|
+
it('should throw an error if CustomData value is null', async () => {
|
|
244
|
+
destination.customData["name3"] = null;
|
|
245
|
+
|
|
246
|
+
let Destination = new DestinationModel(destination)
|
|
247
|
+
try {
|
|
248
|
+
await Destination.validate()
|
|
249
|
+
} catch (err) {
|
|
250
|
+
assert.exists(err, 'err object should exist')
|
|
251
|
+
assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
|
|
252
|
+
}
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
it('should throw an error if CustomData value is undefined', async () => {
|
|
256
|
+
destination.customData["name3"] = undefined;
|
|
257
|
+
|
|
258
|
+
let Destination = new DestinationModel(destination)
|
|
259
|
+
try {
|
|
260
|
+
await Destination.validate()
|
|
261
|
+
} catch (err) {
|
|
262
|
+
assert.exists(err, 'err object should exist')
|
|
263
|
+
assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
|
|
264
|
+
}
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
it('should throw an error if CustomData value is an empty string', async () => {
|
|
268
|
+
destination.customData["name3"] = ' ';
|
|
269
|
+
|
|
270
|
+
let Destination = new DestinationModel(destination)
|
|
271
|
+
try {
|
|
272
|
+
await Destination.validate()
|
|
273
|
+
} catch (err) {
|
|
274
|
+
assert.exists(err, 'err object should exist')
|
|
275
|
+
assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
|
|
276
|
+
}
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
it('should throw an error if CustomData value is an empty object', async () => {
|
|
280
|
+
destination.customData["name3"] = {};
|
|
281
|
+
|
|
282
|
+
let Destination = new DestinationModel(destination)
|
|
283
|
+
try {
|
|
284
|
+
await Destination.validate()
|
|
285
|
+
} catch (err) {
|
|
286
|
+
assert.exists(err, 'err object should exist')
|
|
287
|
+
assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
|
|
288
|
+
}
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
it('should throw an error if CustomData value is an object', async () => {
|
|
292
|
+
destination.customData["name3"] = { key: 'value' };
|
|
293
|
+
|
|
294
|
+
let Destination = new DestinationModel(destination)
|
|
295
|
+
try {
|
|
296
|
+
await Destination.validate()
|
|
297
|
+
} catch (err) {
|
|
298
|
+
assert.exists(err, 'err object should exist')
|
|
299
|
+
assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
|
|
300
|
+
}
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
it('should throw an error if CustomData value is an empty array', async () => {
|
|
304
|
+
destination.customData["name3"] = [];
|
|
305
|
+
|
|
306
|
+
let Destination = new DestinationModel(destination)
|
|
307
|
+
try {
|
|
308
|
+
await Destination.validate()
|
|
309
|
+
} catch (err) {
|
|
310
|
+
assert.exists(err, 'err object should exist')
|
|
311
|
+
assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
|
|
312
|
+
}
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
it('should throw an error if CustomData value is an array', async () => {
|
|
316
|
+
destination.customData["name3"] = ['value1', 'value2'];
|
|
317
|
+
|
|
318
|
+
let Destination = new DestinationModel(destination)
|
|
319
|
+
try {
|
|
320
|
+
await Destination.validate()
|
|
321
|
+
} catch (err) {
|
|
322
|
+
assert.exists(err, 'err object should exist')
|
|
323
|
+
assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
|
|
324
|
+
}
|
|
325
|
+
})
|
|
326
|
+
|
|
327
|
+
it('should throw an error if CustomData value is a number', async () => {
|
|
328
|
+
destination.customData["name3"] = 1;
|
|
329
|
+
|
|
330
|
+
let Destination = new DestinationModel(destination)
|
|
331
|
+
try {
|
|
332
|
+
await Destination.validate()
|
|
333
|
+
} catch (err) {
|
|
334
|
+
assert.exists(err, 'err object should exist')
|
|
335
|
+
assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
|
|
336
|
+
}
|
|
337
|
+
})
|
|
338
|
+
|
|
339
|
+
it('should throw an error if CustomData value is a boolean', async () => {
|
|
340
|
+
destination.customData["name3"] = true;
|
|
341
|
+
|
|
342
|
+
let Destination = new DestinationModel(destination)
|
|
343
|
+
try {
|
|
344
|
+
await Destination.validate()
|
|
345
|
+
} catch (err) {
|
|
346
|
+
assert.exists(err, 'err object should exist')
|
|
347
|
+
assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
|
|
348
|
+
}
|
|
349
|
+
})
|
|
350
|
+
|
|
351
|
+
it('should throw an error if CustomData value is a NaN', async () => {
|
|
352
|
+
destination.customData["name3"] = NaN;
|
|
353
|
+
|
|
354
|
+
let Destination = new DestinationModel(destination)
|
|
355
|
+
try {
|
|
356
|
+
await Destination.validate()
|
|
357
|
+
} catch (err) {
|
|
358
|
+
assert.exists(err, 'err object should exist')
|
|
359
|
+
assert.match(err.message, /(CustomData value should be a string and should not be empty)/i);
|
|
360
|
+
}
|
|
361
|
+
})
|
|
362
|
+
|
|
363
|
+
it('should throw an error if CustomData has more than 20 key-value pairs', async () => {
|
|
364
|
+
for (let i = 0; i < 20; i++) {
|
|
365
|
+
destination.customData[`name_extra${i}`] = `Extra custom value ${i}`;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
let Destination = new DestinationModel(destination)
|
|
369
|
+
try {
|
|
370
|
+
await Destination.validate()
|
|
371
|
+
} catch (err) {
|
|
372
|
+
assert.exists(err, 'err object should exist')
|
|
373
|
+
assert.match(err.message, /(Only 20 custom data fields are allowed to be added per destination)/i);
|
|
374
|
+
}
|
|
375
|
+
})
|
|
376
|
+
|
|
377
|
+
it('should throw an error if CustomData key has invalid character', async () => {
|
|
378
|
+
destination.customData[`name!@`] = `Invalid character`;
|
|
379
|
+
|
|
380
|
+
let Destination = new DestinationModel(destination)
|
|
381
|
+
try {
|
|
382
|
+
await Destination.validate()
|
|
383
|
+
} catch (err) {
|
|
384
|
+
assert.exists(err, 'err object should exist')
|
|
385
|
+
console.log(err)
|
|
386
|
+
assert.match(err.message, /(CustomData key should have alpha numeric, - and _ characters only)/i);
|
|
387
|
+
}
|
|
388
|
+
})
|
|
389
|
+
|
|
390
|
+
it('should throw an error if CustomData keys are duplicate', async () => {
|
|
391
|
+
destination.customData = {
|
|
392
|
+
"key1": "value1",
|
|
393
|
+
"key1": "value1",
|
|
394
|
+
"key2": "value2",
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
let Destination = new DestinationModel(destination)
|
|
398
|
+
try {
|
|
399
|
+
await Destination.validate()
|
|
400
|
+
} catch (err) {
|
|
401
|
+
assert.exists(err, 'err object should exist')
|
|
402
|
+
assert.match(err.message, /(CustomData key should be unique)/i);
|
|
403
|
+
}
|
|
404
|
+
})
|
|
242
405
|
})
|
|
@@ -158,6 +158,38 @@ describe('Project', () => {
|
|
|
158
158
|
const res = await Project.validate()
|
|
159
159
|
assert.notExists(res)
|
|
160
160
|
})
|
|
161
|
+
|
|
162
|
+
it(`should validate a project when webhook structure is 2.1`, async () => {
|
|
163
|
+
project.webhooks = [
|
|
164
|
+
{
|
|
165
|
+
"type": "trigger",
|
|
166
|
+
"url": "https://webhooklistener.bluedotinnovation.com",
|
|
167
|
+
"retryEnabled": true,
|
|
168
|
+
"structure": "2.1"
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
"type": "tempo",
|
|
172
|
+
"url": "https://webhooklistener.bluedotinnovation.com",
|
|
173
|
+
"retryEnabled": true,
|
|
174
|
+
"structure": "2.1"
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
"type": "wave",
|
|
178
|
+
"url": "https://webhooklistener.bluedotinnovation.com",
|
|
179
|
+
"structure": "standard",
|
|
180
|
+
"structure": "2.1"
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
"type": "helloOrder",
|
|
184
|
+
"url": "https://webhooklistener.bluedotinnovation.com",
|
|
185
|
+
"structure": "standard",
|
|
186
|
+
"structure": "2.1"
|
|
187
|
+
}
|
|
188
|
+
]
|
|
189
|
+
let Project = new ProjectModel(project)
|
|
190
|
+
const res = await Project.validate()
|
|
191
|
+
assert.notExists(res)
|
|
192
|
+
})
|
|
161
193
|
})
|
|
162
194
|
testCase('Valid Project ', () => {
|
|
163
195
|
assertions('should validate a valid project ', async () => {
|
|
@@ -604,7 +636,7 @@ describe('Project', () => {
|
|
|
604
636
|
}
|
|
605
637
|
})
|
|
606
638
|
|
|
607
|
-
it(`should not validate if the type is hellOrder and the structure is not default`, async () => {
|
|
639
|
+
it(`should not validate if the type is hellOrder and the structure is not default or 2.1`, async () => {
|
|
608
640
|
project.webhooks[0].type = "helloOrder"
|
|
609
641
|
project.webhooks[0].structure = "standard"
|
|
610
642
|
let Project = new ProjectModel(project)
|
|
@@ -612,7 +644,7 @@ describe('Project', () => {
|
|
|
612
644
|
await Project.validate()
|
|
613
645
|
} catch (err) {
|
|
614
646
|
assert.exists(err, 'err object should exist')
|
|
615
|
-
assert.match(err.message, /type "helloOrder" can only have structure "default"/)
|
|
647
|
+
assert.match(err.message, /type "helloOrder" can only have structure "default" or "2.1"/)
|
|
616
648
|
}
|
|
617
649
|
})
|
|
618
650
|
it(`should not validate if structure is default and the type is not helloOrder`, async () => {
|
|
@@ -623,7 +655,7 @@ describe('Project', () => {
|
|
|
623
655
|
await Project.validate()
|
|
624
656
|
} catch (err) {
|
|
625
657
|
assert.exists(err, 'err object should exist')
|
|
626
|
-
assert.match(err.message, /type "helloOrder" can only have structure "default"/)
|
|
658
|
+
assert.match(err.message, /type "helloOrder" can only have structure "default" or "2.1"/)
|
|
627
659
|
}
|
|
628
660
|
})
|
|
629
661
|
|
package/models/Destination.js
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
const mongoose = require('mongoose')
|
|
2
|
+
const Schema = mongoose.Schema
|
|
2
3
|
const uuidv4 = require('uuid/v4')
|
|
3
4
|
const patcher = require('@bluedot-innovation/mongoose-json-patch')
|
|
4
5
|
const Point = require('../schemas/Point')
|
|
5
6
|
const DestinationHelloSchema = require('../schemas/DestinationHello')
|
|
6
7
|
const helpers = require('../helpers')
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
const customDataValidators = [
|
|
10
|
+
{validator: helpers.validateCustomData, msg: 'CustomData key should have alpha numeric, - and _ characters only'},
|
|
11
|
+
{validator: helpers.checkDuplicateKeys, msg: 'CustomData key should be unique'},
|
|
12
|
+
{validator: helpers.isCustomDataValuesNotEmptyString, msg: 'CustomData value should be a string and should not be empty'},
|
|
13
|
+
{validator: helpers.isValidCustomDataCount, msg: 'Only 20 custom data fields are allowed to be added per destination'}
|
|
14
|
+
];
|
|
9
15
|
|
|
10
16
|
const DestinationSchema = new mongoose.Schema({
|
|
11
17
|
_id: {type: String, default: uuidv4},
|
|
@@ -19,7 +25,8 @@ const DestinationSchema = new mongoose.Schema({
|
|
|
19
25
|
projectId: {type: String, required: true, validate: {validator: helpers.validateUUIDv4, msg: 'Invalid projectId'}},
|
|
20
26
|
creationTime: {type: Date, default: Date.now},
|
|
21
27
|
lastUpdateTime: {type: Date, default: Date.now},
|
|
22
|
-
hello: DestinationHelloSchema
|
|
28
|
+
hello: DestinationHelloSchema,
|
|
29
|
+
customData: {type: Schema.Types.Mixed, validate: customDataValidators},
|
|
23
30
|
}, {collection: 'destination'})
|
|
24
31
|
|
|
25
32
|
DestinationSchema.plugin(patcher)
|
package/package.json
CHANGED
package/schemas/Webhook.js
CHANGED
|
@@ -31,9 +31,9 @@ function validateTypeAndStructure(type) {
|
|
|
31
31
|
if ((type === "register" && !['mobivity'].includes(this.structure))
|
|
32
32
|
|| (type !== "register" && ['mobivity'].includes(this.structure))) {
|
|
33
33
|
throw new Error('type "register" can only have structure "mobivity"')
|
|
34
|
-
} else if ((type === "helloOrder" && this.structure
|
|
34
|
+
} else if ((type === "helloOrder" && !['default', '2.1'].includes(this.structure))
|
|
35
35
|
|| (type !== "helloOrder" && this.structure === 'default')) {
|
|
36
|
-
throw new Error('type "helloOrder" can only have structure "default"')
|
|
36
|
+
throw new Error('type "helloOrder" can only have structure "default" or "2.1"')
|
|
37
37
|
} else if (type !== 'trigger' && this.structure === '2.0') {
|
|
38
38
|
throw new Error('Only type "trigger" can have structure 2.0')
|
|
39
39
|
}
|
|
@@ -46,7 +46,7 @@ let WebhookSchema = new mongoose.Schema({
|
|
|
46
46
|
body: {type: mongoose.Schema.Types.Mixed, validate: [{ validator: validators.validateNonEmptyStringKeyValuePairsOnly, msg: 'webhook body keys and values must be strings' }, { validator: validators.validateNoSpacesInObjectKeys, msg: 'webhook body keys cannot contain spaces' }]},
|
|
47
47
|
structure: {
|
|
48
48
|
type: String,
|
|
49
|
-
enum: ['standard', 'tealium', 'mobivity', 'default', '2.0'],
|
|
49
|
+
enum: ['standard', 'tealium', 'mobivity', 'default', '2.0', '2.1'],
|
|
50
50
|
default: function () {
|
|
51
51
|
return this.type === 'trigger' ? '2.0' : 'standard'
|
|
52
52
|
},
|