joi-to-json 2.3.3 → 2.3.6
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 +26 -2
- package/lib/parsers/open-api.js +4 -0
- package/package.json +1 -1
package/lib/parsers/json.js
CHANGED
|
@@ -115,9 +115,16 @@ class JoiJsonSchemaParser {
|
|
|
115
115
|
return _.get(fieldDefn, 'flags.default')
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
+
_getConst(fieldDefn) {
|
|
119
|
+
const enumList = fieldDefn[this.enumFieldName]
|
|
120
|
+
if (fieldDefn.flags && fieldDefn.flags.only && _.size(enumList) === 1) {
|
|
121
|
+
return enumList[0]
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
118
125
|
_getEnum(fieldDefn) {
|
|
119
126
|
const enumList = fieldDefn[this.enumFieldName]
|
|
120
|
-
if (fieldDefn.flags && fieldDefn.flags.only &&
|
|
127
|
+
if (fieldDefn.flags && fieldDefn.flags.only && _.size(enumList) > 1) {
|
|
121
128
|
return _.uniq(enumList)
|
|
122
129
|
}
|
|
123
130
|
}
|
|
@@ -141,6 +148,7 @@ class JoiJsonSchemaParser {
|
|
|
141
148
|
this._setIfNotEmpty(fieldSchema, 'examples', this._getFieldExample(fieldDefn))
|
|
142
149
|
this._setIfNotEmpty(fieldSchema, 'description', this._getFieldDescription(fieldDefn))
|
|
143
150
|
this._setIfNotEmpty(fieldSchema, 'default', this._getDefaultValue(fieldDefn))
|
|
151
|
+
this._setIfNotEmpty(fieldSchema, 'const', this._getConst(fieldDefn))
|
|
144
152
|
this._setIfNotEmpty(fieldSchema, 'enum', this._getEnum(fieldDefn))
|
|
145
153
|
}
|
|
146
154
|
|
|
@@ -412,14 +420,30 @@ class JoiJsonSchemaParser {
|
|
|
412
420
|
}
|
|
413
421
|
|
|
414
422
|
if (joiSpec.whens) {
|
|
415
|
-
const condition = joiSpec.whens[0]
|
|
416
423
|
schema.oneOf = []
|
|
424
|
+
|
|
425
|
+
const condition = joiSpec.whens[0]
|
|
426
|
+
|
|
427
|
+
if (condition.switch) {
|
|
428
|
+
for (const switchCondition of condition.switch) {
|
|
429
|
+
if (switchCondition.then) {
|
|
430
|
+
schema.oneOf.push(this.parse(switchCondition.then, definitions, level + 1))
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
if (switchCondition.otherwise) {
|
|
434
|
+
schema.oneOf.push(this.parse(switchCondition.otherwise, definitions, level + 1))
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
417
439
|
if (condition.then) {
|
|
418
440
|
schema.oneOf.push(this.parse(condition.then, definitions, level + 1))
|
|
419
441
|
}
|
|
442
|
+
|
|
420
443
|
if (condition.otherwise) {
|
|
421
444
|
schema.oneOf.push(this.parse(condition.otherwise, definitions, level + 1))
|
|
422
445
|
}
|
|
446
|
+
|
|
423
447
|
delete schema.type
|
|
424
448
|
return
|
|
425
449
|
}
|
package/lib/parsers/open-api.js
CHANGED