json-2-joi 2.3.2 → 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 +4 -1
- package/lib/resolver.js +22 -13
- package/package.json +1 -1
- package/CHANGELOG.md +0 -214
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ Converts a JSON schema to a Joi schema for object validation.
|
|
|
6
6
|
|
|
7
7
|
## Change based on the forked version (enjoi):
|
|
8
8
|
|
|
9
|
-
- Provided `refineDescription(schema)`, `allowNull`, `forbidArrayNull`, `strictRequired`, `strictEnum`, `enableEnum` options
|
|
9
|
+
- Provided `refineDescription(schema)`, `allowNull`, `forbidArrayNull`, `strictRequired`, `strictEnum`, `enableEnum`, `customizedNullValues` options
|
|
10
10
|
- Change cycle reference resovling without self-generating id and rely on $id if defined in JSON schema
|
|
11
11
|
- Support `const` keyword in JSON schema
|
|
12
12
|
- Support `example` setting for Joi based on JSON schema's `examples`, `default` or `enum`
|
|
@@ -55,7 +55,9 @@ Please file issues for other unsupported features.
|
|
|
55
55
|
- `refineType(type, format)` - an (optional) function to call to apply to type based on the type and format of the JSON schema.
|
|
56
56
|
- `refineSchema(joiSchema, jsonSchema)` - an (optional) function to call to apply to adjust Joi schema base on the original JSON schema. Primary use case is handling `nullable` flag in OpenAPI 3.0
|
|
57
57
|
- `allowNull` - Default as `false`. When `true` and the field has no `enum` setting, `null` value will be allowed.
|
|
58
|
+
- `customizedNullValues` - Default as `[null]`. When provided, the value in the array will be treated valid null value when schema is 'null' type or other allowing null case.
|
|
58
59
|
- `forbidArrayNull` - Default as `true`. When `false` and `allowNull` is true, `null` value will be allowed for array type field.
|
|
60
|
+
- `strictArrayRequired` - Default as `false`. When `true`, those `required` array-type fields must has at least 1 item and its item must not be `null` item, or value defined by `customizedNullValues`.
|
|
59
61
|
- `strictRequired` - Default as `false`. When `true`, `null` (and empty string additionally for string type) will NOT be allowed for those `required` field in object.
|
|
60
62
|
- `strictEnum` - Default as `true`. When `false`, and `allowNull` is `true`, `null` (and empty string additionally for string type) will be added to `enum` list.
|
|
61
63
|
- `enableEnum` - Default as `true`. When `false`, `enum` restriction will not be added to Joi Schema.
|
|
@@ -221,6 +223,7 @@ It's possible to override these options for each conversion:
|
|
|
221
223
|
* allowNull
|
|
222
224
|
* forbidArrayNull
|
|
223
225
|
* strictRequired
|
|
226
|
+
* strictArrayRequired
|
|
224
227
|
* strictEnum
|
|
225
228
|
* enableEnum
|
|
226
229
|
* noDefaults (Copied from `joiOptions` for the construction time)
|
package/lib/resolver.js
CHANGED
|
@@ -21,9 +21,11 @@ const optionsSchema = Joi.object({
|
|
|
21
21
|
refineDescription: Joi.func().allow(null),
|
|
22
22
|
allowNull: Joi.boolean().default(false),
|
|
23
23
|
forbidArrayNull: Joi.boolean().default(true),
|
|
24
|
+
strictArrayRequired: Joi.boolean().default(false),
|
|
24
25
|
strictRequired: Joi.boolean().default(false),
|
|
25
26
|
strictEnum: Joi.boolean().default(true),
|
|
26
27
|
enableEnum: Joi.boolean().default(true),
|
|
28
|
+
customizedNullValues: Joi.array().items(Joi.any()).default([null]).min(1),
|
|
27
29
|
joiOptions: Joi.object().unknown(true).default({})
|
|
28
30
|
});
|
|
29
31
|
|
|
@@ -33,10 +35,12 @@ const overridableOptions = {
|
|
|
33
35
|
refineDescription: Joi.func().allow(null),
|
|
34
36
|
allowNull: Joi.boolean(),
|
|
35
37
|
forbidArrayNull: Joi.boolean(),
|
|
38
|
+
strictArrayRequired: Joi.boolean(),
|
|
36
39
|
strictRequired: Joi.boolean(),
|
|
37
40
|
noDefaults: Joi.boolean(),
|
|
38
41
|
strictEnum: Joi.boolean(),
|
|
39
|
-
enableEnum: Joi.boolean()
|
|
42
|
+
enableEnum: Joi.boolean(),
|
|
43
|
+
customizedNullValues: Joi.array().items(Joi.any()).min(1),
|
|
40
44
|
};
|
|
41
45
|
const overridableOptionKeys = Object.keys(overridableOptions);
|
|
42
46
|
const overrideOptionsSchema = Joi.object(overridableOptions);
|
|
@@ -64,7 +68,7 @@ function isNotEmptyObject(value) {
|
|
|
64
68
|
return isObject(value) && Object.keys(value).length > 0;
|
|
65
69
|
}
|
|
66
70
|
|
|
67
|
-
function allowNullIfNeeded(schema, { allowNull = false, forbidArrayNull = true }) {
|
|
71
|
+
function allowNullIfNeeded(schema, { allowNull = false, forbidArrayNull = true, customizedNullValues = [null] }) {
|
|
68
72
|
if (allowNull) {
|
|
69
73
|
const definition = schema.describe();
|
|
70
74
|
if (definition.flags && definition.flags.only && definition.allow) {
|
|
@@ -74,7 +78,7 @@ function allowNullIfNeeded(schema, { allowNull = false, forbidArrayNull = true }
|
|
|
74
78
|
if (forbidArrayNull && schema.type === 'array') {
|
|
75
79
|
return schema;
|
|
76
80
|
}
|
|
77
|
-
return schema.allow(
|
|
81
|
+
return schema.allow(...customizedNullValues);
|
|
78
82
|
}
|
|
79
83
|
return schema;
|
|
80
84
|
}
|
|
@@ -86,10 +90,10 @@ function addEnumRestriction(joiSchema, enumList, { allowNull = false, strictEnum
|
|
|
86
90
|
|
|
87
91
|
const valids = [].concat(enumList);
|
|
88
92
|
if (allowNull && !strictEnum) {
|
|
89
|
-
valids.splice(0, 0, null);
|
|
90
93
|
if (joiSchema.type === 'string') {
|
|
91
|
-
valids.
|
|
94
|
+
valids.push('');
|
|
92
95
|
}
|
|
96
|
+
valids.push(null);
|
|
93
97
|
}
|
|
94
98
|
return joiSchema.valid(...valids);
|
|
95
99
|
}
|
|
@@ -382,7 +386,7 @@ class SchemaResolver {
|
|
|
382
386
|
joischema = this.string(schema);
|
|
383
387
|
break;
|
|
384
388
|
case 'null':
|
|
385
|
-
joischema = this.joi.any().valid(
|
|
389
|
+
joischema = this.joi.any().valid(...options.customizedNullValues);
|
|
386
390
|
break;
|
|
387
391
|
default:
|
|
388
392
|
joischema = this.joi.types()[type];
|
|
@@ -418,7 +422,8 @@ class SchemaResolver {
|
|
|
418
422
|
joischema = joitype(schema.type, resolveOptions, schema.format);
|
|
419
423
|
}
|
|
420
424
|
if (nullIdx > -1) {
|
|
421
|
-
joischema = joischema
|
|
425
|
+
joischema = allowNullIfNeeded(joischema,
|
|
426
|
+
{ allowNull: true, customizedNullValues: resolveOptions.customizedNullValues });
|
|
422
427
|
}
|
|
423
428
|
|
|
424
429
|
const typeDefinitionMap = {
|
|
@@ -529,12 +534,13 @@ class SchemaResolver {
|
|
|
529
534
|
}
|
|
530
535
|
|
|
531
536
|
resolveAsArray(value, resolveOptions, baseUri) {
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
537
|
+
return [].concat(value).map((v) => {
|
|
538
|
+
const vSchema = this.resolve(v, resolveOptions, baseUri);
|
|
539
|
+
if (resolveOptions.strictArrayRequired) {
|
|
540
|
+
return vSchema.invalid(...resolveOptions.customizedNullValues);
|
|
541
|
+
}
|
|
542
|
+
return allowNullIfNeeded(vSchema, resolveOptions)
|
|
543
|
+
});
|
|
538
544
|
}
|
|
539
545
|
|
|
540
546
|
array(schema, resolveOptions, baseUri) {
|
|
@@ -554,6 +560,9 @@ class SchemaResolver {
|
|
|
554
560
|
joischema = joischema.max(items.length);
|
|
555
561
|
}
|
|
556
562
|
|
|
563
|
+
if (resolveOptions.strictArrayRequired && !schema.minItems) {
|
|
564
|
+
schema.minItems = 1;
|
|
565
|
+
}
|
|
557
566
|
isNumber(schema.minItems) && (joischema = joischema.min(schema.minItems));
|
|
558
567
|
isNumber(schema.maxItems) && (joischema = joischema.max(schema.maxItems));
|
|
559
568
|
|
package/package.json
CHANGED
package/CHANGELOG.md
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
### 2.3.2
|
|
2
|
-
|
|
3
|
-
- Enhance `null` value support when `type` is array and contains `null`.
|
|
4
|
-
|
|
5
|
-
### 2.3.1
|
|
6
|
-
|
|
7
|
-
- Fix `noDefaults` options validation issue.
|
|
8
|
-
|
|
9
|
-
### 2.3.0
|
|
10
|
-
|
|
11
|
-
- If `noDefaults` is set `true`, `default()` will not be set to Joi schema and allow it overridable.
|
|
12
|
-
|
|
13
|
-
### 2.2.0
|
|
14
|
-
|
|
15
|
-
- Add `forbidArrayNull`, `strictRequired` option.
|
|
16
|
-
|
|
17
|
-
### 2.1.0
|
|
18
|
-
|
|
19
|
-
- Fix the issue that `enum` is only applied to `string` type or when missing type info.
|
|
20
|
-
- Add `strictEnum` and `enableEnum` option.
|
|
21
|
-
- Allow override options when calling `convert` api.
|
|
22
|
-
|
|
23
|
-
### 2.0.2
|
|
24
|
-
|
|
25
|
-
- Remove `min` setting from JOI schema if `minLength` is not defined in JSON schema.
|
|
26
|
-
- Allow `null` value as Array's item if `allowNull` option is set to true.
|
|
27
|
-
|
|
28
|
-
### 2.0.1
|
|
29
|
-
|
|
30
|
-
- Set correct examples data for Array type field.
|
|
31
|
-
|
|
32
|
-
### 2.0.0
|
|
33
|
-
|
|
34
|
-
* [BREAKING] Remove `useDefaults` and `strictMode` options
|
|
35
|
-
|
|
36
|
-
### 1.1.0
|
|
37
|
-
|
|
38
|
-
- Support providing `joiOptions` for JOI default instance creation.
|
|
39
|
-
- Support directly passing a Joi instance for schema building.
|
|
40
|
-
|
|
41
|
-
### 1.0.0
|
|
42
|
-
|
|
43
|
-
- Provided `refineDescription(schema)`, `allowNull` options
|
|
44
|
-
- Change cycle reference resovling without self-generating id and rely on $id if defined in JSON schema
|
|
45
|
-
- Support `const` keyword in JSON schema
|
|
46
|
-
- Support `example` setting for Joi based on JSON schema's `examples`, `default` or `enum`
|
|
47
|
-
- Support `contains` keyword in JSON schema using Joi's `has` method
|
|
48
|
-
- Support `$anchor` keyword in JSON schema
|
|
49
|
-
- Remove `defaults` API. Please refer to section **Reuse JOI Schema Resolver** for similar usage.
|
|
50
|
-
- **Big Change** on `$ref` and `subSchemas`. Please refer to [test/test-references.js](test/test-references.js) for detail usage. Currently, four format of `$ref` are supported:
|
|
51
|
-
* id
|
|
52
|
-
* [baseUri]#anchor
|
|
53
|
-
* [baseUri]#/$defs/shared
|
|
54
|
-
* [baseUri]#/$defs/shared/properties/level1
|
|
55
|
-
|
|
56
|
-
**subSchemas will be modified** for two reason:
|
|
57
|
-
* If any subSchema does not have `$id` field, it will be added using the corresponding key in `subSchemas` object.
|
|
58
|
-
* New key-value pair will be added into it if the schema or any its subschema being parsed has `$id` field.
|
|
59
|
-
|
|
60
|
-
## Before Fork
|
|
61
|
-
|
|
62
|
-
### 9.0.1
|
|
63
|
-
|
|
64
|
-
* #112 Use joi.link to only resolve strictly recursive self-references
|
|
65
|
-
|
|
66
|
-
### 9.0.0
|
|
67
|
-
|
|
68
|
-
* #100 Allow extension constructors
|
|
69
|
-
* #101 Keep string enum type
|
|
70
|
-
* #104 Allow allOf, anyOf, oneOf to be combined with base schema
|
|
71
|
-
* #107 Cycles in schema refs (fixed #106)
|
|
72
|
-
* #108 Handle `not` properly
|
|
73
|
-
* #109 Defaults options
|
|
74
|
-
|
|
75
|
-
### 8.0.0
|
|
76
|
-
|
|
77
|
-
* [BREAKING] Switched from @hapi/joi to joi (#94)
|
|
78
|
-
* Added refineSchema function (#92)
|
|
79
|
-
|
|
80
|
-
### 7.0.0
|
|
81
|
-
|
|
82
|
-
* [BREAKING] Updated to Joi@17
|
|
83
|
-
* [BREAKING] Custom types moved to being defined under `extensions`
|
|
84
|
-
* [BREAKING] Extensions definitions to follow Joi@17
|
|
85
|
-
|
|
86
|
-
### 6.0.2
|
|
87
|
-
|
|
88
|
-
* Correct usage of joi `validate` function. joiSchema.validate(obj) instead of Joi.validate(obj, joiSchema).
|
|
89
|
-
|
|
90
|
-
### 6.0.1
|
|
91
|
-
|
|
92
|
-
* #83 differentiate between undefined and falsy
|
|
93
|
-
* #73 updated fix
|
|
94
|
-
|
|
95
|
-
### 6.0.0
|
|
96
|
-
|
|
97
|
-
* Do not set empty string as valid for all string types (#73)
|
|
98
|
-
* Updated joi and hoek to latest, drop Node 6 support (#76)
|
|
99
|
-
|
|
100
|
-
### v5.0.1
|
|
101
|
-
|
|
102
|
-
* #69
|
|
103
|
-
|
|
104
|
-
### v5.0.1
|
|
105
|
-
|
|
106
|
-
* #66
|
|
107
|
-
|
|
108
|
-
### v5.0.0
|
|
109
|
-
|
|
110
|
-
* [BREAKING] Addresses #63 : date format to follow RFC3339 as per JSON-Schema.
|
|
111
|
-
|
|
112
|
-
### v4.1.1
|
|
113
|
-
|
|
114
|
-
* Fixes `oneOf` requiring (#61).
|
|
115
|
-
|
|
116
|
-
### v4.1.0
|
|
117
|
-
|
|
118
|
-
* Added support for string formats `uuid` (v4) and `guid`.
|
|
119
|
-
|
|
120
|
-
### v4.0.0
|
|
121
|
-
|
|
122
|
-
* [BREAKING] export interface changed. Call `Enjoi.schema` instead of `Enjoi`.
|
|
123
|
-
* [BREAKING] `joi` is a peer.
|
|
124
|
-
* Added `extensions` support.
|
|
125
|
-
* `types` can also contain function values to do complex resolving of custom types.
|
|
126
|
-
* Added support for `Enjoi.defaults` which returns a new Enjoi with default options.
|
|
127
|
-
|
|
128
|
-
### v3.2.5
|
|
129
|
-
|
|
130
|
-
* Fixed allOf support (#53)
|
|
131
|
-
|
|
132
|
-
### v3.2.4
|
|
133
|
-
|
|
134
|
-
* Support for allOf
|
|
135
|
-
* Bug fixes
|
|
136
|
-
|
|
137
|
-
### v3.2.3
|
|
138
|
-
|
|
139
|
-
* Security fix to resolve https://nodesecurity.io/advisories/566
|
|
140
|
-
|
|
141
|
-
### v3.2.2
|
|
142
|
-
|
|
143
|
-
* Documentation fixes.
|
|
144
|
-
|
|
145
|
-
### v3.2.1
|
|
146
|
-
|
|
147
|
-
* Support array:additionalItems false setting.
|
|
148
|
-
|
|
149
|
-
### v3.2.0
|
|
150
|
-
|
|
151
|
-
* Added `strictMode` support and resolves #34.
|
|
152
|
-
|
|
153
|
-
### v3.1.0
|
|
154
|
-
|
|
155
|
-
* Added a refineType function option.
|
|
156
|
-
|
|
157
|
-
### v3.0.0
|
|
158
|
-
|
|
159
|
-
* [BREAKING] supports Joi 13.x (required Node 6+)
|
|
160
|
-
* Fixed additional properties https://github.com/tlivings/enjoi/pull/31
|
|
161
|
-
* Adds support for ordered and items https://github.com/tlivings/enjoi/pull/38
|
|
162
|
-
|
|
163
|
-
### v2.2.4
|
|
164
|
-
|
|
165
|
-
* Fixes mutating `options` to add `stripUnknown`.
|
|
166
|
-
|
|
167
|
-
### v2.2.3
|
|
168
|
-
|
|
169
|
-
* Fix #25 by supporting array for type (e.g. ['string', 'null']).
|
|
170
|
-
* Additional formats: hostname, uri, ipv4, ipv6.
|
|
171
|
-
|
|
172
|
-
### v2.2.2
|
|
173
|
-
|
|
174
|
-
* Added @jsdevel's additionalProperties (#14) fixes (thanks!)
|
|
175
|
-
|
|
176
|
-
### v2.2.1
|
|
177
|
-
|
|
178
|
-
* Add default value (#30).
|
|
179
|
-
|
|
180
|
-
### v2.2.0
|
|
181
|
-
|
|
182
|
-
* Added support for directly passing a `string` instead of a schema to indicate type.
|
|
183
|
-
|
|
184
|
-
### v2.1.0
|
|
185
|
-
|
|
186
|
-
* added support for mapping title (to label in joi).
|
|
187
|
-
* added support for mapping description.
|
|
188
|
-
* fixed engine version.
|
|
189
|
-
|
|
190
|
-
### v2.0.0
|
|
191
|
-
|
|
192
|
-
* updated `joi` to ^9.
|
|
193
|
-
* requires node 4 minimum.
|
|
194
|
-
|
|
195
|
-
### v1.0.4
|
|
196
|
-
|
|
197
|
-
* when undefined `minLength` should default to 0.
|
|
198
|
-
|
|
199
|
-
### v1.0.3
|
|
200
|
-
|
|
201
|
-
* Validate when `additionalProperties` is boolean.
|
|
202
|
-
|
|
203
|
-
### v1.0.2
|
|
204
|
-
|
|
205
|
-
* Support for `oneOf`.
|
|
206
|
-
|
|
207
|
-
### v1.0.1
|
|
208
|
-
|
|
209
|
-
* Support for `format` in string types.
|
|
210
|
-
|
|
211
|
-
### v1.0.0
|
|
212
|
-
|
|
213
|
-
* [BREAKING] `subSchemas` is now passed as a property in `options`.
|
|
214
|
-
* Support for custom types using `options.types`.
|