json-2-joi 2.4.0 → 2.5.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/LICENSE.txt +203 -203
- package/README.md +361 -359
- package/index.js +14 -14
- package/lib/resolver.js +675 -669
- package/package.json +54 -54
package/lib/resolver.js
CHANGED
|
@@ -1,669 +1,675 @@
|
|
|
1
|
-
/* global Map */
|
|
2
|
-
const Joi = require('joi');
|
|
3
|
-
const Hoek = require('@hapi/hoek');
|
|
4
|
-
const Bourne = require('@hapi/bourne');
|
|
5
|
-
const debugCreator = require('debug');
|
|
6
|
-
|
|
7
|
-
const debug = debugCreator('enjoi');
|
|
8
|
-
|
|
9
|
-
debugCreator.formatters.f = function (f) {
|
|
10
|
-
return f();
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const schemaSchema = Joi.alternatives(Joi.object().unknown(true), Joi.string()).required();
|
|
14
|
-
const extensionSchema = Joi.alternatives().try(Joi.object().unknown(true), Joi.function())
|
|
15
|
-
|
|
16
|
-
const optionsSchema = Joi.object({
|
|
17
|
-
subSchemas: Joi.object().unknown(true).default({}),
|
|
18
|
-
extensions: Joi.array().items(extensionSchema).default([]),
|
|
19
|
-
refineType: Joi.func().allow(null),
|
|
20
|
-
refineSchema: Joi.func().allow(null),
|
|
21
|
-
refineDescription: Joi.func().allow(null),
|
|
22
|
-
allowNull: Joi.boolean().default(false),
|
|
23
|
-
forbidArrayNull: Joi.boolean().default(true),
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
joiInstance = Joi;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
this.
|
|
166
|
-
this.
|
|
167
|
-
this.
|
|
168
|
-
|
|
169
|
-
this.
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
//
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
const
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
//
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
schema.type =
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
joischema =
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
joischema = joischema.
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
joischema = joischema.
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
if (schema.
|
|
564
|
-
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
case '
|
|
604
|
-
joischema
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
case '
|
|
615
|
-
return joischema.
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
case '
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
isNumber(schema.
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
case '
|
|
660
|
-
joiType = this.joi.
|
|
661
|
-
break;
|
|
662
|
-
|
|
663
|
-
joiType = this.joi.
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
1
|
+
/* global Map */
|
|
2
|
+
const Joi = require('joi');
|
|
3
|
+
const Hoek = require('@hapi/hoek');
|
|
4
|
+
const Bourne = require('@hapi/bourne');
|
|
5
|
+
const debugCreator = require('debug');
|
|
6
|
+
|
|
7
|
+
const debug = debugCreator('enjoi');
|
|
8
|
+
|
|
9
|
+
debugCreator.formatters.f = function (f) {
|
|
10
|
+
return f();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const schemaSchema = Joi.alternatives(Joi.object().unknown(true), Joi.string()).required();
|
|
14
|
+
const extensionSchema = Joi.alternatives().try(Joi.object().unknown(true), Joi.function())
|
|
15
|
+
|
|
16
|
+
const optionsSchema = Joi.object({
|
|
17
|
+
subSchemas: Joi.object().unknown(true).default({}),
|
|
18
|
+
extensions: Joi.array().items(extensionSchema).default([]),
|
|
19
|
+
refineType: Joi.func().allow(null),
|
|
20
|
+
refineSchema: Joi.func().allow(null),
|
|
21
|
+
refineDescription: Joi.func().allow(null),
|
|
22
|
+
allowNull: Joi.boolean().default(false),
|
|
23
|
+
forbidArrayNull: Joi.boolean().default(true),
|
|
24
|
+
strictArrayRequired: Joi.boolean().default(false),
|
|
25
|
+
strictRequired: Joi.boolean().default(false),
|
|
26
|
+
strictEnum: Joi.boolean().default(true),
|
|
27
|
+
enableEnum: Joi.boolean().default(true),
|
|
28
|
+
customizedNullValues: Joi.array().items(Joi.any()).default([null]).min(1),
|
|
29
|
+
joiOptions: Joi.object().unknown(true).default({})
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const overridableOptions = {
|
|
33
|
+
refineType: Joi.func().allow(null),
|
|
34
|
+
refineSchema: Joi.func().allow(null),
|
|
35
|
+
refineDescription: Joi.func().allow(null),
|
|
36
|
+
allowNull: Joi.boolean(),
|
|
37
|
+
forbidArrayNull: Joi.boolean(),
|
|
38
|
+
strictArrayRequired: Joi.boolean(),
|
|
39
|
+
strictRequired: Joi.boolean(),
|
|
40
|
+
noDefaults: Joi.boolean(),
|
|
41
|
+
strictEnum: Joi.boolean(),
|
|
42
|
+
enableEnum: Joi.boolean(),
|
|
43
|
+
customizedNullValues: Joi.array().items(Joi.any()).min(1),
|
|
44
|
+
};
|
|
45
|
+
const overridableOptionKeys = Object.keys(overridableOptions);
|
|
46
|
+
const overrideOptionsSchema = Joi.object(overridableOptions);
|
|
47
|
+
|
|
48
|
+
function mergeOverridableOptions(target, ...sources) {
|
|
49
|
+
sources.forEach((src) => {
|
|
50
|
+
overridableOptionKeys.forEach((key) => {
|
|
51
|
+
if (typeof src[key] !== 'undefined') {
|
|
52
|
+
target[key] = src[key];
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
});
|
|
56
|
+
return target;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function isNumber(value) {
|
|
60
|
+
return typeof value === 'number';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function isObject(value) {
|
|
64
|
+
return value !== null && typeof value === 'object';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function isNotEmptyObject(value) {
|
|
68
|
+
return isObject(value) && Object.keys(value).length > 0;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function allowNullIfNeeded(schema, { allowNull = false, forbidArrayNull = true, customizedNullValues = [null] }) {
|
|
72
|
+
if (allowNull) {
|
|
73
|
+
const definition = schema.describe();
|
|
74
|
+
if (definition.flags && definition.flags.only && definition.allow) {
|
|
75
|
+
// Already has enum restriction
|
|
76
|
+
return schema;
|
|
77
|
+
}
|
|
78
|
+
if (forbidArrayNull && schema.type === 'array') {
|
|
79
|
+
return schema;
|
|
80
|
+
}
|
|
81
|
+
return schema.allow(...customizedNullValues);
|
|
82
|
+
}
|
|
83
|
+
return schema;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function addEnumRestriction(joiSchema, enumList, { allowNull = false, strictEnum = true, enableEnum = true }) {
|
|
87
|
+
if (!enableEnum || !Array.isArray(enumList) || enumList.length === 0) {
|
|
88
|
+
return joiSchema;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const valids = [].concat(enumList);
|
|
92
|
+
if (allowNull && !strictEnum) {
|
|
93
|
+
if (joiSchema.type === 'string') {
|
|
94
|
+
valids.push('');
|
|
95
|
+
}
|
|
96
|
+
valids.push(null);
|
|
97
|
+
}
|
|
98
|
+
return joiSchema.valid(...valids);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function normalizedId(id) {
|
|
102
|
+
return id.replace(/[^a-zA-Z0-9]/g, '_');
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function getJoiInstance(joiOptions, extensions, joiInstance) {
|
|
106
|
+
if (joiInstance) {
|
|
107
|
+
return joiInstance;
|
|
108
|
+
}
|
|
109
|
+
if (isNotEmptyObject(joiOptions)) {
|
|
110
|
+
joiInstance = Joi.defaults((schema) => schema.options(joiOptions));
|
|
111
|
+
} else {
|
|
112
|
+
joiInstance = Joi;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return joiInstance.extend(
|
|
116
|
+
{
|
|
117
|
+
type: 'object',
|
|
118
|
+
base: joiInstance.object(),
|
|
119
|
+
coerce: {
|
|
120
|
+
from: 'string',
|
|
121
|
+
method(value) {
|
|
122
|
+
if (typeof value !== 'string' || (value[0] !== '{' && !/^\s*\{/.test(value))) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
return { value: Bourne.parse(value) };
|
|
127
|
+
} catch (ignoreErr) { } // eslint-disable-line no-empty
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
type: 'array',
|
|
133
|
+
base: joiInstance.array(),
|
|
134
|
+
coerce: {
|
|
135
|
+
from: 'string',
|
|
136
|
+
method(value) {
|
|
137
|
+
if (typeof value !== 'string' || (value[0] !== '[' && !/^\s*\[/.test(value))) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
try {
|
|
141
|
+
return { value: Bourne.parse(value) };
|
|
142
|
+
} catch (ignoreErr) { } // eslint-disable-line no-empty
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
...extensions
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const mapToObj = m => {
|
|
151
|
+
return Array.from(m).reduce((obj, [key, value]) => {
|
|
152
|
+
obj[key] = Joi.isSchema(value) ? value.describe() : value;
|
|
153
|
+
return obj;
|
|
154
|
+
}, {});
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
class SchemaResolver {
|
|
158
|
+
constructor(options, joiInstance) {
|
|
159
|
+
const validatedResult = optionsSchema.validate(options);
|
|
160
|
+
|
|
161
|
+
Hoek.assert(!validatedResult.error, validatedResult.error);
|
|
162
|
+
|
|
163
|
+
const { subSchemas = {}, extensions = [], joiOptions } = validatedResult.value;
|
|
164
|
+
|
|
165
|
+
this.resolveOptions = mergeOverridableOptions({}, validatedResult.value);
|
|
166
|
+
this.subSchemas = subSchemas;
|
|
167
|
+
this.joiOptions = joiOptions;
|
|
168
|
+
this.joiSharedSchemas = new Map();
|
|
169
|
+
this.resolveOptions.noDefaults = this.joiOptions.noDefaults;
|
|
170
|
+
|
|
171
|
+
this.joi = getJoiInstance(joiOptions, extensions, joiInstance);
|
|
172
|
+
|
|
173
|
+
for (let sharedSchemaId in this.subSchemas) {
|
|
174
|
+
const sharedSchema = this.subSchemas[sharedSchemaId];
|
|
175
|
+
if (typeof sharedSchema === 'object' && !sharedSchema.$id) {
|
|
176
|
+
// shorthand schema type does not need to set $id
|
|
177
|
+
sharedSchema.$id = sharedSchemaId;
|
|
178
|
+
}
|
|
179
|
+
this.joiSharedSchemas.set(sharedSchemaId, this.resolve(sharedSchema, this.resolveOptions));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const itr = this.joiSharedSchemas.keys();
|
|
183
|
+
this.initialSharedSchemaIds = [];
|
|
184
|
+
for (const id of itr) {
|
|
185
|
+
this.initialSharedSchemaIds.push(id);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
resolveShareSchemas(schema, resolveOptions, sharedProperties = '$defs', baseUri = '') {
|
|
190
|
+
const localShared = schema[sharedProperties]
|
|
191
|
+
if (localShared) {
|
|
192
|
+
for (let sharedSchemaId in localShared) {
|
|
193
|
+
this.joiSharedSchemas.set(
|
|
194
|
+
`${baseUri}#/${sharedProperties}/${sharedSchemaId}`,
|
|
195
|
+
this.resolve(localShared[sharedSchemaId], resolveOptions, baseUri)
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
convert(schema, options = {}) {
|
|
202
|
+
const validateSchema = schemaSchema.validate(schema);
|
|
203
|
+
const { value: overrideOptions, error: overrideOptionsError } = overrideOptionsSchema.validate(options);
|
|
204
|
+
|
|
205
|
+
Hoek.assert(!validateSchema.error, validateSchema.error);
|
|
206
|
+
Hoek.assert(!overrideOptionsError, overrideOptionsError);
|
|
207
|
+
|
|
208
|
+
const schemaId = schema.$id || '';
|
|
209
|
+
this.subSchemas[schemaId] = schema;
|
|
210
|
+
|
|
211
|
+
const joiSchema = this.resolve(schema, mergeOverridableOptions({}, this.resolveOptions, overrideOptions));
|
|
212
|
+
|
|
213
|
+
// Cleanup all shared schema registered other than the initial shared ones
|
|
214
|
+
delete this.subSchemas[schemaId];
|
|
215
|
+
const itr = this.joiSharedSchemas.keys();
|
|
216
|
+
for (const id of itr) {
|
|
217
|
+
if (!this.initialSharedSchemaIds.includes(id)) {
|
|
218
|
+
delete this.subSchemas[id];
|
|
219
|
+
this.joiSharedSchemas.delete(id);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return joiSchema;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
resolve(schema, resolveOptions = {}, baseUri = (schema.$id || '')) {
|
|
226
|
+
if (this.joi.isSchema(schema)) {
|
|
227
|
+
return schema;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const schemaId = schema.$id;
|
|
231
|
+
let resolvedSchema;
|
|
232
|
+
|
|
233
|
+
if (schemaId) {
|
|
234
|
+
baseUri = schemaId;
|
|
235
|
+
if (!this.subSchemas[schemaId]) {
|
|
236
|
+
this.subSchemas[schemaId] = schema;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
this.resolveShareSchemas(schema, resolveOptions, '$defs', baseUri);
|
|
241
|
+
this.resolveShareSchemas(schema, resolveOptions, 'definitions', baseUri);
|
|
242
|
+
|
|
243
|
+
if (typeof schema === 'string') {
|
|
244
|
+
// If schema is itself a string, interpret it as a type
|
|
245
|
+
resolvedSchema = this.resolveType({ type: schema }, resolveOptions, baseUri);
|
|
246
|
+
} else if (schema.$ref) {
|
|
247
|
+
if (schema.$ref.trim() === '#') {
|
|
248
|
+
return baseUri ? this.joi.link(`#${normalizedId(baseUri)}`) : this.joi.link('/');
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
resolvedSchema = this.resolve(this.resolveReference(schema.$ref, baseUri), resolveOptions, baseUri);
|
|
252
|
+
} else {
|
|
253
|
+
const partialSchemas = [];
|
|
254
|
+
if (schema.type) {
|
|
255
|
+
partialSchemas.push(this.resolveType(schema, resolveOptions, baseUri));
|
|
256
|
+
} else if (schema.properties) {
|
|
257
|
+
// if no type is specified, just properties
|
|
258
|
+
partialSchemas.push(this.object(schema, resolveOptions, baseUri))
|
|
259
|
+
} else if (schema.format) {
|
|
260
|
+
// if no type is specified, just format
|
|
261
|
+
partialSchemas.push(this.string(schema))
|
|
262
|
+
} else if (schema.enum) {
|
|
263
|
+
// If no type is specified, just enum
|
|
264
|
+
partialSchemas.push(
|
|
265
|
+
addEnumRestriction(this.joi.any().example(schema.enum[0]), schema.enum, resolveOptions)
|
|
266
|
+
);
|
|
267
|
+
} else if (schema.const) {
|
|
268
|
+
// Constant value
|
|
269
|
+
partialSchemas.push(this.getValueJoiType(schema.const).valid(schema.const));
|
|
270
|
+
}
|
|
271
|
+
if (schema.anyOf) {
|
|
272
|
+
partialSchemas.push(this.resolveAnyOf(schema, resolveOptions, baseUri));
|
|
273
|
+
}
|
|
274
|
+
if (schema.allOf) {
|
|
275
|
+
partialSchemas.push(this.resolveAllOf(schema, resolveOptions, baseUri));
|
|
276
|
+
}
|
|
277
|
+
if (schema.oneOf) {
|
|
278
|
+
partialSchemas.push(this.resolveOneOf(schema, resolveOptions, baseUri));
|
|
279
|
+
}
|
|
280
|
+
if (schema.not) {
|
|
281
|
+
partialSchemas.push(this.resolveNot(schema, resolveOptions, baseUri));
|
|
282
|
+
}
|
|
283
|
+
if (partialSchemas.length === 0) {
|
|
284
|
+
//Fall through to whatever.
|
|
285
|
+
//eslint-disable-next-line no-console
|
|
286
|
+
console.warn('WARNING: schema missing a \'type\' or \'$ref\' or \'enum\': \n%s', JSON.stringify(schema, null, 2));
|
|
287
|
+
//TODO: Handle better
|
|
288
|
+
partialSchemas.push(this.joi.any());
|
|
289
|
+
}
|
|
290
|
+
resolvedSchema = partialSchemas.length === 1 ? partialSchemas[0] : this.joi.alternatives(partialSchemas).match('all');
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (resolveOptions.refineSchema) {
|
|
294
|
+
resolvedSchema = resolveOptions.refineSchema(resolvedSchema, schema);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (schema.default !== undefined && !resolveOptions.noDefaults) {
|
|
298
|
+
resolvedSchema = resolvedSchema.default(schema.default)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (schemaId) {
|
|
302
|
+
resolvedSchema = resolvedSchema.id(normalizedId(schemaId));
|
|
303
|
+
this.joiSharedSchemas.set(schemaId, resolvedSchema);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (schema.$anchor) {
|
|
307
|
+
this.joiSharedSchemas.set(`${baseUri}#${schema.$anchor}`, resolvedSchema);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return resolvedSchema;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
resolveReference(value, baseUri = '') {
|
|
314
|
+
debug('----- Resolving schema reference: %s with baseUri: %s ----- ', value, baseUri);
|
|
315
|
+
debug('Registered JOI schema for shared: %f', () => {
|
|
316
|
+
return JSON.stringify(mapToObj(this.joiSharedSchemas), null, 2);
|
|
317
|
+
});
|
|
318
|
+
const hashIdx = value.indexOf('#');
|
|
319
|
+
let refSchema;
|
|
320
|
+
|
|
321
|
+
if (hashIdx === -1) {
|
|
322
|
+
// Locate through $ref: 'refId'
|
|
323
|
+
// debug('# Resolving reference through id: %s', value);
|
|
324
|
+
refSchema = this.joiSharedSchemas.get(value);
|
|
325
|
+
} else {
|
|
326
|
+
const id = value.substring(0, hashIdx);
|
|
327
|
+
const path = value.substring(hashIdx + 1);
|
|
328
|
+
|
|
329
|
+
const canonicalURI = id ? value : `${baseUri}${value}`;
|
|
330
|
+
|
|
331
|
+
if (canonicalURI.indexOf('/') === -1) {
|
|
332
|
+
// Locate through $ref: [baseUri]#anchorSchema
|
|
333
|
+
// debug('# Resolving reference through $anchor: %s', canonicalURI);
|
|
334
|
+
refSchema = this.joiSharedSchemas.get(canonicalURI);
|
|
335
|
+
} else if (canonicalURI.indexOf('/properties') === -1) {
|
|
336
|
+
// Locate through $ref: [baseUri]#/$defs/shared
|
|
337
|
+
// debug('# Resolving reference through direct canonicalURI: %s', canonicalURI);
|
|
338
|
+
refSchema = this.joiSharedSchemas.get(canonicalURI);
|
|
339
|
+
} else {
|
|
340
|
+
// Locate through $ref: [baseUri]#/$defs/shared/properties/level1 or [baseUri]#/properties/product
|
|
341
|
+
// Can only get raw schema and build Joi again because it might not completely built yet
|
|
342
|
+
// debug('# Resolving reference through relative path: baseUri - %s; id - %s; path - %s', baseUri, id, path);
|
|
343
|
+
|
|
344
|
+
const paths = path.split('/');
|
|
345
|
+
refSchema = this.subSchemas[id || baseUri];
|
|
346
|
+
|
|
347
|
+
while (paths.length > 0 && refSchema) {
|
|
348
|
+
if (paths[0]) {
|
|
349
|
+
refSchema = refSchema[paths[0]];
|
|
350
|
+
}
|
|
351
|
+
paths.splice(0, 1);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
Hoek.assert(refSchema, `Can not find schema reference: ${value} with baseUri: ${baseUri}`);
|
|
357
|
+
|
|
358
|
+
return refSchema;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
resolveType(schema, resolveOptions, baseUri) {
|
|
362
|
+
let joischema;
|
|
363
|
+
|
|
364
|
+
const joitype = (type, options, format) => {
|
|
365
|
+
let joischema;
|
|
366
|
+
|
|
367
|
+
if (options.refineType) {
|
|
368
|
+
type = options.refineType(type, format);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
switch (type) {
|
|
372
|
+
case 'array':
|
|
373
|
+
joischema = this.array(schema, options, baseUri);
|
|
374
|
+
break;
|
|
375
|
+
case 'boolean':
|
|
376
|
+
joischema = this.joi.boolean();
|
|
377
|
+
break;
|
|
378
|
+
case 'integer':
|
|
379
|
+
case 'number':
|
|
380
|
+
joischema = this.number(schema);
|
|
381
|
+
break;
|
|
382
|
+
case 'object':
|
|
383
|
+
joischema = this.object(schema, options, baseUri);
|
|
384
|
+
break;
|
|
385
|
+
case 'string':
|
|
386
|
+
joischema = this.string(schema);
|
|
387
|
+
break;
|
|
388
|
+
case 'null':
|
|
389
|
+
joischema = this.joi.any().valid(...options.customizedNullValues);
|
|
390
|
+
break;
|
|
391
|
+
default:
|
|
392
|
+
joischema = this.joi.types()[type];
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
Hoek.assert(joischema, 'Could not resolve type: ' + schema.type + '.');
|
|
396
|
+
|
|
397
|
+
return joischema;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
let nullIdx;
|
|
401
|
+
if (Array.isArray(schema.type)) {
|
|
402
|
+
nullIdx = schema.type.findIndex((type) => type === 'null');
|
|
403
|
+
if (nullIdx > -1) {
|
|
404
|
+
schema.type.splice(nullIdx, 1);
|
|
405
|
+
}
|
|
406
|
+
if (schema.type.length === 1) {
|
|
407
|
+
schema.type = schema.type[0];
|
|
408
|
+
} else if (schema.type.length === 0) {
|
|
409
|
+
schema.type = 'null';
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
if (Array.isArray(schema.type)) {
|
|
414
|
+
const schemas = [];
|
|
415
|
+
|
|
416
|
+
for (let i = 0; i < schema.type.length; i++) {
|
|
417
|
+
schemas.push(joitype(schema.type[i], resolveOptions, schema.format));
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
joischema = this.joi.alternatives(schemas);
|
|
421
|
+
} else {
|
|
422
|
+
joischema = joitype(schema.type, resolveOptions, schema.format);
|
|
423
|
+
}
|
|
424
|
+
if (nullIdx > -1) {
|
|
425
|
+
joischema = allowNullIfNeeded(joischema,
|
|
426
|
+
{ allowNull: true, customizedNullValues: resolveOptions.customizedNullValues });
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const typeDefinitionMap = {
|
|
430
|
+
title: 'label'
|
|
431
|
+
};
|
|
432
|
+
Object.keys(typeDefinitionMap).forEach(function (key) {
|
|
433
|
+
if (schema[key] !== undefined) {
|
|
434
|
+
joischema = joischema[typeDefinitionMap[key]](schema[key]);
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
let desc = schema.description;
|
|
439
|
+
if (resolveOptions.refineDescription) {
|
|
440
|
+
desc = resolveOptions.refineDescription(schema);
|
|
441
|
+
}
|
|
442
|
+
if (desc) {
|
|
443
|
+
joischema = joischema.description(desc);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
let example;
|
|
447
|
+
if (schema.examples && schema.examples.length > 0) {
|
|
448
|
+
example = schema.examples[0];
|
|
449
|
+
}
|
|
450
|
+
if (!example && typeof schema.default !== 'undefined') {
|
|
451
|
+
example = schema.default;
|
|
452
|
+
}
|
|
453
|
+
if (!example && schema.enum && schema.enum.length > 0) {
|
|
454
|
+
example = schema.enum[0];
|
|
455
|
+
}
|
|
456
|
+
if (example) {
|
|
457
|
+
joischema = joischema.type === 'array' && !Array.isArray(example) ? joischema.example([example]) : joischema.example(example);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
return addEnumRestriction(joischema, schema.enum, resolveOptions);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
resolveOneOf(schema, resolveOptions, baseUri) {
|
|
464
|
+
Hoek.assert(Array.isArray(schema.oneOf), 'Expected oneOf to be an array.');
|
|
465
|
+
|
|
466
|
+
return this.joi.alternatives(schema.oneOf.map(schema => this.resolve(schema, resolveOptions, baseUri))).match('one');
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
resolveAnyOf(schema, resolveOptions, baseUri) {
|
|
470
|
+
Hoek.assert(Array.isArray(schema.anyOf), 'Expected anyOf to be an array.');
|
|
471
|
+
|
|
472
|
+
return this.joi.alternatives(schema.anyOf.map(schema => this.resolve(schema, resolveOptions, baseUri))).match('any');
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
resolveAllOf(schema, resolveOptions, baseUri) {
|
|
476
|
+
Hoek.assert(Array.isArray(schema.allOf), 'Expected allOf to be an array.');
|
|
477
|
+
|
|
478
|
+
return this.joi.alternatives(schema.allOf.map(schema => this.resolve(schema, resolveOptions, baseUri))).match('all');
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
resolveNot(schema, resolveOptions, baseUri) {
|
|
482
|
+
Hoek.assert(isObject(schema.not), 'Expected Not to be an object.');
|
|
483
|
+
|
|
484
|
+
return this.joi.alternatives().conditional(
|
|
485
|
+
'.',
|
|
486
|
+
{
|
|
487
|
+
not: this.resolve(schema.not, resolveOptions, baseUri),
|
|
488
|
+
then: this.joi.any(),
|
|
489
|
+
otherwise: this.joi.any().forbidden()
|
|
490
|
+
}
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
resolveProperties(jsonSchema, resolveOptions, baseUri) {
|
|
495
|
+
const schemas = {};
|
|
496
|
+
|
|
497
|
+
if (!isObject(jsonSchema.properties)) {
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
Object.keys(jsonSchema.properties).forEach((key) => {
|
|
502
|
+
const property = jsonSchema.properties[key];
|
|
503
|
+
|
|
504
|
+
let joischema = this.resolve(property, resolveOptions, baseUri);
|
|
505
|
+
|
|
506
|
+
if (Array.isArray(jsonSchema.required) && !!~jsonSchema.required.indexOf(key)) {
|
|
507
|
+
joischema = joischema.required();
|
|
508
|
+
if (resolveOptions.strictRequired) {
|
|
509
|
+
joischema = joischema.invalid(null, '');
|
|
510
|
+
}
|
|
511
|
+
} else {
|
|
512
|
+
joischema = allowNullIfNeeded(joischema, resolveOptions);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
schemas[key] = joischema;
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
return schemas;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
object(schema, resolveOptions, baseUri) {
|
|
522
|
+
let joischema = this.joi.object(this.resolveProperties(schema, resolveOptions, baseUri));
|
|
523
|
+
|
|
524
|
+
if (isObject(schema.additionalProperties)) {
|
|
525
|
+
joischema = joischema.pattern(/^/, this.resolve(schema.additionalProperties, resolveOptions, baseUri));
|
|
526
|
+
} else if (typeof this.joiOptions.allowUnknown === 'undefined') {
|
|
527
|
+
joischema = joischema.unknown(schema.additionalProperties !== false);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
isNumber(schema.minProperties) && (joischema = joischema.min(schema.minProperties));
|
|
531
|
+
isNumber(schema.maxProperties) && (joischema = joischema.max(schema.maxProperties));
|
|
532
|
+
|
|
533
|
+
return joischema;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
resolveAsArray(value, resolveOptions, baseUri) {
|
|
537
|
+
return [].concat(value).map((v) => {
|
|
538
|
+
const vSchema = this.resolve(v, resolveOptions, baseUri);
|
|
539
|
+
if (resolveOptions.strictArrayRequired) {
|
|
540
|
+
return vSchema.invalid(...resolveOptions.customizedNullValues);
|
|
541
|
+
}
|
|
542
|
+
return allowNullIfNeeded(vSchema, resolveOptions)
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
array(schema, resolveOptions, baseUri) {
|
|
547
|
+
let joischema = this.joi.array();
|
|
548
|
+
let items;
|
|
549
|
+
|
|
550
|
+
if (schema.items) {
|
|
551
|
+
items = this.resolveAsArray(schema.items, resolveOptions, baseUri);
|
|
552
|
+
|
|
553
|
+
joischema = joischema.items(...items);
|
|
554
|
+
} else if (schema.ordered) {
|
|
555
|
+
items = this.resolveAsArray(schema.ordered, resolveOptions, baseUri);
|
|
556
|
+
joischema = joischema.ordered(...items);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
if (items && schema.additionalItems === false) {
|
|
560
|
+
joischema = joischema.max(items.length);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
if (resolveOptions.strictArrayRequired && !schema.minItems) {
|
|
564
|
+
schema.minItems = 1;
|
|
565
|
+
}
|
|
566
|
+
isNumber(schema.minItems) && (joischema = joischema.min(schema.minItems));
|
|
567
|
+
isNumber(schema.maxItems) && (joischema = joischema.max(schema.maxItems));
|
|
568
|
+
|
|
569
|
+
if (schema.uniqueItems) {
|
|
570
|
+
joischema = joischema.unique();
|
|
571
|
+
}
|
|
572
|
+
if (schema.contains) {
|
|
573
|
+
joischema = joischema.has(this.resolve(schema.contains, resolveOptions, baseUri));
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
return joischema;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
number(schema) {
|
|
580
|
+
let joischema = this.joi.number();
|
|
581
|
+
|
|
582
|
+
if (schema.type === 'integer') {
|
|
583
|
+
joischema = joischema.integer();
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
isNumber(schema.minimum) && (joischema = joischema.min(schema.minimum));
|
|
587
|
+
isNumber(schema.maximum) && (joischema = joischema.max(schema.maximum));
|
|
588
|
+
isNumber(schema.exclusiveMinimum) && (joischema = joischema.greater(schema.exclusiveMinimum));
|
|
589
|
+
isNumber(schema.exclusiveMaximum) && (joischema = joischema.less(schema.exclusiveMaximum));
|
|
590
|
+
isNumber(schema.multipleOf) && schema.multipleOf !== 0 && (joischema = joischema.multiple(schema.multipleOf));
|
|
591
|
+
|
|
592
|
+
return joischema;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
string(schema) {
|
|
596
|
+
let joischema = this.joi.string();
|
|
597
|
+
|
|
598
|
+
const dateRegex = '(\\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])';
|
|
599
|
+
const timeRegex = '([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(.[0-9]+)?(Z|(\\+|-)([01][0-9]|2[0-3]):([0-5][0-9]))';
|
|
600
|
+
const dateTimeRegex = dateRegex + 'T' + timeRegex;
|
|
601
|
+
|
|
602
|
+
switch (schema.format) {
|
|
603
|
+
case 'date':
|
|
604
|
+
return joischema.regex(new RegExp('^' + dateRegex + '$', 'i'), 'JsonSchema date format');
|
|
605
|
+
case 'time':
|
|
606
|
+
return joischema.regex(new RegExp('^' + timeRegex + '$', 'i'), 'JsonSchema time format');
|
|
607
|
+
case 'date-time':
|
|
608
|
+
return joischema.regex(new RegExp('^' + dateTimeRegex + '$', 'i'), 'JsonSchema date-time format');
|
|
609
|
+
case 'binary':
|
|
610
|
+
joischema = this.binary(schema);
|
|
611
|
+
break;
|
|
612
|
+
case 'email':
|
|
613
|
+
return joischema.email();
|
|
614
|
+
case 'hostname':
|
|
615
|
+
return joischema.hostname();
|
|
616
|
+
case 'ipv4':
|
|
617
|
+
return joischema.ip({
|
|
618
|
+
version: ['ipv4']
|
|
619
|
+
});
|
|
620
|
+
case 'ipv6':
|
|
621
|
+
return joischema.ip({
|
|
622
|
+
version: ['ipv6']
|
|
623
|
+
});
|
|
624
|
+
case 'uri':
|
|
625
|
+
return joischema.uri();
|
|
626
|
+
case 'byte':
|
|
627
|
+
joischema = joischema.base64();
|
|
628
|
+
break;
|
|
629
|
+
case 'uuid':
|
|
630
|
+
return joischema.guid({ version: ['uuidv4'] });
|
|
631
|
+
case 'guid':
|
|
632
|
+
return joischema.guid();
|
|
633
|
+
}
|
|
634
|
+
return this.regularString(schema, joischema);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
regularString(schema, joischema) {
|
|
638
|
+
schema.pattern && (joischema = joischema.regex(new RegExp(schema.pattern)));
|
|
639
|
+
|
|
640
|
+
if ((typeof schema.minLength === 'undefined' || schema.minLength === 0) && !schema.pattern && !schema.format && !schema.enum) {
|
|
641
|
+
joischema = joischema.allow('');
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
isNumber(schema.minLength) && (joischema = joischema.min(schema.minLength));
|
|
645
|
+
isNumber(schema.maxLength) && (joischema = joischema.max(schema.maxLength));
|
|
646
|
+
return joischema;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
binary(schema) {
|
|
650
|
+
let joischema = this.joi.binary();
|
|
651
|
+
isNumber(schema.minLength) && (joischema = joischema.min(schema.minLength));
|
|
652
|
+
isNumber(schema.maxLength) && (joischema = joischema.max(schema.maxLength));
|
|
653
|
+
return joischema;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
getValueJoiType(value) {
|
|
657
|
+
let joiType;
|
|
658
|
+
switch (typeof value) {
|
|
659
|
+
case 'string':
|
|
660
|
+
joiType = this.joi.string();
|
|
661
|
+
break;
|
|
662
|
+
case 'number':
|
|
663
|
+
joiType = this.joi.number();
|
|
664
|
+
break;
|
|
665
|
+
case 'boolean':
|
|
666
|
+
joiType = this.joi.boolean();
|
|
667
|
+
break;
|
|
668
|
+
default:
|
|
669
|
+
joiType = this.joi.any();
|
|
670
|
+
}
|
|
671
|
+
return joiType;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
module.exports = SchemaResolver;
|