forked from kenspirit/joi-to-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (55 loc) · 2.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
const cmp = require('semver-compare')
const c17 = require('./lib/convertors/v17')
const c16 = require('./lib/convertors/v16')
const c15 = require('./lib/convertors/v15')
const c14 = require('./lib/convertors/v14')
const c13 = require('./lib/convertors/v13')
const c12 = require('./lib/convertors/v12')
const JoiJsonSchemaParser = require('./lib/parsers/json')
const JoiOpenApiSchemaParser = require('./lib/parsers/open-api')
const JoiJsonDraftSchemaParser19 = require('./lib/parsers/json-draft-2019-09')
const JoiJsonDraftSchemaParser = require('./lib/parsers/json-draft-04')
const convertors = [
c17, c16, c15, c14, c13, c12
]
const parsers = {
'json-draft-2019-09': JoiJsonDraftSchemaParser19,
'json-draft-04': JoiJsonDraftSchemaParser,
json: JoiJsonSchemaParser,
'open-api': JoiOpenApiSchemaParser
}
function parse(joiObj, type = 'json', definitions = {}, parserOptions = {}) {
if (typeof joiObj.describe !== 'function') {
throw new Error('Not an joi object.')
}
let convertor
for (let i = 0; i < convertors.length; i++) {
const tmpConvertor = convertors[i]
try {
let version = tmpConvertor.getVersion(joiObj)
let result = cmp(tmpConvertor.getSupportVersion(), version)
if (result <= 0) {
// The first parser has smaller or equal version
convertor = tmpConvertor
break
}
} catch (e) {
// Format does not match this parser version.
// Skip to check the next one
continue
}
}
if (!convertor) {
console.warn('No matched joi version convertor found, using the latest version')
convertor = convertors[0]
}
// fs.writeFileSync('./joi_spec.json', JSON.stringify(joiObj.describe(), null, 2))
const joiBaseSpec = new convertor().toBaseSpec(joiObj.describe())
// fs.writeFileSync(`./internal_${convertor.getSupportVersion()}_${type}.json`, JSON.stringify(joiBaseSpec, null, 2))
const parser = parsers[type]
if (!parser) {
throw new Error(`No parser is registered for ${type}`)
}
return new parser(parserOptions).parse(joiBaseSpec, definitions)
}
module.exports = parse