@rosen-bridge/config 0.1.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.
Files changed (50) hide show
  1. package/.eslintignore +1 -0
  2. package/CHANGELOG.md +8 -0
  3. package/README.md +24 -0
  4. package/dist/cli.d.ts +3 -0
  5. package/dist/cli.d.ts.map +1 -0
  6. package/dist/cli.js +102 -0
  7. package/dist/config.d.ts +131 -0
  8. package/dist/config.d.ts.map +1 -0
  9. package/dist/config.js +578 -0
  10. package/dist/index.d.ts +2 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +2 -0
  13. package/dist/schema/Validators/fieldProperties.d.ts +29 -0
  14. package/dist/schema/Validators/fieldProperties.d.ts.map +1 -0
  15. package/dist/schema/Validators/fieldProperties.js +254 -0
  16. package/dist/schema/types/fields.d.ts +37 -0
  17. package/dist/schema/types/fields.d.ts.map +1 -0
  18. package/dist/schema/types/fields.js +2 -0
  19. package/dist/schema/types/validations.d.ts +46 -0
  20. package/dist/schema/types/validations.d.ts.map +1 -0
  21. package/dist/schema/types/validations.js +2 -0
  22. package/dist/tsconfig.tsbuildinfo +1 -0
  23. package/dist/utils.d.ts +36 -0
  24. package/dist/utils.d.ts.map +1 -0
  25. package/dist/utils.js +59 -0
  26. package/dist/value/validators.d.ts +3 -0
  27. package/dist/value/validators.d.ts.map +1 -0
  28. package/dist/value/validators.js +188 -0
  29. package/lib/cli.ts +113 -0
  30. package/lib/config.ts +664 -0
  31. package/lib/index.ts +1 -0
  32. package/lib/schema/Validators/fieldProperties.ts +273 -0
  33. package/lib/schema/types/fields.ts +46 -0
  34. package/lib/schema/types/validations.ts +63 -0
  35. package/lib/utils.ts +68 -0
  36. package/lib/value/validators.ts +268 -0
  37. package/package.json +48 -0
  38. package/tests/.gitkeep +0 -0
  39. package/tests/config.spec.ts +895 -0
  40. package/tests/configEnvSetup.ts +34 -0
  41. package/tests/configTestData.ts +977 -0
  42. package/tests/configTestFiles/custom-environment-variables.json +5 -0
  43. package/tests/configTestFiles/default.json +12 -0
  44. package/tests/configTestFiles/local.json +5 -0
  45. package/tests/utils.spec.ts +117 -0
  46. package/tests/utilsTestData.ts +26 -0
  47. package/tsconfig.build.json +7 -0
  48. package/tsconfig.build.tsbuildinfo +1 -0
  49. package/tsconfig.json +7 -0
  50. package/vitest.config.ts +11 -0
