joi-to-json 5.0.3 → 5.0.4
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/lib/parsers/json.js +822 -811
- package/lib/parsers/open-api.js +38 -46
- package/package.json +45 -45
package/lib/parsers/json.js
CHANGED
|
@@ -1,811 +1,822 @@
|
|
|
1
|
-
/* eslint no-use-before-define: 'off' */
|
|
2
|
-
const _ = require('lodash')
|
|
3
|
-
const combinations = require('combinations')
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Default Joi logical operator (or/and/nand/xor/oxor) parsers
|
|
7
|
-
*/
|
|
8
|
-
const LOGICAL_OP_PARSER = {
|
|
9
|
-
or: function (schema, dependency) {
|
|
10
|
-
schema.anyOf = _.map(dependency.peers, (peer) => {
|
|
11
|
-
return { required: [peer] }
|
|
12
|
-
})
|
|
13
|
-
},
|
|
14
|
-
and: function (schema, dependency) {
|
|
15
|
-
schema.oneOf = [{ required: dependency.peers }]
|
|
16
|
-
schema.oneOf.push({
|
|
17
|
-
allOf: _.map(dependency.peers, (peer) => {
|
|
18
|
-
return { not: { required: [peer] } }
|
|
19
|
-
})
|
|
20
|
-
})
|
|
21
|
-
},
|
|
22
|
-
nand: function (schema, dependency) {
|
|
23
|
-
schema.not = { required: dependency.peers }
|
|
24
|
-
},
|
|
25
|
-
xor: function (schema, dependency) {
|
|
26
|
-
schema.if = {
|
|
27
|
-
propertyNames: { enum: dependency.peers },
|
|
28
|
-
minProperties: 2
|
|
29
|
-
}
|
|
30
|
-
schema.then = false
|
|
31
|
-
schema.else = {
|
|
32
|
-
oneOf: _.map(dependency.peers, (peer) => {
|
|
33
|
-
return { required: [peer] }
|
|
34
|
-
})
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
oxor: function (schema, dependency) {
|
|
38
|
-
schema.oneOf = _.map(dependency.peers, (peer) => {
|
|
39
|
-
return { required: [peer] }
|
|
40
|
-
})
|
|
41
|
-
schema.oneOf.push({
|
|
42
|
-
not: {
|
|
43
|
-
oneOf: _.map(combinations(dependency.peers, 1, 2), (combination) => {
|
|
44
|
-
return { required: combination }
|
|
45
|
-
})
|
|
46
|
-
}
|
|
47
|
-
})
|
|
48
|
-
},
|
|
49
|
-
with: function (schema, dependency) {
|
|
50
|
-
schema.dependentRequired = schema.dependentRequired || {}
|
|
51
|
-
schema.dependentRequired[dependency.key] = dependency.peers
|
|
52
|
-
},
|
|
53
|
-
without: function (schema, dependency) {
|
|
54
|
-
schema.if = { required: [dependency.key] }
|
|
55
|
-
schema.then = {
|
|
56
|
-
not: {
|
|
57
|
-
anyOf: _.map(dependency.peers, (peer) => {
|
|
58
|
-
return { required: [peer] }
|
|
59
|
-
})
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Recognize the `joi.override` representation in `describe()` output.
|
|
67
|
-
*
|
|
68
|
-
* `joi.override` is a Symbol that can be used in `joi.any().valid(…)`
|
|
69
|
-
* statements, to reset the list of valid values. In `describe()` output, it
|
|
70
|
-
* turns up as an object with 1 property:
|
|
71
|
-
*
|
|
72
|
-
* ```
|
|
73
|
-
* { override: true }
|
|
74
|
-
* ```
|
|
75
|
-
*/
|
|
76
|
-
function isJoiOverride(e) {
|
|
77
|
-
return typeof e === 'object'
|
|
78
|
-
&& e !== null
|
|
79
|
-
&& Object.keys(e).length === 1
|
|
80
|
-
&& e.override === true
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
class JoiJsonSchemaParser {
|
|
84
|
-
constructor(opts = {}) {
|
|
85
|
-
this.$schema = opts.$schema || 'http://json-schema.org/draft-07/schema#'
|
|
86
|
-
this.includeSchemaDialect = opts.includeSchemaDialect || false
|
|
87
|
-
this.childrenFieldName = this._getChildrenFieldName()
|
|
88
|
-
this.optionsFieldName = this._getOptionsFieldName()
|
|
89
|
-
this.ruleArgFieldName = this._getRuleArgFieldName()
|
|
90
|
-
this.enumFieldName = this._getEnumFieldName()
|
|
91
|
-
this.allowUnknownFlagName = this._getAllowUnknownFlagName()
|
|
92
|
-
this.inheritedPreferences = []
|
|
93
|
-
|
|
94
|
-
if (opts.logicalOpParser === false) {
|
|
95
|
-
this.logicalOpParser = {}
|
|
96
|
-
} else {
|
|
97
|
-
this.logicalOpParser = _.merge({}, LOGICAL_OP_PARSER, opts.logicalOpParser)
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
parse(joiSpec, definitions = {}, level = 0) {
|
|
102
|
-
let schema = {}
|
|
103
|
-
|
|
104
|
-
const hasPreferences = this._pushInheritiedPreferences(joiSpec)
|
|
105
|
-
|
|
106
|
-
if (this._getPresence(joiSpec) === 'forbidden') {
|
|
107
|
-
if (hasPreferences) {
|
|
108
|
-
this._popInheritiedPreferences()
|
|
109
|
-
}
|
|
110
|
-
schema.not = {}
|
|
111
|
-
return schema
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
this._setBasicProperties(schema, joiSpec)
|
|
115
|
-
this._setNumberFieldProperties(schema, joiSpec)
|
|
116
|
-
this._setBinaryFieldProperties(schema, joiSpec)
|
|
117
|
-
this._setStringFieldProperties(schema, joiSpec)
|
|
118
|
-
this._setDateFieldProperties(schema, joiSpec)
|
|
119
|
-
this._setArrayFieldProperties(schema, joiSpec, definitions, level)
|
|
120
|
-
this._setObjectProperties(schema, joiSpec, definitions, level)
|
|
121
|
-
this._setAlternativesProperties(schema, joiSpec, definitions, level)
|
|
122
|
-
this._setConditionProperties(schema, joiSpec, definitions, level, 'whens')
|
|
123
|
-
this._setMetaProperties(schema, joiSpec)
|
|
124
|
-
this._setLinkFieldProperties(schema, joiSpec)
|
|
125
|
-
this._setConst(schema, joiSpec)
|
|
126
|
-
this._setAnyProperties(schema, joiSpec, definitions, level)
|
|
127
|
-
this._addNullTypeIfNullable(schema, joiSpec)
|
|
128
|
-
|
|
129
|
-
if (!_.isEmpty(joiSpec.shared)) {
|
|
130
|
-
_.forEach(joiSpec.shared, (sharedSchema) => {
|
|
131
|
-
this.parse(sharedSchema, definitions, level)
|
|
132
|
-
})
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
this._copyMetasToSchema(joiSpec, schema)
|
|
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
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
-
const
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
this.
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
if (
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
if (
|
|
306
|
-
return
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
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
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
fieldSchema.maximum = value.limit
|
|
414
|
-
break
|
|
415
|
-
case '
|
|
416
|
-
fieldSchema.
|
|
417
|
-
break
|
|
418
|
-
case '
|
|
419
|
-
|
|
420
|
-
|
|
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
|
-
|
|
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
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
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
|
-
|
|
604
|
-
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
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
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
if (conditionSpec.
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
}
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
}
|
|
789
|
-
}
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
if (
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
|
|
1
|
+
/* eslint no-use-before-define: 'off' */
|
|
2
|
+
const _ = require('lodash')
|
|
3
|
+
const combinations = require('combinations')
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Default Joi logical operator (or/and/nand/xor/oxor) parsers
|
|
7
|
+
*/
|
|
8
|
+
const LOGICAL_OP_PARSER = {
|
|
9
|
+
or: function (schema, dependency) {
|
|
10
|
+
schema.anyOf = _.map(dependency.peers, (peer) => {
|
|
11
|
+
return { required: [peer] }
|
|
12
|
+
})
|
|
13
|
+
},
|
|
14
|
+
and: function (schema, dependency) {
|
|
15
|
+
schema.oneOf = [{ required: dependency.peers }]
|
|
16
|
+
schema.oneOf.push({
|
|
17
|
+
allOf: _.map(dependency.peers, (peer) => {
|
|
18
|
+
return { not: { required: [peer] } }
|
|
19
|
+
})
|
|
20
|
+
})
|
|
21
|
+
},
|
|
22
|
+
nand: function (schema, dependency) {
|
|
23
|
+
schema.not = { required: dependency.peers }
|
|
24
|
+
},
|
|
25
|
+
xor: function (schema, dependency) {
|
|
26
|
+
schema.if = {
|
|
27
|
+
propertyNames: { enum: dependency.peers },
|
|
28
|
+
minProperties: 2
|
|
29
|
+
}
|
|
30
|
+
schema.then = false
|
|
31
|
+
schema.else = {
|
|
32
|
+
oneOf: _.map(dependency.peers, (peer) => {
|
|
33
|
+
return { required: [peer] }
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
oxor: function (schema, dependency) {
|
|
38
|
+
schema.oneOf = _.map(dependency.peers, (peer) => {
|
|
39
|
+
return { required: [peer] }
|
|
40
|
+
})
|
|
41
|
+
schema.oneOf.push({
|
|
42
|
+
not: {
|
|
43
|
+
oneOf: _.map(combinations(dependency.peers, 1, 2), (combination) => {
|
|
44
|
+
return { required: combination }
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
},
|
|
49
|
+
with: function (schema, dependency) {
|
|
50
|
+
schema.dependentRequired = schema.dependentRequired || {}
|
|
51
|
+
schema.dependentRequired[dependency.key] = dependency.peers
|
|
52
|
+
},
|
|
53
|
+
without: function (schema, dependency) {
|
|
54
|
+
schema.if = { required: [dependency.key] }
|
|
55
|
+
schema.then = {
|
|
56
|
+
not: {
|
|
57
|
+
anyOf: _.map(dependency.peers, (peer) => {
|
|
58
|
+
return { required: [peer] }
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Recognize the `joi.override` representation in `describe()` output.
|
|
67
|
+
*
|
|
68
|
+
* `joi.override` is a Symbol that can be used in `joi.any().valid(…)`
|
|
69
|
+
* statements, to reset the list of valid values. In `describe()` output, it
|
|
70
|
+
* turns up as an object with 1 property:
|
|
71
|
+
*
|
|
72
|
+
* ```
|
|
73
|
+
* { override: true }
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
function isJoiOverride(e) {
|
|
77
|
+
return typeof e === 'object'
|
|
78
|
+
&& e !== null
|
|
79
|
+
&& Object.keys(e).length === 1
|
|
80
|
+
&& e.override === true
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
class JoiJsonSchemaParser {
|
|
84
|
+
constructor(opts = {}) {
|
|
85
|
+
this.$schema = opts.$schema || 'http://json-schema.org/draft-07/schema#'
|
|
86
|
+
this.includeSchemaDialect = opts.includeSchemaDialect || false
|
|
87
|
+
this.childrenFieldName = this._getChildrenFieldName()
|
|
88
|
+
this.optionsFieldName = this._getOptionsFieldName()
|
|
89
|
+
this.ruleArgFieldName = this._getRuleArgFieldName()
|
|
90
|
+
this.enumFieldName = this._getEnumFieldName()
|
|
91
|
+
this.allowUnknownFlagName = this._getAllowUnknownFlagName()
|
|
92
|
+
this.inheritedPreferences = []
|
|
93
|
+
|
|
94
|
+
if (opts.logicalOpParser === false) {
|
|
95
|
+
this.logicalOpParser = {}
|
|
96
|
+
} else {
|
|
97
|
+
this.logicalOpParser = _.merge({}, LOGICAL_OP_PARSER, opts.logicalOpParser)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
parse(joiSpec, definitions = {}, level = 0) {
|
|
102
|
+
let schema = {}
|
|
103
|
+
|
|
104
|
+
const hasPreferences = this._pushInheritiedPreferences(joiSpec)
|
|
105
|
+
|
|
106
|
+
if (this._getPresence(joiSpec) === 'forbidden') {
|
|
107
|
+
if (hasPreferences) {
|
|
108
|
+
this._popInheritiedPreferences()
|
|
109
|
+
}
|
|
110
|
+
schema.not = {}
|
|
111
|
+
return schema
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
this._setBasicProperties(schema, joiSpec)
|
|
115
|
+
this._setNumberFieldProperties(schema, joiSpec)
|
|
116
|
+
this._setBinaryFieldProperties(schema, joiSpec)
|
|
117
|
+
this._setStringFieldProperties(schema, joiSpec)
|
|
118
|
+
this._setDateFieldProperties(schema, joiSpec)
|
|
119
|
+
this._setArrayFieldProperties(schema, joiSpec, definitions, level)
|
|
120
|
+
this._setObjectProperties(schema, joiSpec, definitions, level)
|
|
121
|
+
this._setAlternativesProperties(schema, joiSpec, definitions, level)
|
|
122
|
+
this._setConditionProperties(schema, joiSpec, definitions, level, 'whens')
|
|
123
|
+
this._setMetaProperties(schema, joiSpec)
|
|
124
|
+
this._setLinkFieldProperties(schema, joiSpec)
|
|
125
|
+
this._setConst(schema, joiSpec)
|
|
126
|
+
this._setAnyProperties(schema, joiSpec, definitions, level)
|
|
127
|
+
this._addNullTypeIfNullable(schema, joiSpec)
|
|
128
|
+
|
|
129
|
+
if (!_.isEmpty(joiSpec.shared)) {
|
|
130
|
+
_.forEach(joiSpec.shared, (sharedSchema) => {
|
|
131
|
+
this.parse(sharedSchema, definitions, level)
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
this._copyMetasToSchema(joiSpec, schema)
|
|
136
|
+
|
|
137
|
+
// Last step to remove any unknown keys that are not in the JSON Schema specification or known meta keys
|
|
138
|
+
schema = this._retainKnownKeys(schema)
|
|
139
|
+
|
|
140
|
+
const schemaId = _.get(joiSpec, 'flags.id')
|
|
141
|
+
if (schemaId) {
|
|
142
|
+
definitions[schemaId] = schema
|
|
143
|
+
schema = {
|
|
144
|
+
$ref: `${this._getLocalSchemaBasePath()}/${schemaId}`
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (level === 0 && !_.isEmpty(definitions)) {
|
|
148
|
+
_.set(schema, `${this._getLocalSchemaBasePath().replace('#/', '').replace(/\//, '.')}`, definitions)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (this.includeSchemaDialect && level === 0 && this.$schema) {
|
|
152
|
+
schema.$schema = this.$schema
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (hasPreferences) {
|
|
156
|
+
this._popInheritiedPreferences()
|
|
157
|
+
}
|
|
158
|
+
return schema
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
_retainKnownKeys(schema) {
|
|
162
|
+
if (!this.knownKeys) {
|
|
163
|
+
return schema
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return _.pickBy(schema, (value, key) => this.knownKeys.includes(key) || this._isKnownMetaKey(key))
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
_pushInheritiedPreferences(joiSpec) {
|
|
170
|
+
if (joiSpec.preferences) {
|
|
171
|
+
this.inheritedPreferences.push(_.merge({}, this._getInheritedPreferences(), joiSpec.preferences))
|
|
172
|
+
return true
|
|
173
|
+
}
|
|
174
|
+
return false
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
_popInheritiedPreferences() {
|
|
178
|
+
this.inheritedPreferences.pop()
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
_getInheritedPreferences() {
|
|
182
|
+
return this.inheritedPreferences[this.inheritedPreferences.length - 1] || {}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
_isIfThenElseSupported() {
|
|
186
|
+
return true
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
_getChildrenFieldName() {
|
|
190
|
+
return 'keys'
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
_getOptionsFieldName() {
|
|
194
|
+
return 'preferences'
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
_getRuleArgFieldName() {
|
|
198
|
+
return 'args'
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
_getEnumFieldName() {
|
|
202
|
+
return 'allow'
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
_getAllowUnknownFlagName() {
|
|
206
|
+
return 'unknown'
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
_getLocalSchemaBasePath() {
|
|
210
|
+
return '#/$defs'
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
_getFieldDescription(fieldDefn) {
|
|
214
|
+
return _.get(fieldDefn, 'flags.description')
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
_getFieldType(fieldDefn) {
|
|
218
|
+
let type = fieldDefn.type
|
|
219
|
+
if (type === 'number' && !_.isEmpty(fieldDefn.rules) &&
|
|
220
|
+
fieldDefn.rules[0].name === 'integer') {
|
|
221
|
+
type = 'integer'
|
|
222
|
+
}
|
|
223
|
+
return type
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
_addNullTypeIfNullable(fieldSchema, fieldDefn) {
|
|
227
|
+
// This should always be the last call in parse
|
|
228
|
+
if (fieldSchema.const === null) {
|
|
229
|
+
return
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const enums = _.get(fieldDefn, this.enumFieldName)
|
|
233
|
+
if (Array.isArray(enums) && enums.includes(null)) {
|
|
234
|
+
if (Array.isArray(fieldSchema.type)) {
|
|
235
|
+
if (!fieldSchema.type.includes('null')) {
|
|
236
|
+
fieldSchema.type.push('null')
|
|
237
|
+
}
|
|
238
|
+
} else if (fieldSchema.type) {
|
|
239
|
+
fieldSchema.type = [fieldSchema.type, 'null']
|
|
240
|
+
} else {
|
|
241
|
+
fieldSchema.type = 'null'
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
_getFieldExample(fieldDefn) {
|
|
247
|
+
return _.get(fieldDefn, 'examples')
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
_getPresence(fieldDefn) {
|
|
251
|
+
const presence = _.get(fieldDefn, 'flags.presence')
|
|
252
|
+
if (presence !== undefined) {
|
|
253
|
+
return presence
|
|
254
|
+
}
|
|
255
|
+
const preferences = fieldDefn[this.optionsFieldName] || this._getInheritedPreferences()
|
|
256
|
+
return _.get(preferences, 'presence')
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
_isRequired(fieldDefn) {
|
|
260
|
+
const presence = this._getPresence(fieldDefn)
|
|
261
|
+
return presence === 'required'
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
_getDefaultValue(fieldDefn) {
|
|
265
|
+
return _.get(fieldDefn, 'flags.default')
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
_getEnum(fieldDefn) {
|
|
269
|
+
const enumList = fieldDefn[this.enumFieldName]
|
|
270
|
+
const filteredEnumList = enumList ? _.filter(enumList, e => !isJoiOverride(e)) : enumList
|
|
271
|
+
if (fieldDefn.flags && fieldDefn.flags.only && _.size(filteredEnumList) > 1) {
|
|
272
|
+
return _.uniq(filteredEnumList)
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
_getUnknown(joiSpec) {
|
|
277
|
+
const allowUnknown = _.get(joiSpec, `flags.${this.allowUnknownFlagName}`)
|
|
278
|
+
if (allowUnknown !== undefined) {
|
|
279
|
+
return allowUnknown
|
|
280
|
+
}
|
|
281
|
+
const preferences = joiSpec[this.optionsFieldName] || this._getInheritedPreferences()
|
|
282
|
+
return _.get(preferences, 'allowUnknown') || false
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
_setIfNotEmpty(schema, field, value) {
|
|
286
|
+
if (value !== null && value !== undefined) {
|
|
287
|
+
schema[field] = value
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
_setBasicProperties(fieldSchema, fieldDefn) {
|
|
292
|
+
this._setIfNotEmpty(fieldSchema, 'type', this._getFieldType(fieldDefn))
|
|
293
|
+
this._setIfNotEmpty(fieldSchema, 'examples', this._getFieldExample(fieldDefn))
|
|
294
|
+
this._setIfNotEmpty(fieldSchema, 'description', this._getFieldDescription(fieldDefn))
|
|
295
|
+
this._setIfNotEmpty(fieldSchema, 'default', this._getDefaultValue(fieldDefn))
|
|
296
|
+
this._setIfNotEmpty(fieldSchema, 'enum', this._getEnum(fieldDefn))
|
|
297
|
+
if (fieldDefn.invalid && fieldDefn.invalid.length > 0) {
|
|
298
|
+
fieldSchema.not = {
|
|
299
|
+
enum: fieldDefn.invalid
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
_setBinaryFieldProperties(fieldSchema, fieldDefn) {
|
|
305
|
+
if (fieldSchema.type !== 'binary') {
|
|
306
|
+
return
|
|
307
|
+
}
|
|
308
|
+
fieldSchema.type = 'string'
|
|
309
|
+
if (fieldDefn.flags && fieldDefn.flags.encoding) {
|
|
310
|
+
fieldSchema.contentEncoding = fieldDefn.flags.encoding
|
|
311
|
+
}
|
|
312
|
+
fieldSchema.format = 'binary'
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
_setObjectProperties(schema, joiSpec, definitions, level) {
|
|
316
|
+
if (schema.type !== 'object') {
|
|
317
|
+
return
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
schema.properties = {}
|
|
321
|
+
schema.required = []
|
|
322
|
+
|
|
323
|
+
schema.additionalProperties = this._getUnknown(joiSpec)
|
|
324
|
+
|
|
325
|
+
_.map(joiSpec[this.childrenFieldName], (fieldDefn, key) => {
|
|
326
|
+
const fieldSchema = this.parse(fieldDefn, definitions, level + 1)
|
|
327
|
+
if (this._isRequired(fieldDefn)) {
|
|
328
|
+
schema.required.push(key)
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
schema.properties[key] = fieldSchema
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* For dynamic key scenarios to store the pattern as key
|
|
336
|
+
* and have the properties be as with other examples
|
|
337
|
+
*/
|
|
338
|
+
if (joiSpec.patterns) {
|
|
339
|
+
_.each(joiSpec.patterns, patternObj => {
|
|
340
|
+
if (typeof patternObj.rule !== 'object' || typeof patternObj.regex === 'undefined') {
|
|
341
|
+
return
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
schema.properties[patternObj.regex] = this.parse(patternObj.rule, definitions, level + 1)
|
|
345
|
+
schema.patternProperties = schema.patternProperties || {}
|
|
346
|
+
|
|
347
|
+
let regexString = patternObj.regex
|
|
348
|
+
regexString = regexString.indexOf('/') === 0 ? regexString.substring(1) : regexString
|
|
349
|
+
regexString = regexString.lastIndexOf('/') > -1 ? regexString.substring(0, regexString.length - 1) : regexString
|
|
350
|
+
|
|
351
|
+
schema.patternProperties[regexString] = schema.properties[patternObj.regex]
|
|
352
|
+
})
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (_.isEmpty(schema.required)) {
|
|
356
|
+
delete schema.required
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
this._setObjectDependencies(schema, joiSpec)
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
_setObjectDependencies(schema, joiSpec) {
|
|
363
|
+
if (!_.isArray(joiSpec.dependencies) || joiSpec.dependencies.length === 0) {
|
|
364
|
+
return
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (joiSpec.dependencies.length === 1) {
|
|
368
|
+
this._setDependencySubSchema(schema, joiSpec.dependencies[0])
|
|
369
|
+
} else {
|
|
370
|
+
const withDependencies = _.remove(joiSpec.dependencies, (dependency) => {
|
|
371
|
+
return dependency.rel === 'with'
|
|
372
|
+
})
|
|
373
|
+
|
|
374
|
+
schema.allOf = _.compact(_.map(joiSpec.dependencies, (dependency) => {
|
|
375
|
+
const subSchema = this._setDependencySubSchema({}, dependency)
|
|
376
|
+
if (_.isEmpty(subSchema)) {
|
|
377
|
+
return null
|
|
378
|
+
}
|
|
379
|
+
return subSchema
|
|
380
|
+
}))
|
|
381
|
+
|
|
382
|
+
_.each(withDependencies, (dependency) => {
|
|
383
|
+
this._setDependencySubSchema(schema, dependency)
|
|
384
|
+
})
|
|
385
|
+
|
|
386
|
+
if (schema.allOf.length === 0) {
|
|
387
|
+
// When the logicalOpParser is set to false
|
|
388
|
+
delete schema.allOf
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
_setDependencySubSchema(schema, dependency) {
|
|
394
|
+
const opParser = this.logicalOpParser[dependency.rel]
|
|
395
|
+
if (typeof opParser !== 'function') {
|
|
396
|
+
return schema
|
|
397
|
+
}
|
|
398
|
+
opParser(schema, dependency)
|
|
399
|
+
return schema
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
_setNumberFieldProperties(fieldSchema, fieldDefn) {
|
|
403
|
+
if (fieldSchema.type !== 'number' && fieldSchema.type !== 'integer') {
|
|
404
|
+
return
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const ruleArgFieldName = this.ruleArgFieldName
|
|
408
|
+
|
|
409
|
+
_.each(fieldDefn.rules, (rule) => {
|
|
410
|
+
const value = rule[ruleArgFieldName]
|
|
411
|
+
switch (rule.name) {
|
|
412
|
+
case 'max':
|
|
413
|
+
fieldSchema.maximum = value.limit
|
|
414
|
+
break
|
|
415
|
+
case 'min':
|
|
416
|
+
fieldSchema.minimum = value.limit
|
|
417
|
+
break
|
|
418
|
+
case 'greater':
|
|
419
|
+
fieldSchema.exclusiveMinimum = value.limit
|
|
420
|
+
fieldSchema.minimum = value.limit
|
|
421
|
+
break
|
|
422
|
+
case 'less':
|
|
423
|
+
fieldSchema.exclusiveMaximum = value.limit
|
|
424
|
+
fieldSchema.maximum = value.limit
|
|
425
|
+
break
|
|
426
|
+
case 'multiple':
|
|
427
|
+
fieldSchema.multipleOf = value.base
|
|
428
|
+
break
|
|
429
|
+
case 'sign':
|
|
430
|
+
if (rule.args.sign === 'positive') {
|
|
431
|
+
fieldSchema.exclusiveMinimum = 0
|
|
432
|
+
} else if (rule.args.sign === 'negative') {
|
|
433
|
+
fieldSchema.exclusiveMaximum = 0
|
|
434
|
+
}
|
|
435
|
+
break
|
|
436
|
+
default:
|
|
437
|
+
break
|
|
438
|
+
}
|
|
439
|
+
})
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
_setStringFieldProperties(fieldSchema, fieldDefn) {
|
|
443
|
+
if (fieldSchema.type !== 'string') {
|
|
444
|
+
return
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
if (fieldDefn.flags && fieldDefn.flags.encoding) {
|
|
448
|
+
fieldSchema.contentEncoding = fieldDefn.flags.encoding
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
const ruleArgFieldName = this.ruleArgFieldName
|
|
452
|
+
|
|
453
|
+
_.forEach(fieldDefn.rules, (rule) => {
|
|
454
|
+
switch (rule.name) {
|
|
455
|
+
case 'min':
|
|
456
|
+
fieldSchema.minLength = rule[ruleArgFieldName].limit
|
|
457
|
+
break
|
|
458
|
+
case 'max':
|
|
459
|
+
fieldSchema.maxLength = rule[ruleArgFieldName].limit
|
|
460
|
+
break
|
|
461
|
+
case 'email':
|
|
462
|
+
fieldSchema.format = 'email'
|
|
463
|
+
break
|
|
464
|
+
case 'hostname':
|
|
465
|
+
fieldSchema.format = 'hostname'
|
|
466
|
+
break
|
|
467
|
+
case 'uri':
|
|
468
|
+
fieldSchema.format = 'uri'
|
|
469
|
+
break
|
|
470
|
+
case 'ip':
|
|
471
|
+
const versions = rule[ruleArgFieldName].options.version
|
|
472
|
+
if (!_.isEmpty(versions)) {
|
|
473
|
+
if (versions.length === 1) {
|
|
474
|
+
fieldSchema.format = versions[0]
|
|
475
|
+
} else {
|
|
476
|
+
fieldSchema.oneOf = _.map(versions, (version) => {
|
|
477
|
+
return {
|
|
478
|
+
format: version
|
|
479
|
+
}
|
|
480
|
+
})
|
|
481
|
+
}
|
|
482
|
+
} else {
|
|
483
|
+
fieldSchema.format = 'ipv4'
|
|
484
|
+
}
|
|
485
|
+
break
|
|
486
|
+
case 'pattern':
|
|
487
|
+
let regex = rule[ruleArgFieldName].regex
|
|
488
|
+
let idx = regex.indexOf('/')
|
|
489
|
+
if (idx === 0) {
|
|
490
|
+
regex = regex.replace('/', '')
|
|
491
|
+
}
|
|
492
|
+
idx = regex.lastIndexOf('/') === regex.length - 1
|
|
493
|
+
if (idx > -1) {
|
|
494
|
+
regex = regex.replace(/\/$/, '')
|
|
495
|
+
}
|
|
496
|
+
fieldSchema.pattern = regex
|
|
497
|
+
break
|
|
498
|
+
case 'isoDate':
|
|
499
|
+
fieldSchema.format = 'date-time'
|
|
500
|
+
break
|
|
501
|
+
case 'isoDuration':
|
|
502
|
+
fieldSchema.format = 'duration'
|
|
503
|
+
break
|
|
504
|
+
case 'uuid':
|
|
505
|
+
case 'guid':
|
|
506
|
+
fieldSchema.format = 'uuid'
|
|
507
|
+
break
|
|
508
|
+
default:
|
|
509
|
+
break
|
|
510
|
+
}
|
|
511
|
+
})
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
_setArrayFieldProperties(fieldSchema, fieldDefn, definitions, level) {
|
|
515
|
+
if (fieldSchema.type !== 'array') {
|
|
516
|
+
return
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const ruleArgFieldName = this.ruleArgFieldName
|
|
520
|
+
|
|
521
|
+
_.each(fieldDefn.rules, (rule) => {
|
|
522
|
+
const value = rule[ruleArgFieldName]
|
|
523
|
+
switch (rule.name) {
|
|
524
|
+
case 'max':
|
|
525
|
+
fieldSchema.maxItems = value.limit
|
|
526
|
+
break
|
|
527
|
+
case 'min':
|
|
528
|
+
fieldSchema.minItems = value.limit
|
|
529
|
+
break
|
|
530
|
+
case 'length':
|
|
531
|
+
fieldSchema.maxItems = value.limit
|
|
532
|
+
fieldSchema.minItems = value.limit
|
|
533
|
+
break
|
|
534
|
+
case 'unique':
|
|
535
|
+
fieldSchema.uniqueItems = true
|
|
536
|
+
break
|
|
537
|
+
case 'has':
|
|
538
|
+
fieldSchema.contains = this.parse(value.schema, definitions, level + 1)
|
|
539
|
+
break
|
|
540
|
+
default:
|
|
541
|
+
break
|
|
542
|
+
}
|
|
543
|
+
})
|
|
544
|
+
|
|
545
|
+
if (!fieldDefn.items) {
|
|
546
|
+
fieldSchema.items = {}
|
|
547
|
+
return
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
if (fieldDefn.items.length === 1) {
|
|
551
|
+
fieldSchema.items = this.parse(fieldDefn.items[0], definitions, level + 1)
|
|
552
|
+
} else {
|
|
553
|
+
fieldSchema.items = {
|
|
554
|
+
anyOf: _.map(fieldDefn.items, (itemSchema) => {
|
|
555
|
+
return this.parse(itemSchema, definitions, level + 1)
|
|
556
|
+
})
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
_setDateFieldProperties(fieldSchema, fieldDefn) {
|
|
562
|
+
if (fieldSchema.type !== 'date') {
|
|
563
|
+
return
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
if (fieldDefn.flags && fieldDefn.flags.format !== 'iso') {
|
|
567
|
+
fieldSchema.type = 'integer'
|
|
568
|
+
} else {
|
|
569
|
+
// https://datatracker.ietf.org/doc/draft-handrews-json-schema-validation
|
|
570
|
+
// JSON Schema does not have date type, but use string with format.
|
|
571
|
+
// However, joi definition cannot clearly tells the date/time/date-time format
|
|
572
|
+
fieldSchema.type = 'string'
|
|
573
|
+
fieldSchema.format = 'date-time'
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
_setAlternativesProperties(schema, joiSpec, definitions, level) {
|
|
578
|
+
if (schema.type !== 'alternatives' || joiSpec.matches.length === 0) {
|
|
579
|
+
return
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
if (joiSpec.matches[0].schema) {
|
|
583
|
+
// try style
|
|
584
|
+
let mode = 'anyOf'
|
|
585
|
+
if (joiSpec.flags && joiSpec.flags.match === 'one') {
|
|
586
|
+
mode = 'oneOf'
|
|
587
|
+
} else if (joiSpec.flags && joiSpec.flags.match === 'all') {
|
|
588
|
+
mode = 'allOf'
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
schema[mode] = _.map(joiSpec.matches, (match) => {
|
|
592
|
+
return this.parse(match.schema, definitions, level + 1)
|
|
593
|
+
})
|
|
594
|
+
|
|
595
|
+
if (schema[mode].length === 1) {
|
|
596
|
+
_.merge(schema, schema[mode][0])
|
|
597
|
+
delete schema[mode]
|
|
598
|
+
}
|
|
599
|
+
} else {
|
|
600
|
+
this._setConditionProperties(schema, joiSpec, definitions, level, 'matches', 'anyOf')
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
if (schema.type === 'alternatives') {
|
|
604
|
+
delete schema.type
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
_setConditionProperties(schema, joiSpec, definitions, level, conditionFieldName, logicKeyword = 'allOf') {
|
|
609
|
+
if (!joiSpec[conditionFieldName] || joiSpec[conditionFieldName].length === 0) {
|
|
610
|
+
return
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
let ifThenStyle = this._isIfThenElseSupported()
|
|
614
|
+
const styleSetting = _.remove(joiSpec.metas, (meta) => {
|
|
615
|
+
return typeof meta['if-style'] !== 'undefined'
|
|
616
|
+
})
|
|
617
|
+
|
|
618
|
+
if (ifThenStyle && styleSetting.length > 0 && styleSetting[0]['if-style'] === false) {
|
|
619
|
+
ifThenStyle = false
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
if (joiSpec[conditionFieldName].length > 1) {
|
|
623
|
+
// Multiple case
|
|
624
|
+
schema[logicKeyword] = _.map(joiSpec[conditionFieldName], (condition) => {
|
|
625
|
+
return this._setConditionSchema(ifThenStyle, {}, condition, definitions, level + 1)
|
|
626
|
+
})
|
|
627
|
+
} else {
|
|
628
|
+
this._setConditionSchema(ifThenStyle, schema, joiSpec[conditionFieldName][0], definitions, level + 1)
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
_setConditionSchema(ifThenStyle, schema, conditionJoiSpec, definitions, level) {
|
|
633
|
+
// When "if" is not present, both "then" and "else" MUST be entirely ignored.
|
|
634
|
+
// There must be either "is" or "switch"
|
|
635
|
+
if (!conditionJoiSpec.is && !conditionJoiSpec.switch) {
|
|
636
|
+
return
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
if (conditionJoiSpec.switch) {
|
|
640
|
+
this._parseSwitchCondition(ifThenStyle, schema, conditionJoiSpec, definitions, level)
|
|
641
|
+
} else {
|
|
642
|
+
if (ifThenStyle) {
|
|
643
|
+
this._parseIfThenElseCondition(schema, conditionJoiSpec, definitions, level)
|
|
644
|
+
} else {
|
|
645
|
+
this._setConditionCompositionStyle(schema, conditionJoiSpec, definitions, level)
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
return schema
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
_parseIfThenElseCondition(schema, conditionSpec, definitions, level) {
|
|
653
|
+
if (conditionSpec.ref) {
|
|
654
|
+
// Currently, if there is reference, if-then-else style is not supported
|
|
655
|
+
// To use it, the condition must be defined in parent level using schema-style is
|
|
656
|
+
return this._setConditionCompositionStyle(schema, conditionSpec, definitions, level)
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
schema.if = this.parse(conditionSpec.is, definitions, level)
|
|
660
|
+
|
|
661
|
+
if (conditionSpec.then) {
|
|
662
|
+
schema.then = this.parse(conditionSpec.then, definitions, level)
|
|
663
|
+
}
|
|
664
|
+
if (conditionSpec.otherwise) {
|
|
665
|
+
schema.else = this.parse(conditionSpec.otherwise, definitions, level)
|
|
666
|
+
}
|
|
667
|
+
return schema
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
_parseSwitchCondition(ifThenStyle, schema, condition, definitions, level) {
|
|
671
|
+
// Switch cannot be used if the joi condition is a schema
|
|
672
|
+
// Hence, condition.ref should always exists
|
|
673
|
+
const anyOf = []
|
|
674
|
+
for (const switchCondition of condition.switch) {
|
|
675
|
+
if (ifThenStyle && !condition.ref) {
|
|
676
|
+
const innerSchema = this._parseIfThenElseCondition(
|
|
677
|
+
{}, switchCondition, definitions, level + 1)
|
|
678
|
+
|
|
679
|
+
anyOf.push(innerSchema)
|
|
680
|
+
} else {
|
|
681
|
+
if (switchCondition.then) {
|
|
682
|
+
anyOf.push(this.parse(switchCondition.then, definitions, level + 1))
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
if (switchCondition.otherwise) {
|
|
686
|
+
anyOf.push(this.parse(switchCondition.otherwise, definitions, level + 1))
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
if (anyOf.length > 1) {
|
|
692
|
+
schema.anyOf = anyOf
|
|
693
|
+
} else {
|
|
694
|
+
_.merge(schema, oneOf[0])
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
_setConditionCompositionStyle(schema, condition, definitions, level) {
|
|
699
|
+
if (condition.ref) {
|
|
700
|
+
// If the condition is refering to other field, cannot use the `is` condition for composition
|
|
701
|
+
// Simple choise between then / otherwise
|
|
702
|
+
const oneOf = schema.oneOf || []
|
|
703
|
+
if (condition.then) {
|
|
704
|
+
oneOf.push(this.parse(condition.then, definitions, level + 1))
|
|
705
|
+
}
|
|
706
|
+
if (condition.otherwise) {
|
|
707
|
+
oneOf.push(this.parse(condition.otherwise, definitions, level + 1))
|
|
708
|
+
}
|
|
709
|
+
if (oneOf.length > 1) {
|
|
710
|
+
schema.oneOf = oneOf
|
|
711
|
+
} else {
|
|
712
|
+
_.merge(schema, oneOf[0])
|
|
713
|
+
}
|
|
714
|
+
} else {
|
|
715
|
+
// Before Draft 7, you can express an “if-then” conditional using the Schema Composition keywords
|
|
716
|
+
// and a boolean algebra concept called “implication”.
|
|
717
|
+
// A -> B(pronounced, A implies B) means that if A is true, then B must also be true.
|
|
718
|
+
// It can be expressed as !A || B
|
|
719
|
+
|
|
720
|
+
// Variations of implication can express the same things you can express with the if/then/else keywords.
|
|
721
|
+
// if/then can be expressed as A -> B, if/else can be expressed as !A -> B,
|
|
722
|
+
// and if/then/else can be expressed as A -> B AND !A -> C
|
|
723
|
+
if (condition.is && condition.then && condition.otherwise) {
|
|
724
|
+
schema.allOf = [
|
|
725
|
+
{
|
|
726
|
+
anyOf: [
|
|
727
|
+
{
|
|
728
|
+
not: this.parse(condition.is, definitions, level + 1)
|
|
729
|
+
},
|
|
730
|
+
this.parse(condition.then, definitions, level + 1)
|
|
731
|
+
]
|
|
732
|
+
},
|
|
733
|
+
{
|
|
734
|
+
anyOf: [
|
|
735
|
+
this.parse(condition.is, definitions, level + 1),
|
|
736
|
+
this.parse(condition.otherwise, definitions, level + 1)
|
|
737
|
+
]
|
|
738
|
+
}
|
|
739
|
+
]
|
|
740
|
+
} else if (condition.is && condition.then) {
|
|
741
|
+
schema.anyOf = [
|
|
742
|
+
{
|
|
743
|
+
not: this.parse(condition.is, definitions, level + 1)
|
|
744
|
+
},
|
|
745
|
+
this.parse(condition.then, definitions, level + 1)
|
|
746
|
+
]
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
return schema
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
_setConst(schema, fieldDefn) {
|
|
754
|
+
const enumList = fieldDefn[this.enumFieldName]
|
|
755
|
+
const filteredEnumList = enumList ? _.filter(enumList, e => !isJoiOverride(e)) : enumList
|
|
756
|
+
if (fieldDefn.flags && fieldDefn.flags.only && _.size(filteredEnumList) === 1) {
|
|
757
|
+
schema.const = filteredEnumList[0]
|
|
758
|
+
delete schema.type
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
_setAnyProperties(schema) {
|
|
763
|
+
if (schema.type !== 'any') {
|
|
764
|
+
return
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
schema.type = [
|
|
768
|
+
'array',
|
|
769
|
+
'boolean',
|
|
770
|
+
'number',
|
|
771
|
+
'object',
|
|
772
|
+
'string',
|
|
773
|
+
'null'
|
|
774
|
+
]
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
_setMetaProperties(schema, joiSpec) {
|
|
778
|
+
_.forEach(joiSpec.metas, (m) => {
|
|
779
|
+
if (m.contentMediaType) {
|
|
780
|
+
schema.contentMediaType = m.contentMediaType
|
|
781
|
+
}
|
|
782
|
+
if (m.format) {
|
|
783
|
+
schema.format = m.format
|
|
784
|
+
}
|
|
785
|
+
if (m.title) {
|
|
786
|
+
schema.title = m.title
|
|
787
|
+
}
|
|
788
|
+
})
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
_setLinkFieldProperties(schema, joiSpec) {
|
|
792
|
+
if (schema.type !== 'link') {
|
|
793
|
+
return
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
if (_.get(joiSpec, 'link.ref.type') === 'local') {
|
|
797
|
+
schema.$ref = `${this._getLocalSchemaBasePath()}/${joiSpec.link.ref.path.join('/')}`
|
|
798
|
+
delete schema.type
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
_copyMetasToSchema(joiSpec, schema) {
|
|
803
|
+
if (!_.isEmpty(joiSpec.metas)) {
|
|
804
|
+
_.each(joiSpec.metas, meta => {
|
|
805
|
+
_.each(meta, (value, key) => {
|
|
806
|
+
if (this._isKnownMetaKey(key)) {
|
|
807
|
+
schema[key] = value
|
|
808
|
+
}
|
|
809
|
+
})
|
|
810
|
+
})
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
// Intended to be overridden by child parsers. By default no meta key will be
|
|
815
|
+
// is considered "known".
|
|
816
|
+
// eslint-disable-next-line no-unused-vars
|
|
817
|
+
_isKnownMetaKey(key) {
|
|
818
|
+
return false
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
module.exports = JoiJsonSchemaParser
|