joi-to-json 2.3.5 → 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/README.md CHANGED
@@ -95,6 +95,13 @@ const jsonSchema = parse(joiSchema)
95
95
  // const openApiSchema = parse(joiSchema, 'open-api')
96
96
  ```
97
97
 
98
+ ## Browser support
99
+ For generating JSON Schema in a browser you should use below import syntax for `joi` library in order to work because the `joi` browser minimized build does not have `describe` api which the `joi-to-json` relies on.
100
+
101
+ ```typescript
102
+ import Joi from 'joi/lib/index';
103
+ ```
104
+
98
105
  ## Test
99
106
 
100
107
  >npm run test
package/index.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import Joi from 'joi-17';
2
+
3
+ declare module Joi2Json {
4
+ /**
5
+ * @type {string}
6
+ */
7
+ export type Mode = 'json' | 'open-api' | 'json-draft-2019-09' | 'json-draft-04';
8
+
9
+ /**
10
+ * @param {string} joi - A Joi schema.
11
+ * @param {string} [mode='json'] - json / open-api / json-draft-2019-09 / json-draft-04
12
+ * @param {Record} [sharedSchema={}] - Passed-in object storing shared schemas
13
+ * @returns {any} Converted JSON schema object.
14
+ */
15
+ export function parse(joi: Joi.Schema, mode?: Mode, sharedSchema?: Record<string, any>): any;
16
+ }
17
+
18
+ export default Joi2Json.parse;
package/index.js CHANGED
@@ -1,25 +1,26 @@
1
1
  const cmp = require('semver-compare')
2
- const fs = require('fs')
3
- const path = require('path')
4
2
 
5
- const convertorsDir = path.resolve(__dirname, './lib/convertors')
6
- const parsersDir = path.resolve(__dirname, './lib/parsers')
7
- const convertors = []
8
- const parsers = {}
3
+ const c17 = require('./lib/convertors/v17')
4
+ const c16 = require('./lib/convertors/v16')
5
+ const c15 = require('./lib/convertors/v15')
6
+ const c14 = require('./lib/convertors/v14')
7
+ const c13 = require('./lib/convertors/v13')
8
+ const c12 = require('./lib/convertors/v12')
9
9
 
10
- fs.readdirSync(convertorsDir).sort().reverse().forEach(file => {
11
- if (file.endsWith('.js')) {
12
- const convertor = require(`${convertorsDir}/${file}`)
13
- convertors.push(convertor)
14
- }
15
- })
10
+ const JoiJsonSchemaParser = require('./lib/parsers/json')
11
+ const JoiOpenApiSchemaParser = require('./lib/parsers/open-api')
12
+ const JoiJsonDraftSchemaParser19 = require('./lib/parsers/json-draft-2019-09')
13
+ const JoiJsonDraftSchemaParser = require('./lib/parsers/json-draft-04')
16
14
 
17
- fs.readdirSync(parsersDir).forEach(file => {
18
- if (file.endsWith('.js')) {
19
- const parser = require(`${parsersDir}/${file}`)
20
- parsers[file.split('.')[0]] = parser
21
- }
22
- })
15
+ const convertors = [
16
+ c17, c16, c15, c14, c13, c12
17
+ ]
18
+ const parsers = {
19
+ 'json-draft-2019-09': JoiJsonDraftSchemaParser19,
20
+ 'json-draft-4': JoiJsonDraftSchemaParser,
21
+ json: JoiJsonSchemaParser,
22
+ 'open-api': JoiOpenApiSchemaParser
23
+ }
23
24
 
24
25
  function parse(joiObj, type = 'json', definitions = {}) {
25
26
  if (typeof joiObj.describe !== 'function') {
@@ -420,14 +420,30 @@ class JoiJsonSchemaParser {
420
420
  }
421
421
 
422
422
  if (joiSpec.whens) {
423
- const condition = joiSpec.whens[0]
424
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
+
425
439
  if (condition.then) {
426
440
  schema.oneOf.push(this.parse(condition.then, definitions, level + 1))
427
441
  }
442
+
428
443
  if (condition.otherwise) {
429
444
  schema.oneOf.push(this.parse(condition.otherwise, definitions, level + 1))
430
445
  }
446
+
431
447
  delete schema.type
432
448
  return
433
449
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "joi-to-json",
3
- "version": "2.3.5",
3
+ "version": "2.5.0",
4
4
  "description": "joi to JSON / OpenAPI Schema Converter",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -36,10 +36,12 @@
36
36
  "joi-14": "npm:joi@^14.3.1",
37
37
  "joi-15": "npm:@hapi/joi@^15.1.1",
38
38
  "joi-16": "npm:@hapi/joi@^16.1.8",
39
- "joi-17": "npm:joi@^17.4.2"
39
+ "joi-17": "npm:joi@^17.6.0"
40
40
  },
41
+ "types": "index.d.ts",
41
42
  "files": [
42
43
  "lib/**/*.js",
43
- "index.js"
44
+ "index.js",
45
+ "index.d.ts"
44
46
  ]
45
47
  }