joi-to-json 2.2.0 → 2.2.1

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/index.js CHANGED
@@ -51,6 +51,7 @@ function parse(joiObj, type = 'json') {
51
51
  }
52
52
 
53
53
  const joiBaseSpec = new convertor().toBaseSpec(joiObj.describe())
54
+ // fs.writeFileSync(`./internal_${convertor.getSupportVersion()}_${type}.json`, JSON.stringify(joiBaseSpec, null, 2))
54
55
  const parser = parsers[type]
55
56
  if (!parser) {
56
57
  throw new Error(`No parser is registered for ${type}`)
@@ -37,7 +37,13 @@ class JoiSpecConvertor {
37
37
  }
38
38
 
39
39
  _convertAlternatives(joiObj) {
40
- if (joiObj.base && joiObj.base.type === 'any') {
40
+ let isWhenCase = false
41
+ if (joiObj.alternatives.length === 1) {
42
+ const condition = joiObj.alternatives[0]
43
+ isWhenCase = !!condition.then || !!condition.otherwise
44
+ }
45
+
46
+ if (isWhenCase) {
41
47
  // when case
42
48
  joiObj.type = 'any'
43
49
  joiObj.whens = joiObj.alternatives
@@ -354,10 +354,17 @@ class JoiJsonSchemaParser {
354
354
  return
355
355
  }
356
356
 
357
- if (joiSpec.matches.length === 1 && joiSpec.matches[0].switch) {
358
- schema.oneOf = _.map(joiSpec.matches[0].switch, (condition) => {
359
- return this.parse(condition.then || condition.otherwise)
360
- })
357
+ if (joiSpec.matches.length === 1) {
358
+ const match = joiSpec.matches[0]
359
+ if (match.switch) {
360
+ schema.oneOf = _.map(match.switch, (condition) => {
361
+ return this.parse(condition.then || condition.otherwise)
362
+ })
363
+ } else if (match.then || match.otherwise) {
364
+ schema.oneOf = []
365
+ if (match.then) schema.oneOf.push(this.parse(match.then))
366
+ if (match.otherwise) schema.oneOf.push(this.parse(match.otherwise))
367
+ }
361
368
  } else {
362
369
  schema.oneOf = _.map(joiSpec.matches, (match) => {
363
370
  return this.parse(match.schema)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "joi-to-json",
3
- "version": "2.2.0",
3
+ "version": "2.2.1",
4
4
  "description": "joi to JSON / OpenAPI Schema Converter",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -27,10 +27,10 @@
27
27
  "semver-compare": "^1.0.0"
28
28
  },
29
29
  "devDependencies": {
30
- "ajv": "^8.6.3",
30
+ "ajv": "^8.11.0",
31
31
  "ajv-draft-04": "^1.0.0",
32
- "eslint": "^7.32.0",
33
- "jest": "^27.2.0",
32
+ "eslint": "^8.12.0",
33
+ "jest": "^27.5.1",
34
34
  "joi-12": "npm:@commercial/joi@^12.1.0",
35
35
  "joi-13": "npm:joi@^13.7.0",
36
36
  "joi-14": "npm:joi@^14.3.1",