joi-to-json 3.0.0 → 3.1.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/CHANGELOG.md +4 -0
- package/docs/logical_rel_support.md +241 -0
- package/index.js +65 -65
- package/lib/convertors/v12.js +258 -258
- package/lib/parsers/json-draft-04.js +34 -25
- package/lib/parsers/json.js +612 -589
- package/lib/parsers/open-api.js +9 -0
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
## Logical Relation
|
|
2
|
+
|
|
3
|
+
There are some logical relation operators in Joi:
|
|
4
|
+
|
|
5
|
+
* `and`
|
|
6
|
+
* `nand`
|
|
7
|
+
* `or`
|
|
8
|
+
* `xor`
|
|
9
|
+
* `oxor` (After Joi v14)
|
|
10
|
+
* `with`
|
|
11
|
+
* `without`
|
|
12
|
+
|
|
13
|
+
For different operator, I have managed to describe them in JSON schema as below for different cases.
|
|
14
|
+
Some named as `xxxGeneral` means it should be supported since JSON Draft 4.
|
|
15
|
+
Some named as `xxxDraft7` means it is using some features until JSON Draft 7.
|
|
16
|
+
|
|
17
|
+
Hence, if you are converting the Joi to Draft 4 or OpenAPI, the `xor` will be ignored.
|
|
18
|
+
|
|
19
|
+
### Usage
|
|
20
|
+
|
|
21
|
+
By default, this feature is enabled.
|
|
22
|
+
It can be disabled completely by passing option `{ logicalOpParser: false }` to `parse` API:
|
|
23
|
+
|
|
24
|
+
`parse(joiObj, 'json', {}, { logicalOpParser: false })`
|
|
25
|
+
|
|
26
|
+
It's also possible to disable one particular operator or use your own convertor if you come up with a more suitable JSON schema representation by passing options like `{ logicalOpParser: { xor: convertorFun, oxor: null } }`.
|
|
27
|
+
|
|
28
|
+
The signature of the `convertorFun` is `function (schema, dependency)`. For example, the built-in `or` convertor function is:
|
|
29
|
+
|
|
30
|
+
```javascript
|
|
31
|
+
function (schema, dependency) {
|
|
32
|
+
schema.anyOf = _.map(dependency.peers, (peer) => {
|
|
33
|
+
return { required: [peer] }
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### OR
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
// At least one of a or b or c exists.
|
|
42
|
+
// Failed on { d: 1 }
|
|
43
|
+
const orJoi = joi.object({
|
|
44
|
+
a: joi.string(),
|
|
45
|
+
b: joi.number(),
|
|
46
|
+
c: joi.boolean(),
|
|
47
|
+
d: joi.number()
|
|
48
|
+
}).or('a', 'b', 'c');
|
|
49
|
+
|
|
50
|
+
const orSchemaGeneral = {
|
|
51
|
+
type: 'object',
|
|
52
|
+
anyOf: [
|
|
53
|
+
{ required: ['a'] }, { required: ['b'] }, { required: ['c'] }
|
|
54
|
+
],
|
|
55
|
+
properties: { a: { type: 'string' }, b: { type: 'number' }, c: { type: 'boolean' }, d: { type: 'number' } },
|
|
56
|
+
additionalProperties: false
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### AND
|
|
61
|
+
|
|
62
|
+
```javascript
|
|
63
|
+
// Either a, b, and c All NOT exists, or all of them exists
|
|
64
|
+
// Failed on { a: 'hi', b: 1 }
|
|
65
|
+
const andJoi = joi.object({
|
|
66
|
+
a: joi.string(),
|
|
67
|
+
b: joi.number(),
|
|
68
|
+
c: joi.boolean(),
|
|
69
|
+
d: joi.number()
|
|
70
|
+
}).and('a', 'b', 'c');
|
|
71
|
+
|
|
72
|
+
const andSchemaGeneral = {
|
|
73
|
+
type: 'object',
|
|
74
|
+
oneOf: [
|
|
75
|
+
{
|
|
76
|
+
allOf: [
|
|
77
|
+
{
|
|
78
|
+
not: { required: ['a'] }
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
not: { required: ['b'] }
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
not: { required: ['c'] }
|
|
85
|
+
}
|
|
86
|
+
]
|
|
87
|
+
},
|
|
88
|
+
{ required: ['a', 'b', 'c'] }
|
|
89
|
+
],
|
|
90
|
+
properties: { a: { type: 'string' }, b: { type: 'number' }, c: { type: 'boolean' }, d: { type: 'number' } },
|
|
91
|
+
additionalProperties: false
|
|
92
|
+
};
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### NAND
|
|
96
|
+
|
|
97
|
+
```javascript
|
|
98
|
+
// a, b, and c cannot all exist at the same time
|
|
99
|
+
// Failed on { a: 'hi', b: 1, c: true }
|
|
100
|
+
const nandJoi = joi.object({
|
|
101
|
+
a: joi.string(),
|
|
102
|
+
b: joi.number(),
|
|
103
|
+
c: joi.boolean(),
|
|
104
|
+
d: joi.number()
|
|
105
|
+
}).nand('a', 'b', 'c');
|
|
106
|
+
|
|
107
|
+
const nandSchemaGeneral = {
|
|
108
|
+
type: 'object',
|
|
109
|
+
not: { required: ['a', 'b', 'c'] },
|
|
110
|
+
properties: { a: { type: 'string' }, b: { type: 'number' }, c: { type: 'boolean' }, d: { type: 'number' } },
|
|
111
|
+
additionalProperties: false
|
|
112
|
+
};
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### XOR
|
|
116
|
+
|
|
117
|
+
```javascript
|
|
118
|
+
// Only one of a, b and c can and must exist
|
|
119
|
+
// Failed on { d: 1 } or { a: 'hi', b: 1 }
|
|
120
|
+
const xorJoi = joi.object({
|
|
121
|
+
a: joi.string(),
|
|
122
|
+
b: joi.number(),
|
|
123
|
+
c: joi.boolean(),
|
|
124
|
+
d: joi.number()
|
|
125
|
+
}).xor('a', 'b', 'c');
|
|
126
|
+
|
|
127
|
+
const xorSchemaDraft7 = {
|
|
128
|
+
type: 'object',
|
|
129
|
+
if: { propertyNames: { enum: ['a', 'b', 'c'] }, minProperties: 2 },
|
|
130
|
+
then: false,
|
|
131
|
+
else: {
|
|
132
|
+
oneOf: [
|
|
133
|
+
{
|
|
134
|
+
required: ['a']
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
required: ['b']
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
required: ['c']
|
|
141
|
+
}
|
|
142
|
+
]
|
|
143
|
+
},
|
|
144
|
+
properties: { a: { type: 'string' }, b: { type: 'number' }, c: { type: 'boolean' }, d: { type: 'number' } },
|
|
145
|
+
additionalProperties: false
|
|
146
|
+
};
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### OXOR
|
|
150
|
+
|
|
151
|
+
```javascript
|
|
152
|
+
// Only one of a, b and c can exist but none is required
|
|
153
|
+
// Failed on { a: 'hi', b: 1 }
|
|
154
|
+
const oxorJoi = joi.object({
|
|
155
|
+
a: joi.string(),
|
|
156
|
+
b: joi.number(),
|
|
157
|
+
c: joi.boolean(),
|
|
158
|
+
d: joi.number()
|
|
159
|
+
}).oxor('a', 'b', 'c');
|
|
160
|
+
|
|
161
|
+
const oxorSchemaGeneral = {
|
|
162
|
+
type: 'object',
|
|
163
|
+
oneOf: [
|
|
164
|
+
{ required: ['a'] },
|
|
165
|
+
{ required: ['b'] },
|
|
166
|
+
{ required: ['c'] },
|
|
167
|
+
{
|
|
168
|
+
not: {
|
|
169
|
+
oneOf: [
|
|
170
|
+
{ required: ['a'] },
|
|
171
|
+
{ required: ['b'] },
|
|
172
|
+
{ required: ['c'] },
|
|
173
|
+
{ required: ['a', 'b'] },
|
|
174
|
+
{ required: ['a', 'c'] },
|
|
175
|
+
{ required: ['b', 'c'] } // Combination up to 2 elements
|
|
176
|
+
]
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
],
|
|
180
|
+
properties: { a: { type: 'string' }, b: { type: 'number' }, c: { type: 'boolean' }, d: { type: 'number' } },
|
|
181
|
+
additionalProperties: false
|
|
182
|
+
};
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### WITH
|
|
186
|
+
|
|
187
|
+
```javascript
|
|
188
|
+
// With d exists, both a and b must exist
|
|
189
|
+
// Failed on { d: 1, a: '' }
|
|
190
|
+
const withJoi = jjoi.object({
|
|
191
|
+
a: joi.string(),
|
|
192
|
+
b: joi.number(),
|
|
193
|
+
c: joi.boolean(),
|
|
194
|
+
d: joi.number()
|
|
195
|
+
}).with('c', ['a']).with('d', ['a', 'b']);
|
|
196
|
+
|
|
197
|
+
const withSchemaBeforeBefore2019 = {
|
|
198
|
+
type: 'object',
|
|
199
|
+
dependencies: {
|
|
200
|
+
c: ['a'],
|
|
201
|
+
d: ['a', 'b']
|
|
202
|
+
},
|
|
203
|
+
properties: { a: { type: 'string' }, b: { type: 'number' }, c: { type: 'boolean' }, d: { type: 'number' } },
|
|
204
|
+
additionalProperties: false
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const withSchemaBeforeDraft2019 = {
|
|
208
|
+
type: 'object',
|
|
209
|
+
dependentRequired: {
|
|
210
|
+
c: ['a'],
|
|
211
|
+
d: ['a', 'b']
|
|
212
|
+
},
|
|
213
|
+
properties: { a: { type: 'string' }, b: { type: 'number' }, c: { type: 'boolean' }, d: { type: 'number' } },
|
|
214
|
+
additionalProperties: false
|
|
215
|
+
};
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### WITHOUT
|
|
219
|
+
|
|
220
|
+
```javascript
|
|
221
|
+
// With a exists, either b or c must not exist
|
|
222
|
+
// Failed on { a: '', b: 1 }
|
|
223
|
+
const withoutJoi = jjoi.object({
|
|
224
|
+
a: joi.string(),
|
|
225
|
+
b: joi.number(),
|
|
226
|
+
c: joi.boolean(),
|
|
227
|
+
d: joi.number()
|
|
228
|
+
}).without('a', ['b', 'c']);
|
|
229
|
+
|
|
230
|
+
const withoutSchemaDraft7 = {
|
|
231
|
+
type: 'object',
|
|
232
|
+
if: { required: ['a'] },
|
|
233
|
+
then: {
|
|
234
|
+
not: {
|
|
235
|
+
anyOf: [{ required: ['b'] }, { required: ['c'] }]
|
|
236
|
+
}
|
|
237
|
+
},
|
|
238
|
+
properties: { a: { type: 'string' }, b: { type: 'number' }, c: { type: 'boolean' }, d: { type: 'number' } },
|
|
239
|
+
additionalProperties: false
|
|
240
|
+
};
|
|
241
|
+
```
|
package/index.js
CHANGED
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
const cmp = require('semver-compare')
|
|
2
|
-
|
|
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
|
-
|
|
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')
|
|
14
|
-
|
|
15
|
-
const convertors = [
|
|
16
|
-
c17, c16, c15, c14, c13, c12
|
|
17
|
-
]
|
|
18
|
-
const parsers = {
|
|
19
|
-
'json-draft-2019-09': JoiJsonDraftSchemaParser19,
|
|
20
|
-
'json-draft-04': JoiJsonDraftSchemaParser,
|
|
21
|
-
json: JoiJsonSchemaParser,
|
|
22
|
-
'open-api': JoiOpenApiSchemaParser
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function parse(joiObj, type = 'json', definitions = {}, parserOptions = {}) {
|
|
26
|
-
if (typeof joiObj.describe !== 'function') {
|
|
27
|
-
throw new Error('Not an joi object.')
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
let convertor
|
|
31
|
-
|
|
32
|
-
for (let i = 0; i < convertors.length; i++) {
|
|
33
|
-
const tmpConvertor = convertors[i]
|
|
34
|
-
try {
|
|
35
|
-
let version = tmpConvertor.getVersion(joiObj)
|
|
36
|
-
let result = cmp(tmpConvertor.getSupportVersion(), version)
|
|
37
|
-
if (result <= 0) {
|
|
38
|
-
// The first parser has smaller or equal version
|
|
39
|
-
convertor = tmpConvertor
|
|
40
|
-
break
|
|
41
|
-
}
|
|
42
|
-
} catch (e) {
|
|
43
|
-
// Format does not match this parser version.
|
|
44
|
-
// Skip to check the next one
|
|
45
|
-
continue
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (!convertor) {
|
|
50
|
-
console.warn('No matched joi version convertor found, using the latest version')
|
|
51
|
-
convertor = convertors[0]
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// fs.writeFileSync('./joi_spec.json', JSON.stringify(joiObj.describe(), null, 2))
|
|
55
|
-
const joiBaseSpec = new convertor().toBaseSpec(joiObj.describe())
|
|
56
|
-
// fs.writeFileSync(`./internal_${convertor.getSupportVersion()}_${type}.json`, JSON.stringify(joiBaseSpec, null, 2))
|
|
57
|
-
const parser = parsers[type]
|
|
58
|
-
if (!parser) {
|
|
59
|
-
throw new Error(`No parser is registered for ${type}`)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return new parser(parserOptions).parse(joiBaseSpec, definitions)
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
module.exports = parse
|
|
1
|
+
const cmp = require('semver-compare')
|
|
2
|
+
|
|
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
|
+
|
|
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')
|
|
14
|
+
|
|
15
|
+
const convertors = [
|
|
16
|
+
c17, c16, c15, c14, c13, c12
|
|
17
|
+
]
|
|
18
|
+
const parsers = {
|
|
19
|
+
'json-draft-2019-09': JoiJsonDraftSchemaParser19,
|
|
20
|
+
'json-draft-04': JoiJsonDraftSchemaParser,
|
|
21
|
+
json: JoiJsonSchemaParser,
|
|
22
|
+
'open-api': JoiOpenApiSchemaParser
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function parse(joiObj, type = 'json', definitions = {}, parserOptions = {}) {
|
|
26
|
+
if (typeof joiObj.describe !== 'function') {
|
|
27
|
+
throw new Error('Not an joi object.')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let convertor
|
|
31
|
+
|
|
32
|
+
for (let i = 0; i < convertors.length; i++) {
|
|
33
|
+
const tmpConvertor = convertors[i]
|
|
34
|
+
try {
|
|
35
|
+
let version = tmpConvertor.getVersion(joiObj)
|
|
36
|
+
let result = cmp(tmpConvertor.getSupportVersion(), version)
|
|
37
|
+
if (result <= 0) {
|
|
38
|
+
// The first parser has smaller or equal version
|
|
39
|
+
convertor = tmpConvertor
|
|
40
|
+
break
|
|
41
|
+
}
|
|
42
|
+
} catch (e) {
|
|
43
|
+
// Format does not match this parser version.
|
|
44
|
+
// Skip to check the next one
|
|
45
|
+
continue
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (!convertor) {
|
|
50
|
+
console.warn('No matched joi version convertor found, using the latest version')
|
|
51
|
+
convertor = convertors[0]
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// fs.writeFileSync('./joi_spec.json', JSON.stringify(joiObj.describe(), null, 2))
|
|
55
|
+
const joiBaseSpec = new convertor().toBaseSpec(joiObj.describe())
|
|
56
|
+
// fs.writeFileSync(`./internal_${convertor.getSupportVersion()}_${type}.json`, JSON.stringify(joiBaseSpec, null, 2))
|
|
57
|
+
const parser = parsers[type]
|
|
58
|
+
if (!parser) {
|
|
59
|
+
throw new Error(`No parser is registered for ${type}`)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return new parser(parserOptions).parse(joiBaseSpec, definitions)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
module.exports = parse
|