@@ -0,0 +1,895 @@
1
+ import { configDir } from './configEnvSetup';
2
+ import config from 'config';
3
+ import fs from 'fs';
4
+ import path from 'path';
5
+ import { afterAll, beforeEach, describe, expect, it } from 'vitest';
6
+ import { ConfigValidator } from '../lib';
7
+ import { ConfigSchema } from '../lib/schema/types/fields';
8
+ import * as testData from './configTestData';
9
+
10
+ afterAll(() => {
11
+ fs.rmSync(configDir, { force: true, recursive: true });
12
+ });
13
+
14
+ beforeEach(() => {
15
+ fs.readdirSync(configDir).forEach((file) => {
16
+ fs.unlinkSync(path.join(configDir, file));
17
+ });
18
+ fs.cpSync(path.join(__dirname, 'configTestFiles'), configDir, {
19
+ recursive: true,
20
+ });
21
+ });
22
+
23
+ describe('ConfigValidator', () => {
24
+ describe('generateDefault', () => {
25
+ /**
26
+ * @target generateDefault should return default values object for the
27
+ * passed schema
28
+ * @dependencies
29
+ * @scenario
30
+ * - call generateDefault
31
+ * - check if correct default value object is returned
32
+ * @expected
33
+ * - correct default value object should have been returned
34
+ */
35
+ it(`should return default values object for the passed schema`, async () => {
36
+ const config = new ConfigValidator(
37
+ <ConfigSchema>testData.apiSchemaDefaultValuePairSample.schema
38
+ );
39
+ expect(config.generateDefault()).toEqual(
40
+ testData.apiSchemaDefaultValuePairSample.defaultVal
41
+ );
42
+ });
43
+ });
44
+
45
+ describe('validateSchema', () => {
46
+ /**
47
+ * @target validateSchema should not throw any exceptions when a correct
48
+ * schema is passed
49
+ * @dependencies
50
+ * @scenario
51
+ * - create a new instance of Config which calls Config.validateSchema
52
+ * - check if any exception is thrown
53
+ * @expected
54
+ * - no errors should be thrown
55
+ */
56
+ it(`should not throw any exceptions when a correct schema is passed`, async () => {
57
+ new ConfigValidator(<ConfigSchema>testData.correctApiSchema);
58
+ });
59
+
60
+ /**
61
+ * @target validateSchema should throw exception when a schema with
62
+ * incorrect default value type is passed
63
+ * @dependencies
64
+ * @scenario
65
+ * - create a new instance of Config which calls Config.validateSchema
66
+ * - check if any exception is thrown
67
+ * @expected
68
+ * - exception should be thrown
69
+ */
70
+ it(`should throw exception when a schema with incorrect default value type
71
+ is passed`, async () => {
72
+ expect(
73
+ () =>
74
+ new ConfigValidator(
75
+ <ConfigSchema>testData.schemaWithIncorrectPortDefaultValueTypeSample
76
+ )
77
+ ).toThrow();
78
+ });
79
+ });
80
+
81
+ describe('validateConfig', () => {
82
+ /**
83
+ * @target validateConfig should not throw any exceptions when a correct
84
+ * config is passed
85
+ * @dependencies
86
+ * @scenario
87
+ * - call validateConfig with the config
88
+ * - check if any exception is thrown
89
+ * @expected
90
+ * - no errors should be thrown
91
+ */
92
+ it(`should not throw any exceptions when a correct config is passed`, async () => {
93
+ const confValidator = new ConfigValidator(
94
+ <ConfigSchema>testData.apiSchemaConfigPair.schema
95
+ );
96
+ confValidator.validateConfig(testData.apiSchemaConfigPair.config);
97
+ });
98
+
99
+ /**
100
+ * @target validateConfig should throw exception when a config violating
101
+ * choices constraint is passed
102
+ * @dependencies
103
+ * @scenario
104
+ * - call validateConfig with the config
105
+ * - check if any exception is thrown
106
+ * @expected
107
+ * - exception should be thrown
108
+ */
109
+ it(`should throw exception when a config violating choices constraint is
110
+ passed`, async () => {
111
+ const confValidator = new ConfigValidator(
112
+ <ConfigSchema>testData.apiSchemaConfigPairWrongChoice.schema
113
+ );
114
+
115
+ expect(() =>
116
+ confValidator.validateConfig(
117
+ testData.apiSchemaConfigPairWrongChoice.config
118
+ )
119
+ ).toThrow();
120
+ });
121
+
122
+ /**
123
+ * @target validateSchema should throw exception when a config violating
124
+ * regex constraint is passed
125
+ * @dependencies
126
+ * @scenario
127
+ * - call validateConfig with the config
128
+ * - check if any exception is thrown
129
+ * @expected
130
+ * - exception should be thrown
131
+ */
132
+ it(`should throw exception when a config violating regex constraint is
133
+ passed`, async () => {
134
+ const confValidator = new ConfigValidator(
135
+ <ConfigSchema>testData.apiSchemaConfigPairWrongRegex.schema
136
+ );
137
+
138
+ expect(() =>
139
+ confValidator.validateConfig(
140
+ testData.apiSchemaConfigPairWrongRegex.config
141
+ )
142
+ ).toThrow();
143
+ });
144
+
145
+ /**
146
+ * @target validateSchema should throw exception when a config violating the
147
+ * "required" constraint is passed
148
+ * @dependencies
149
+ * @scenario
150
+ * - call validateConfig with the config
151
+ * - check if any exception is thrown
152
+ * @expected
153
+ * - exception should be thrown
154
+ */
155
+ it(`should throw exception when a config violating the "required" constraint
156
+ is passed`, async () => {
157
+ const confValidator = new ConfigValidator(
158
+ <ConfigSchema>testData.apiSchemaConfigPairWrongRequired.schema
159
+ );
160
+
161
+ expect(() =>
162
+ confValidator.validateConfig(
163
+ testData.apiSchemaConfigPairWrongRequired.config
164
+ )
165
+ ).toThrow();
166
+ });
167
+
168
+ /**
169
+ * @target validateSchema should throw exception when a config violating the
170
+ * value type is passed
171
+ * @dependencies
172
+ * @scenario
173
+ * - call validateConfig with the config
174
+ * - check if any exception is thrown
175
+ * @expected
176
+ * - exception should be thrown
177
+ */
178
+ it(`should throw exception when a config violating the value type is passed`, async () => {
179
+ const confValidator = new ConfigValidator(
180
+ <ConfigSchema>testData.apiSchemaConfigPairWrongPortType.schema
181
+ );
182
+
183
+ expect(() =>
184
+ confValidator.validateConfig(
185
+ testData.apiSchemaConfigPairWrongPortType.config
186
+ )
187
+ ).toThrow();
188
+ });
189
+
190
+ /**
191
+ * @target validateSchema should throw exception when a config violating the
192
+ * "greater than" constraint, is passed
193
+ * @dependencies
194
+ * @scenario
195
+ * - call validateConfig with the config
196
+ * - check if any exception is thrown
197
+ * @expected
198
+ * - exception should be thrown
199
+ */
200
+ it(`should throw exception when a config violating the "greater than"
201
+ constraint, is passed`, async () => {
202
+ const confValidator = new ConfigValidator(
203
+ <ConfigSchema>testData.apiSchemaConfigPairWrongGreater.schema
204
+ );
205
+
206
+ expect(() =>
207
+ confValidator.validateConfig(
208
+ testData.apiSchemaConfigPairWrongGreater.config
209
+ )
210
+ ).toThrow();
211
+ });
212
+
213
+ /**
214
+ * @target validateSchema should throw exception when a config violating the
215
+ * "greater than or equal" constraint, is passed
216
+ * @dependencies
217
+ * @scenario
218
+ * - call validateConfig with the config
219
+ * - check if any exception is thrown
220
+ * @expected
221
+ * - exception should be thrown
222
+ */
223
+ it(`should throw exception when a config violating the
224
+ "greater than or equal" constraint, is passed`, async () => {
225
+ const confValidator = new ConfigValidator(
226
+ <ConfigSchema>testData.apiSchemaConfigPairWrongGreaterEqual.schema
227
+ );
228
+
229
+ expect(() =>
230
+ confValidator.validateConfig(
231
+ testData.apiSchemaConfigPairWrongGreaterEqual.config
232
+ )
233
+ ).toThrow();
234
+ });
235
+
236
+ /**
237
+ * @target validateSchema should throw exception when a config violating the
238
+ * "less than" constraint, is passed
239
+ * @dependencies
240
+ * @scenario
241
+ * - call validateConfig with the config
242
+ * - check if any exception is thrown
243
+ * @expected
244
+ * - exception should be thrown
245
+ */
246
+ it(`should throw exception when a config violating the "less than"
247
+ constraint, is passed`, async () => {
248
+ const confValidator = new ConfigValidator(
249
+ <ConfigSchema>testData.apiSchemaConfigPairWrongLess.schema
250
+ );
251
+
252
+ expect(() =>
253
+ confValidator.validateConfig(
254
+ testData.apiSchemaConfigPairWrongLess.config
255
+ )
256
+ ).toThrow();
257
+ });
258
+
259
+ /**
260
+ * @target validateSchema should throw exception when a config violating the
261
+ * "less than or equal" constraint, is passed
262
+ * @dependencies
263
+ * @scenario
264
+ * - call validateConfig with the config
265
+ * - check if any exception is thrown
266
+ * @expected
267
+ * - exception should be thrown
268
+ */
269
+ it(`should throw exception when a config violating the "less than or equal"
270
+ constraint, is passed`, async () => {
271
+ const confValidator = new ConfigValidator(
272
+ <ConfigSchema>testData.apiSchemaConfigPairWrongLessEqual.schema
273
+ );
274
+
275
+ expect(() =>
276
+ confValidator.validateConfig(
277
+ testData.apiSchemaConfigPairWrongLessEqual.config
278
+ )
279
+ ).toThrow();
280
+ });
281
+
282
+ /**
283
+ * @target validateSchema should throw exception when a config violating the
284
+ * "greater than for bigint" constraint, is passed
285
+ * @dependencies
286
+ * @scenario
287
+ * - call validateConfig with the config
288
+ * - check if any exception is thrown
289
+ * @expected
290
+ * - exception should be thrown
291
+ */
292
+ it(`should throw exception when a config violating the
293
+ "greater than for bigint" constraint, is passed`, async () => {
294
+ const confValidator = new ConfigValidator(
295
+ <ConfigSchema>testData.apiSchemaConfigPairWrongGreaterBigInt.schema
296
+ );
297
+
298
+ expect(() =>
299
+ confValidator.validateConfig(
300
+ testData.apiSchemaConfigPairWrongGreaterBigInt.config
301
+ )
302
+ ).toThrow();
303
+ });
304
+
305
+ /**
306
+ * @target validateSchema should throw exception when a config violating the
307
+ * "greater than or equal for bigint" constraint, is passed
308
+ * @dependencies
309
+ * @scenario
310
+ * - call validateConfig with the config
311
+ * - check if any exception is thrown
312
+ * @expected
313
+ * - exception should be thrown
314
+ */
315
+ it(`should throw exception when a config violating the
316
+ "greater than or equal for bigint" constraint, is passed`, async () => {
317
+ const confValidator = new ConfigValidator(
318
+ <ConfigSchema>testData.apiSchemaConfigPairWrongGreaterEqualBigInt.schema
319
+ );
320
+
321
+ expect(() =>
322
+ confValidator.validateConfig(
323
+ testData.apiSchemaConfigPairWrongGreaterEqualBigInt.config
324
+ )
325
+ ).toThrow();
326
+ });
327
+
328
+ /**
329
+ * @target validateSchema should throw exception when a config violating the
330
+ * "less than for bigint" constraint, is passed
331
+ * @dependencies
332
+ * @scenario
333
+ * - call validateConfig with the config
334
+ * - check if any exception is thrown
335
+ * @expected
336
+ * - exception should be thrown
337
+ */
338
+ it(`should throw exception when a config violating the
339
+ "less than for bigint" constraint, is passed`, async () => {
340
+ const confValidator = new ConfigValidator(
341
+ <ConfigSchema>testData.apiSchemaConfigPairWrongLessBigInt.schema
342
+ );
343
+
344
+ expect(() =>
345
+ confValidator.validateConfig(
346
+ testData.apiSchemaConfigPairWrongLessBigInt.config
347
+ )
348
+ ).toThrow();
349
+ });
350
+
351
+ /**
352
+ * @target validateSchema should throw exception when a config violating the
353
+ * "less than or equal for bigint" constraint, is passed
354
+ * @dependencies
355
+ * @scenario
356
+ * - call validateConfig with the config
357
+ * - check if any exception is thrown
358
+ * @expected
359
+ * - exception should be thrown
360
+ */
361
+ it(`should throw exception when a config violating the
362
+ "less than or equal for bigint" constraint, is passed`, async () => {
363
+ const confValidator = new ConfigValidator(
364
+ <ConfigSchema>testData.apiSchemaConfigPairWrongLessEqualBigInt.schema
365
+ );
366
+
367
+ expect(() =>
368
+ confValidator.validateConfig(
369
+ testData.apiSchemaConfigPairWrongLessEqualBigInt.config
370
+ )
371
+ ).toThrow();
372
+ });
373
+
374
+ /**
375
+ * @target validateSchema should not throw exception when config violates
376
+ * "required" validation but the "when" clause is false
377
+ * @dependencies
378
+ * @scenario
379
+ * - call validateConfig with the config
380
+ * - check if any exception is thrown
381
+ * @expected
382
+ * - exception should be thrown
383
+ */
384
+ it(`should not throw exception when config violates "required" validation
385
+ but the "when" clause is false`, async () => {
386
+ const confValidator = new ConfigValidator(
387
+ <ConfigSchema>testData.apiSchemaConfigPairWrongRequiredFalseWhen.schema
388
+ );
389
+
390
+ confValidator.validateConfig(
391
+ testData.apiSchemaConfigPairWrongRequiredFalseWhen.config
392
+ );
393
+ });
394
+
395
+ /**
396
+ * @target validateSchema should not throw exception when config violates
397
+ * "regex" validation but the "when" clause is false
398
+ * @dependencies
399
+ * @scenario
400
+ * - call validateConfig with the config
401
+ * - check if any exception is thrown
402
+ * @expected
403
+ * - exception should be thrown
404
+ */
405
+ it(`should not throw exception when config violates "regex" validation but
406
+ the "when" clause is false`, async () => {
407
+ const confValidator = new ConfigValidator(
408
+ <ConfigSchema>testData.apiSchemaConfigPairWrongRegexFalseWhen.schema
409
+ );
410
+
411
+ confValidator.validateConfig(
412
+ testData.apiSchemaConfigPairWrongRegexFalseWhen.config
413
+ );
414
+ });
415
+
416
+ /**
417
+ * @target validateSchema should not throw exception when config violates
418
+ * "choices" validation but the "when" clause is false
419
+ * @dependencies
420
+ * @scenario
421
+ * - call validateConfig with the config
422
+ * - check if any exception is thrown
423
+ * @expected
424
+ * - exception should be thrown
425
+ */
426
+ it(`should not throw exception when config violates "choices" validation but
427
+ the "when" clause is false`, async () => {
428
+ const confValidator = new ConfigValidator(
429
+ <ConfigSchema>testData.apiSchemaConfigPairWrongChoiceFalseWhen.schema
430
+ );
431
+
432
+ confValidator.validateConfig(
433
+ testData.apiSchemaConfigPairWrongChoiceFalseWhen.config
434
+ );
435
+ });
436
+
437
+ /**
438
+ * @target validateSchema should not throw exception when config violates
439
+ * "gt" validation but the "when" clause is false
440
+ * @dependencies
441
+ * @scenario
442
+ * - call validateConfig with the config
443
+ * - check if any exception is thrown
444
+ * @expected
445
+ * - exception should be thrown
446
+ */
447
+ it(`should not throw exception when config violates "gt" validation but the
448
+ "when" clause is false`, async () => {
449
+ const confValidator = new ConfigValidator(
450
+ <ConfigSchema>testData.apiSchemaConfigPairWrongGreaterFalseWhen.schema
451
+ );
452
+
453
+ confValidator.validateConfig(
454
+ testData.apiSchemaConfigPairWrongGreaterFalseWhen.config
455
+ );
456
+ });
457
+
458
+ /**
459
+ * @target validateSchema should not throw exception when config violates
460
+ * "gte" validation but the "when" clause is false
461
+ * @dependencies
462
+ * @scenario
463
+ * - call validateConfig with the config
464
+ * - check if any exception is thrown
465
+ * @expected
466
+ * - exception should be thrown
467
+ */
468
+ it(`should not throw exception when config violates "gte" validation but
469
+ the "when" clause is false`, async () => {
470
+ const confValidator = new ConfigValidator(
471
+ <ConfigSchema>(
472
+ testData.apiSchemaConfigPairWrongGreaterEqualFalseWhen.schema
473
+ )
474
+ );
475
+
476
+ confValidator.validateConfig(
477
+ testData.apiSchemaConfigPairWrongGreaterEqualFalseWhen.config
478
+ );
479
+ });
480
+
481
+ /**
482
+ * @target validateSchema should not throw exception when config violates
483
+ * "lt" validation but the "when" clause is false
484
+ * @dependencies
485
+ * @scenario
486
+ * - call validateConfig with the config
487
+ * - check if any exception is thrown
488
+ * @expected
489
+ * - exception should be thrown
490
+ */
491
+ it(`should not throw exception when config violates "lt" validation but the
492
+ "when" clause is false`, async () => {
493
+ const confValidator = new ConfigValidator(
494
+ <ConfigSchema>testData.apiSchemaConfigPairWrongLessFalseWhen.schema
495
+ );
496
+
497
+ confValidator.validateConfig(
498
+ testData.apiSchemaConfigPairWrongLessFalseWhen.config
499
+ );
500
+ });
501
+
502
+ /**
503
+ * @target validateSchema should not throw exception when config violates
504
+ * "lte" validation but the "when" clause is false
505
+ * @dependencies
506
+ * @scenario
507
+ * - call validateConfig with the config
508
+ * - check if any exception is thrown
509
+ * @expected
510
+ * - exception should be thrown
511
+ */
512
+ it(`should not throw exception when config violates "lte" validation but the
513
+ "when" clause is false`, async () => {
514
+ const confValidator = new ConfigValidator(
515
+ <ConfigSchema>testData.apiSchemaConfigPairWrongLessEqualFalseWhen.schema
516
+ );
517
+
518
+ confValidator.validateConfig(
519
+ testData.apiSchemaConfigPairWrongLessEqualFalseWhen.config
520
+ );
521
+ });
522
+
523
+ /**
524
+ * @target validateSchema should not throw exception when config violates
525
+ * "bigint gt" validation but the "when" clause is false
526
+ * @dependencies
527
+ * @scenario
528
+ * - call validateConfig with the config
529
+ * - check if any exception is thrown
530
+ * @expected
531
+ * - exception should be thrown
532
+ */
533
+ it(`should not throw exception when config violates "bigint gt" validation
534
+ but the "when" clause is false`, async () => {
535
+ const confValidator = new ConfigValidator(
536
+ <ConfigSchema>(
537
+ testData.apiSchemaConfigPairWrongBigIntGreaterFalseWhen.schema
538
+ )
539
+ );
540
+
541
+ confValidator.validateConfig(
542
+ testData.apiSchemaConfigPairWrongBigIntGreaterFalseWhen.config
543
+ );
544
+ });
545
+
546
+ /**
547
+ * @target validateSchema should not throw exception when config violates
548
+ * "bigint gte" validation but the "when" clause is false
549
+ * @dependencies
550
+ * @scenario
551
+ * - call validateConfig with the config
552
+ * - check if any exception is thrown
553
+ * @expected
554
+ * - exception should be thrown
555
+ */
556
+ it(`should not throw exception when config violates "bigint gte" validation
557
+ but the "when" clause is false`, async () => {
558
+ const confValidator = new ConfigValidator(
559
+ <ConfigSchema>(
560
+ testData.apiSchemaConfigPairWrongBigIntGreaterEqualFalseWhen.schema
561
+ )
562
+ );
563
+
564
+ confValidator.validateConfig(
565
+ testData.apiSchemaConfigPairWrongBigIntGreaterEqualFalseWhen.config
566
+ );
567
+ });
568
+
569
+ /**
570
+ * @target validateSchema should not throw exception when config violates
571
+ * "bigint lt" validation but the "when" clause is false
572
+ * @dependencies
573
+ * @scenario
574
+ * - call validateConfig with the config
575
+ * - check if any exception is thrown
576
+ * @expected
577
+ * - exception should be thrown
578
+ */
579
+ it(`should not throw exception when config violates "bigint lt" validation
580
+ but the "when" clause is false`, async () => {
581
+ const confValidator = new ConfigValidator(
582
+ <ConfigSchema>(
583
+ testData.apiSchemaConfigPairWrongBigIntLessFalseWhen.schema
584
+ )
585
+ );
586
+
587
+ confValidator.validateConfig(
588
+ testData.apiSchemaConfigPairWrongBigIntLessFalseWhen.config
589
+ );
590
+ });
591
+
592
+ /**
593
+ * @target validateSchema should not throw exception when config violates
594
+ * "bigint lte" validation but the "when" clause is false
595
+ * @dependencies
596
+ * @scenario
597
+ * - call validateConfig with the config
598
+ * - check if any exception is thrown
599
+ * @expected
600
+ * - exception should be thrown
601
+ */
602
+ it(`should not throw exception when config violates "bigint lte" validation
603
+ but the "when" clause is false`, async () => {
604
+ const confValidator = new ConfigValidator(
605
+ <ConfigSchema>(
606
+ testData.apiSchemaConfigPairWrongBigIntLessEqualFalseWhen.schema
607
+ )
608
+ );
609
+
610
+ confValidator.validateConfig(
611
+ testData.apiSchemaConfigPairWrongBigIntLessEqualFalseWhen.config
612
+ );
613
+ });
614
+
615
+ /**
616
+ * @target validateSchema should throw exception using the custom message
617
+ * when the validation has error property set
618
+ * @dependencies
619
+ * @scenario
620
+ * - call validateConfig with the config
621
+ * - check if any exception is thrown with the right message
622
+ * @expected
623
+ * - exception should be thrown with the right message
624
+ */
625
+ it(`should throw exception using the custom message when the validation has
626
+ error property set`, async () => {
627
+ const confValidator = new ConfigValidator(
628
+ <ConfigSchema>testData.apiSchemaConfigPairWrongChoice.schema
629
+ );
630
+
631
+ expect(() =>
632
+ confValidator.validateConfig(
633
+ testData.apiSchemaConfigPairWrongChoice.config
634
+ )
635
+ ).toThrow(
636
+ testData.apiSchemaConfigPairWrongChoice.schema.apiType.validations[1]
637
+ .error
638
+ );
639
+ });
640
+
641
+ /**
642
+ * @target validateSchema should not throw exception when "bigint" field is
643
+ * passed in string format
644
+ * @dependencies
645
+ * @scenario
646
+ * - call validateConfig with the config
647
+ * - check if any exception is thrown
648
+ * @expected
649
+ * - exception should not be thrown
650
+ */
651
+ it(`should not throw exception when "bigint" field is passed in string
652
+ format`, async () => {
653
+ const confValidator = new ConfigValidator(
654
+ <ConfigSchema>testData.apiSchemaConfigPairWithStringBigInt.schema
655
+ );
656
+
657
+ confValidator.validateConfig(
658
+ testData.apiSchemaConfigPairWithStringBigInt.config
659
+ );
660
+ });
661
+
662
+ /**
663
+ * @target validateSchema should not throw exception when "number" field is
664
+ * passed in string format
665
+ * @dependencies
666
+ * @scenario
667
+ * - call validateConfig with the config
668
+ * - check if any exception is thrown
669
+ * @expected
670
+ * - exception should not be thrown
671
+ */
672
+ it(`should not throw exception when "number" field is passed in string
673
+ format`, async () => {
674
+ const confValidator = new ConfigValidator(
675
+ <ConfigSchema>testData.apiSchemaConfigPairWithStringNumber.schema
676
+ );
677
+
678
+ confValidator.validateConfig(
679
+ testData.apiSchemaConfigPairWithStringNumber.config
680
+ );
681
+ });
682
+ });
683
+
684
+ describe('valueAt', () => {
685
+ /**
686
+ * @target valueAt should return the value at specified path in config
687
+ * object
688
+ * @dependencies
689
+ * @scenario
690
+ * - call valueAt with the config and path
691
+ * - check if correct value is returned
692
+ * @expected
693
+ * - correct value should be returned
694
+ */
695
+ it(`should return the value at specified path in config object`, async () => {
696
+ const configObject = {
697
+ apiType: 'explorer',
698
+ servers: {
699
+ url: 'node256.mydomain.net',
700
+ },
701
+ apis: {
702
+ explorer: {
703
+ url: 'example.com',
704
+ port: 600,
705
+ },
706
+ },
707
+ };
708
+
709
+ const value = ConfigValidator.valueAt(configObject, [
710
+ 'apis',
711
+ 'explorer',
712
+ 'url',
713
+ ]);
714
+
715
+ expect(value).toEqual('example.com');
716
+ });
717
+
718
+ /**
719
+ * @target valueAt should return undefined when invalid path is passed
720
+ * @dependencies
721
+ * @scenario
722
+ * - call valueAt with the config and path
723
+ * - check if undefined is returned
724
+ * @expected
725
+ * - undefined should be returned
726
+ */
727
+ it(`should return undefined when invalid path is passed`, async () => {
728
+ const configObject = {
729
+ apiType: 'explorer',
730
+ servers: {
731
+ url: 'node256.mydomain.net',
732
+ },
733
+ apis: {
734
+ explorer: {
735
+ url: 'example.com',
736
+ port: 600,
737
+ },
738
+ },
739
+ };
740
+
741
+ const value = ConfigValidator.valueAt(configObject, [
742
+ 'apis',
743
+ 'node',
744
+ 'url',
745
+ ]);
746
+
747
+ expect(value).toEqual(undefined);
748
+ });
749
+ });
750
+
751
+ describe('generateTSTypes', () => {
752
+ /**
753
+ * @target generateTSTypes should return TypeScript interfaces for
754
+ * this.schema
755
+ * @dependencies
756
+ * @scenario
757
+ * - call generateTSTypes
758
+ * - check if correct types string is returned
759
+ * @expected
760
+ * - correct types string should be returned
761
+ */
762
+ it(`should return TypeScript interfaces for this.schema`, async () => {
763
+ const confValidator = new ConfigValidator(
764
+ <ConfigSchema>testData.schemaTypeScriptTypesPair.schema
765
+ );
766
+ const types = confValidator.generateTSTypes('Infrastructure');
767
+ expect(types).toEqual(testData.schemaTypeScriptTypesPair.types);
768
+ });
769
+ });
770
+
771
+ describe('getConfigForLevel', () => {
772
+ /**
773
+ * @target getConfigForLevel should return the correct characteristic object
774
+ * for passed level of node config package
775
+ * @dependencies
776
+ * @scenario
777
+ * - call getConfigForLevel
778
+ * - check if correct characteristic object is returned
779
+ * @expected
780
+ * - correct characteristic object should be returned
781
+ */
782
+ it(`should return the correct characteristic object for passed level of node
783
+ config package`, async () => {
784
+ const confValidator = new ConfigValidator(
785
+ <ConfigSchema>testData.schemaConfigCharPair.schema
786
+ );
787
+ const configCharacteristic = confValidator.getConfigForLevel(
788
+ config,
789
+ 'local'
790
+ );
791
+
792
+ expect(configCharacteristic).toEqual(
793
+ testData.schemaConfigCharPair.characteristic
794
+ );
795
+ });
796
+ });
797
+
798
+ describe('validateAndWriteConfig', () => {
799
+ /**
800
+ * @target validateAndWriteConfig should validate config when merged with
801
+ * passed object and write it to the appropriate config file
802
+ * @dependencies
803
+ * @scenario
804
+ * - call validateAndWriteConfig
805
+ * - check validateAndWriteConfig to throw no exception
806
+ * - check the config file to be correctly saved
807
+ * @expected
808
+ * - validateAndWriteConfig should throw no exception
809
+ * - the config file should be correctly saved
810
+ */
811
+ it(`should validate config when merged with passed object and write it to
812
+ the appropriate config file`, async () => {
813
+ const confValidator = new ConfigValidator(
814
+ <ConfigSchema>testData.schemaConfigCharPair.schema
815
+ );
816
+ const obj = { apiType: 'node' };
817
+ confValidator.validateAndWriteConfig(obj, config, 'default', 'json');
818
+ const savedObj = JSON.parse(
819
+ fs.readFileSync(path.join(configDir, 'default.json'), 'utf-8')
820
+ );
821
+
822
+ expect(savedObj).toEqual(obj);
823
+ });
824
+
825
+ /**
826
+ * @target validateAndWriteConfig should throw exception when config after
827
+ * being merged with passed object is not valid and preserve the original
828
+ * config file
829
+ * @dependencies
830
+ * @scenario
831
+ * - call validateAndWriteConfig
832
+ * - check validateAndWriteConfig to throw exception
833
+ * - check the config file to be unchanged
834
+ * @expected
835
+ * - validateAndWriteConfig should throw exception
836
+ * - config file should be unchanged
837
+ */
838
+ it(`should throw exception when config after being merged with passed object
839
+ is not valid and preserve the original config file`, async () => {
840
+ const confValidator = new ConfigValidator(
841
+ <ConfigSchema>testData.schemaConfigCharPair.schema
842
+ );
843
+ const originalObj = JSON.parse(
844
+ fs.readFileSync(path.join(configDir, 'local.json'), 'utf-8')
845
+ );
846
+
847
+ const obj = { apiType: 'wrong-value' };
848
+
849
+ expect(() =>
850
+ confValidator.validateAndWriteConfig(obj, config, 'local', 'json')
851
+ ).toThrow('value should be one of the choices');
852
+
853
+ const savedObj = JSON.parse(
854
+ fs.readFileSync(path.join(configDir, 'local.json'), 'utf-8')
855
+ );
856
+ expect(savedObj).toEqual(originalObj);
857
+ });
858
+
859
+ /**
860
+ * @target validateAndWriteConfig should not throw exception when config
861
+ * after being merged with passed object is valid even if the passed object
862
+ * itself is not valid
863
+ * @dependencies
864
+ * @scenario
865
+ * - call validateAndWriteConfig
866
+ * - check validateAndWriteConfig not to throw exception
867
+ * - check the config file to be correctly saved
868
+ * @expected
869
+ * - validateAndWriteConfig should throw exception
870
+ * - config file should be correctly saved
871
+ */
872
+ it(`should throw exception when config after being merged with passed object
873
+ is not valid and preserve the original config file`, async () => {
874
+ const confValidator = new ConfigValidator(
875
+ <ConfigSchema>testData.schemaConfigCharPair.schema
876
+ );
877
+ const originalObj = JSON.parse(
878
+ fs.readFileSync(path.join(configDir, 'local.json'), 'utf-8')
879
+ );
880
+
881
+ const obj = {
882
+ servers: {
883
+ url: 555,
884
+ },
885
+ };
886
+
887
+ confValidator.validateAndWriteConfig(obj, config, 'local', 'json');
888
+
889
+ const savedObj = JSON.parse(
890
+ fs.readFileSync(path.join(configDir, 'local.json'), 'utf-8')
891
+ );
892
+ expect(savedObj).toEqual(obj);
893
+ });
894
+ });
895
+ });