joi-to-json 4.3.0 → 4.3.2
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 +346 -346
- package/docs/logical_rel_support.md +241 -241
- package/index.d.ts +36 -36
- package/index.js +67 -67
- package/lib/convertors/v12.js +330 -330
- package/lib/convertors/v16.js +32 -32
- package/lib/parsers/json-draft-04.js +74 -74
- package/lib/parsers/json-draft-2019-09.js +16 -16
- package/lib/parsers/json.js +785 -801
- package/lib/parsers/open-api-3.1.js +31 -31
- package/lib/parsers/open-api.js +133 -133
- package/package.json +50 -50
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
const _ = require('lodash')
|
|
2
|
-
const JoiJsonSchemaParser = require('./json-draft-2019-09')
|
|
3
|
-
|
|
4
|
-
class JoiOpenApiSchemaParser extends JoiJsonSchemaParser {
|
|
5
|
-
constructor(opts = {}) {
|
|
6
|
-
super(opts)
|
|
7
|
-
|
|
8
|
-
this.$schema = undefined
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
parse(joiSpec, definitions = {}, level = 0) {
|
|
12
|
-
const schema = super.parse(joiSpec, definitions, level)
|
|
13
|
-
|
|
14
|
-
if (level === 0 && !_.isEmpty(definitions)) {
|
|
15
|
-
schema.schemas = definitions
|
|
16
|
-
}
|
|
17
|
-
delete schema.components
|
|
18
|
-
|
|
19
|
-
return schema
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
_getLocalSchemaBasePath() {
|
|
23
|
-
return '#/components/schemas'
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
_isKnownMetaKey(key) {
|
|
27
|
-
return key.startsWith('x-') || key === 'deprecated' || key === 'readOnly' || key === 'writeOnly'
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
module.exports = JoiOpenApiSchemaParser
|
|
1
|
+
const _ = require('lodash')
|
|
2
|
+
const JoiJsonSchemaParser = require('./json-draft-2019-09')
|
|
3
|
+
|
|
4
|
+
class JoiOpenApiSchemaParser extends JoiJsonSchemaParser {
|
|
5
|
+
constructor(opts = {}) {
|
|
6
|
+
super(opts)
|
|
7
|
+
|
|
8
|
+
this.$schema = undefined
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
parse(joiSpec, definitions = {}, level = 0) {
|
|
12
|
+
const schema = super.parse(joiSpec, definitions, level)
|
|
13
|
+
|
|
14
|
+
if (level === 0 && !_.isEmpty(definitions)) {
|
|
15
|
+
schema.schemas = definitions
|
|
16
|
+
}
|
|
17
|
+
delete schema.components
|
|
18
|
+
|
|
19
|
+
return schema
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
_getLocalSchemaBasePath() {
|
|
23
|
+
return '#/components/schemas'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
_isKnownMetaKey(key) {
|
|
27
|
+
return key.startsWith('x-') || key === 'deprecated' || key === 'readOnly' || key === 'writeOnly'
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = JoiOpenApiSchemaParser
|
package/lib/parsers/open-api.js
CHANGED
|
@@ -1,134 +1,134 @@
|
|
|
1
|
-
const _ = require('lodash')
|
|
2
|
-
const JoiJsonSchemaParser = require('./json-draft-04')
|
|
3
|
-
|
|
4
|
-
class JoiOpenApiSchemaParser extends JoiJsonSchemaParser {
|
|
5
|
-
constructor(opts = {}) {
|
|
6
|
-
super(_.merge({
|
|
7
|
-
logicalOpParser: {
|
|
8
|
-
xor: null,
|
|
9
|
-
with: null,
|
|
10
|
-
without: null
|
|
11
|
-
}
|
|
12
|
-
}, opts))
|
|
13
|
-
|
|
14
|
-
this.$schema = undefined
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
parse(joiSpec, definitions = {}, level = 0) {
|
|
18
|
-
const fullSchema = super.parse(joiSpec, definitions, level)
|
|
19
|
-
const schema = _.pickBy(fullSchema, (value, key) => isKnownKey(key))
|
|
20
|
-
|
|
21
|
-
if (level === 0 && !_.isEmpty(definitions)) {
|
|
22
|
-
schema.schemas = definitions
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (fullSchema.const) {
|
|
26
|
-
schema.enum = [fullSchema.const]
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return schema
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
_isIfThenElseSupported() {
|
|
33
|
-
return false
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
_getLocalSchemaBasePath() {
|
|
37
|
-
return '#/components/schemas'
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
_setBasicProperties(fieldSchema, fieldDefn) {
|
|
41
|
-
super._setBasicProperties(fieldSchema, fieldDefn)
|
|
42
|
-
|
|
43
|
-
if (!_.isEmpty(fieldSchema.examples)) {
|
|
44
|
-
fieldSchema.example = fieldSchema.examples[0]
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
_addNullTypeIfNullable(fieldSchema, fieldDefn) {
|
|
49
|
-
super._addNullTypeIfNullable(fieldSchema, fieldDefn)
|
|
50
|
-
|
|
51
|
-
if (fieldSchema.type === 'null') {
|
|
52
|
-
delete fieldSchema.type
|
|
53
|
-
fieldSchema.nullable = true
|
|
54
|
-
} else if (_.isArray(fieldSchema.type) && fieldSchema.type.includes('null')) {
|
|
55
|
-
_.remove(fieldSchema.type, (i) => {
|
|
56
|
-
return i === 'null'
|
|
57
|
-
})
|
|
58
|
-
fieldSchema.nullable = true
|
|
59
|
-
|
|
60
|
-
if (fieldSchema.type.length === 1) {
|
|
61
|
-
fieldSchema.type = fieldSchema.type[0]
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (_.isArray(fieldSchema.type)) {
|
|
66
|
-
// anyOf might exist for When / Alternative case
|
|
67
|
-
fieldSchema.anyOf = fieldSchema.anyOf || []
|
|
68
|
-
|
|
69
|
-
_.forEach(fieldSchema.type, (t) => {
|
|
70
|
-
const typeExisted = _.some(fieldSchema.anyOf, (condition) => {
|
|
71
|
-
return condition.type === t
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
if (!typeExisted) {
|
|
75
|
-
const def = { type: t }
|
|
76
|
-
if (t === 'array') {
|
|
77
|
-
def.items = {}
|
|
78
|
-
}
|
|
79
|
-
fieldSchema.anyOf.push(def)
|
|
80
|
-
}
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
delete fieldSchema.type
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
_isKnownMetaKey(key) {
|
|
88
|
-
return isKnownMetaKey(key)
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function isKnownMetaKey(key) {
|
|
93
|
-
return key.startsWith('x-') || key === 'deprecated' || key === 'readOnly' || key === 'writeOnly'
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function isKnownKey(key) {
|
|
97
|
-
const knownKeys = new Set([
|
|
98
|
-
'$ref',
|
|
99
|
-
'title',
|
|
100
|
-
'multipleOf',
|
|
101
|
-
'maximum',
|
|
102
|
-
'exclusiveMaximum',
|
|
103
|
-
'minimum',
|
|
104
|
-
'exclusiveMinimum',
|
|
105
|
-
'maxLength',
|
|
106
|
-
'minLength',
|
|
107
|
-
'pattern',
|
|
108
|
-
'maxItems',
|
|
109
|
-
'minItems',
|
|
110
|
-
'uniqueItems',
|
|
111
|
-
'maxProperties',
|
|
112
|
-
'minProperties',
|
|
113
|
-
'required',
|
|
114
|
-
'enum',
|
|
115
|
-
'description',
|
|
116
|
-
'format',
|
|
117
|
-
'default',
|
|
118
|
-
'type',
|
|
119
|
-
|
|
120
|
-
'allOf',
|
|
121
|
-
'oneOf',
|
|
122
|
-
'anyOf',
|
|
123
|
-
'not',
|
|
124
|
-
'items',
|
|
125
|
-
'properties',
|
|
126
|
-
'additionalProperties',
|
|
127
|
-
|
|
128
|
-
'example',
|
|
129
|
-
'nullable'
|
|
130
|
-
])
|
|
131
|
-
return knownKeys.has(key) || isKnownMetaKey(key)
|
|
132
|
-
}
|
|
133
|
-
|
|
1
|
+
const _ = require('lodash')
|
|
2
|
+
const JoiJsonSchemaParser = require('./json-draft-04')
|
|
3
|
+
|
|
4
|
+
class JoiOpenApiSchemaParser extends JoiJsonSchemaParser {
|
|
5
|
+
constructor(opts = {}) {
|
|
6
|
+
super(_.merge({
|
|
7
|
+
logicalOpParser: {
|
|
8
|
+
xor: null,
|
|
9
|
+
with: null,
|
|
10
|
+
without: null
|
|
11
|
+
}
|
|
12
|
+
}, opts))
|
|
13
|
+
|
|
14
|
+
this.$schema = undefined
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
parse(joiSpec, definitions = {}, level = 0) {
|
|
18
|
+
const fullSchema = super.parse(joiSpec, definitions, level)
|
|
19
|
+
const schema = _.pickBy(fullSchema, (value, key) => isKnownKey(key))
|
|
20
|
+
|
|
21
|
+
if (level === 0 && !_.isEmpty(definitions)) {
|
|
22
|
+
schema.schemas = definitions
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (fullSchema.const) {
|
|
26
|
+
schema.enum = [fullSchema.const]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return schema
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
_isIfThenElseSupported() {
|
|
33
|
+
return false
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
_getLocalSchemaBasePath() {
|
|
37
|
+
return '#/components/schemas'
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
_setBasicProperties(fieldSchema, fieldDefn) {
|
|
41
|
+
super._setBasicProperties(fieldSchema, fieldDefn)
|
|
42
|
+
|
|
43
|
+
if (!_.isEmpty(fieldSchema.examples)) {
|
|
44
|
+
fieldSchema.example = fieldSchema.examples[0]
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
_addNullTypeIfNullable(fieldSchema, fieldDefn) {
|
|
49
|
+
super._addNullTypeIfNullable(fieldSchema, fieldDefn)
|
|
50
|
+
|
|
51
|
+
if (fieldSchema.type === 'null') {
|
|
52
|
+
delete fieldSchema.type
|
|
53
|
+
fieldSchema.nullable = true
|
|
54
|
+
} else if (_.isArray(fieldSchema.type) && fieldSchema.type.includes('null')) {
|
|
55
|
+
_.remove(fieldSchema.type, (i) => {
|
|
56
|
+
return i === 'null'
|
|
57
|
+
})
|
|
58
|
+
fieldSchema.nullable = true
|
|
59
|
+
|
|
60
|
+
if (fieldSchema.type.length === 1) {
|
|
61
|
+
fieldSchema.type = fieldSchema.type[0]
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (_.isArray(fieldSchema.type)) {
|
|
66
|
+
// anyOf might exist for When / Alternative case
|
|
67
|
+
fieldSchema.anyOf = fieldSchema.anyOf || []
|
|
68
|
+
|
|
69
|
+
_.forEach(fieldSchema.type, (t) => {
|
|
70
|
+
const typeExisted = _.some(fieldSchema.anyOf, (condition) => {
|
|
71
|
+
return condition.type === t
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
if (!typeExisted) {
|
|
75
|
+
const def = { type: t }
|
|
76
|
+
if (t === 'array') {
|
|
77
|
+
def.items = {}
|
|
78
|
+
}
|
|
79
|
+
fieldSchema.anyOf.push(def)
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
delete fieldSchema.type
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
_isKnownMetaKey(key) {
|
|
88
|
+
return isKnownMetaKey(key)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function isKnownMetaKey(key) {
|
|
93
|
+
return key.startsWith('x-') || key === 'deprecated' || key === 'readOnly' || key === 'writeOnly'
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function isKnownKey(key) {
|
|
97
|
+
const knownKeys = new Set([
|
|
98
|
+
'$ref',
|
|
99
|
+
'title',
|
|
100
|
+
'multipleOf',
|
|
101
|
+
'maximum',
|
|
102
|
+
'exclusiveMaximum',
|
|
103
|
+
'minimum',
|
|
104
|
+
'exclusiveMinimum',
|
|
105
|
+
'maxLength',
|
|
106
|
+
'minLength',
|
|
107
|
+
'pattern',
|
|
108
|
+
'maxItems',
|
|
109
|
+
'minItems',
|
|
110
|
+
'uniqueItems',
|
|
111
|
+
'maxProperties',
|
|
112
|
+
'minProperties',
|
|
113
|
+
'required',
|
|
114
|
+
'enum',
|
|
115
|
+
'description',
|
|
116
|
+
'format',
|
|
117
|
+
'default',
|
|
118
|
+
'type',
|
|
119
|
+
|
|
120
|
+
'allOf',
|
|
121
|
+
'oneOf',
|
|
122
|
+
'anyOf',
|
|
123
|
+
'not',
|
|
124
|
+
'items',
|
|
125
|
+
'properties',
|
|
126
|
+
'additionalProperties',
|
|
127
|
+
|
|
128
|
+
'example',
|
|
129
|
+
'nullable'
|
|
130
|
+
])
|
|
131
|
+
return knownKeys.has(key) || isKnownMetaKey(key)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
134
|
module.exports = JoiOpenApiSchemaParser
|
package/package.json
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "joi-to-json",
|
|
3
|
-
"version": "4.3.
|
|
4
|
-
"description": "joi to JSON / OpenAPI Schema Converter",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"repository": {
|
|
7
|
-
"type": "git",
|
|
8
|
-
"url": "https://github.com/kenspirit/joi-to-json.git"
|
|
9
|
-
},
|
|
10
|
-
"scripts": {
|
|
11
|
-
"lint": "./node_modules/.bin/eslint .",
|
|
12
|
-
"test": "jest"
|
|
13
|
-
},
|
|
14
|
-
"author": "Ken Chen",
|
|
15
|
-
"license": "MIT",
|
|
16
|
-
"keywords": [
|
|
17
|
-
"joi",
|
|
18
|
-
"json-schema",
|
|
19
|
-
"json",
|
|
20
|
-
"schema",
|
|
21
|
-
"openapi",
|
|
22
|
-
"swagger",
|
|
23
|
-
"oas"
|
|
24
|
-
],
|
|
25
|
-
"dependencies": {
|
|
26
|
-
"combinations": "^1.0.0",
|
|
27
|
-
"lodash": "^4.17.21",
|
|
28
|
-
"semver-compare": "^1.0.0"
|
|
29
|
-
},
|
|
30
|
-
"devDependencies": {
|
|
31
|
-
"@jest/transform": "^29.7.0",
|
|
32
|
-
"ajv": "^8.16.0",
|
|
33
|
-
"ajv-draft-04": "^1.0.0",
|
|
34
|
-
"eslint": "^8.57.0",
|
|
35
|
-
"jest": "^29.7.0",
|
|
36
|
-
"joi-12": "npm:@commercial/joi@^12.1.0",
|
|
37
|
-
"joi-13": "npm:joi@^13.7.0",
|
|
38
|
-
"joi-14": "npm:joi@^14.3.1",
|
|
39
|
-
"joi-15": "npm:@hapi/joi@^15.1.1",
|
|
40
|
-
"joi-16": "npm:@hapi/joi@^16.1.8",
|
|
41
|
-
"joi-17": "npm:joi@^17.9.2"
|
|
42
|
-
},
|
|
43
|
-
"types": "index.d.ts",
|
|
44
|
-
"files": [
|
|
45
|
-
"docs/**/*",
|
|
46
|
-
"lib/**/*.js",
|
|
47
|
-
"index.js",
|
|
48
|
-
"index.d.ts"
|
|
49
|
-
]
|
|
50
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "joi-to-json",
|
|
3
|
+
"version": "4.3.2",
|
|
4
|
+
"description": "joi to JSON / OpenAPI Schema Converter",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/kenspirit/joi-to-json.git"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"lint": "./node_modules/.bin/eslint .",
|
|
12
|
+
"test": "jest"
|
|
13
|
+
},
|
|
14
|
+
"author": "Ken Chen",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"joi",
|
|
18
|
+
"json-schema",
|
|
19
|
+
"json",
|
|
20
|
+
"schema",
|
|
21
|
+
"openapi",
|
|
22
|
+
"swagger",
|
|
23
|
+
"oas"
|
|
24
|
+
],
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"combinations": "^1.0.0",
|
|
27
|
+
"lodash": "^4.17.21",
|
|
28
|
+
"semver-compare": "^1.0.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@jest/transform": "^29.7.0",
|
|
32
|
+
"ajv": "^8.16.0",
|
|
33
|
+
"ajv-draft-04": "^1.0.0",
|
|
34
|
+
"eslint": "^8.57.0",
|
|
35
|
+
"jest": "^29.7.0",
|
|
36
|
+
"joi-12": "npm:@commercial/joi@^12.1.0",
|
|
37
|
+
"joi-13": "npm:joi@^13.7.0",
|
|
38
|
+
"joi-14": "npm:joi@^14.3.1",
|
|
39
|
+
"joi-15": "npm:@hapi/joi@^15.1.1",
|
|
40
|
+
"joi-16": "npm:@hapi/joi@^16.1.8",
|
|
41
|
+
"joi-17": "npm:joi@^17.9.2"
|
|
42
|
+
},
|
|
43
|
+
"types": "index.d.ts",
|
|
44
|
+
"files": [
|
|
45
|
+
"docs/**/*",
|
|
46
|
+
"lib/**/*.js",
|
|
47
|
+
"index.js",
|
|
48
|
+
"index.d.ts"
|
|
49
|
+
]
|
|
50
|
+
}
|