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.
@@ -1,241 +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
- ```
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.d.ts CHANGED
@@ -1,36 +1,36 @@
1
- import Joi from 'joi-17';
2
-
3
- /**
4
- * @type {string}
5
- */
6
- export type Mode = 'json' | 'open-api' | 'json-draft-2019-09' | 'json-draft-04';
7
-
8
- /**
9
- * The parser function modifies the schema in-place.
10
- * @param {any} schema - JSON schema object
11
- * @param {any} dependency - JOI dependency object
12
- */
13
- export type LogicalOpParserFn = (schema, dependency) => void;
14
-
15
- export interface LogicalOpParserOpts {
16
- and?: LogicalOpParserFn,
17
- nand?: LogicalOpParserFn,
18
- or?: LogicalOpParserFn,
19
- xor?: LogicalOpParserFn,
20
- oxor?: LogicalOpParserFn,
21
- with?: LogicalOpParserFn,
22
- without?: LogicalOpParserFn
23
- };
24
-
25
- export type ParserOptions = false | { logicalOpParser?: LogicalOpParserOpts };
26
-
27
- /**
28
- * @param {Joi.Schema} joi - A Joi schema.
29
- * @param {string} [mode='json'] - json / open-api / json-draft-2019-09 / json-draft-04
30
- * @param {Record} [sharedSchema={}] - Passed-in object storing shared schemas
31
- * @param {ParserOptions} [parserOptions={}] - Passed-in options for parser
32
- * @returns {any} Converted JSON schema object.
33
- */
34
- export function parse(joi: typeof Joi.Schema, mode?: Mode, sharedSchema?: Record<string, any>, parserOptions?: ParserOptions): any;
35
-
36
- export default parse;
1
+ import Joi from 'joi-17';
2
+
3
+ /**
4
+ * @type {string}
5
+ */
6
+ export type Mode = 'json' | 'open-api' | 'open-api-3.1' | 'json-draft-2019-09' | 'json-draft-04';
7
+
8
+ /**
9
+ * The parser function modifies the schema in-place.
10
+ * @param {any} schema - JSON schema object
11
+ * @param {any} dependency - JOI dependency object
12
+ */
13
+ export type LogicalOpParserFn = (schema, dependency) => void;
14
+
15
+ export interface LogicalOpParserOpts {
16
+ and?: LogicalOpParserFn,
17
+ nand?: LogicalOpParserFn,
18
+ or?: LogicalOpParserFn,
19
+ xor?: LogicalOpParserFn,
20
+ oxor?: LogicalOpParserFn,
21
+ with?: LogicalOpParserFn,
22
+ without?: LogicalOpParserFn
23
+ };
24
+
25
+ export type ParserOptions = false | { logicalOpParser?: LogicalOpParserOpts };
26
+
27
+ /**
28
+ * @param {Joi.Schema} joi - A Joi schema.
29
+ * @param {string} [mode='json'] - json / open-api / json-draft-2019-09 / json-draft-04
30
+ * @param {Record} [sharedSchema={}] - Passed-in object storing shared schemas
31
+ * @param {ParserOptions} [parserOptions={}] - Passed-in options for parser
32
+ * @returns {any} Converted JSON schema object.
33
+ */
34
+ export function parse(joi: typeof Joi.Schema, mode?: Mode, sharedSchema?: Record<string, any>, parserOptions?: ParserOptions): any;
35
+
36
+ export default parse;