json-2-joi 2.2.0 → 2.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/CHANGELOG.md +12 -0
- package/README.md +3 -1
- package/lib/resolver.js +24 -8
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
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
|
+
|
|
1
13
|
### 2.2.0
|
|
2
14
|
|
|
3
15
|
- Add `forbidArrayNull`, `strictRequired` option.
|
package/README.md
CHANGED
|
@@ -49,10 +49,11 @@ Please file issues for other unsupported features.
|
|
|
49
49
|
### Options
|
|
50
50
|
|
|
51
51
|
- `subSchemas` - an (optional) object with keys representing schema ids, and values representing schemas.
|
|
52
|
+
- `extensions` - an array of extensions to pass [joi.extend](https://github.com/hapijs/joi/blob/master/API.md#extendextension).
|
|
53
|
+
- `joiOptions` - Options directly passed to Joi for instance creation. If the `noDefaults` is set `true`, `default()` will NOT be called and applied to Joi schema.
|
|
52
54
|
- `refineDescription(schema)` - an (optional) function to call to apply to the JSON schema so that it's possible to return customized description.
|
|
53
55
|
- `refineType(type, format)` - an (optional) function to call to apply to type based on the type and format of the JSON schema.
|
|
54
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
|
|
55
|
-
- `extensions` - an array of extensions to pass [joi.extend](https://github.com/hapijs/joi/blob/master/API.md#extendextension).
|
|
56
57
|
- `allowNull` - Default as `false`. When `true` and the field has no `enum` setting, `null` value will be allowed.
|
|
57
58
|
- `forbidArrayNull` - Default as `true`. When `false` and `allowNull` is true, `null` value will be allowed for array type field.
|
|
58
59
|
- `strictRequired` - Default as `false`. When `true`, `null` (and empty string additionally for string type) will NOT be allowed for those `required` field in object.
|
|
@@ -222,6 +223,7 @@ It's possible to override these options for each conversion:
|
|
|
222
223
|
* strictRequired
|
|
223
224
|
* strictEnum
|
|
224
225
|
* enableEnum
|
|
226
|
+
* noDefaults (Copied from `joiOptions` for the construction time)
|
|
225
227
|
|
|
226
228
|
However, the overriden options do not have impact on the `subSchemas` as they are preprocessed during Resolver construction.
|
|
227
229
|
|
package/lib/resolver.js
CHANGED
|
@@ -34,6 +34,7 @@ const overridableOptions = {
|
|
|
34
34
|
allowNull: Joi.boolean(),
|
|
35
35
|
forbidArrayNull: Joi.boolean(),
|
|
36
36
|
strictRequired: Joi.boolean(),
|
|
37
|
+
noDefaults: Joi.boolean(),
|
|
37
38
|
strictEnum: Joi.boolean(),
|
|
38
39
|
enableEnum: Joi.boolean()
|
|
39
40
|
};
|
|
@@ -85,10 +86,10 @@ function addEnumRestriction(joiSchema, enumList, { allowNull = false, strictEnum
|
|
|
85
86
|
|
|
86
87
|
const valids = [].concat(enumList);
|
|
87
88
|
if (allowNull && !strictEnum) {
|
|
89
|
+
valids.splice(0, 0, null);
|
|
88
90
|
if (joiSchema.type === 'string') {
|
|
89
|
-
valids.
|
|
91
|
+
valids.splice(0, 0, '');
|
|
90
92
|
}
|
|
91
|
-
valids.push(null);
|
|
92
93
|
}
|
|
93
94
|
return joiSchema.valid(...valids);
|
|
94
95
|
}
|
|
@@ -161,6 +162,7 @@ class SchemaResolver {
|
|
|
161
162
|
this.subSchemas = subSchemas;
|
|
162
163
|
this.joiOptions = joiOptions;
|
|
163
164
|
this.joiSharedSchemas = new Map();
|
|
165
|
+
this.resolveOptions.noDefaults = this.joiOptions.noDefaults;
|
|
164
166
|
|
|
165
167
|
this.joi = getJoiInstance(joiOptions, extensions, joiInstance);
|
|
166
168
|
|
|
@@ -288,7 +290,7 @@ class SchemaResolver {
|
|
|
288
290
|
resolvedSchema = resolveOptions.refineSchema(resolvedSchema, schema);
|
|
289
291
|
}
|
|
290
292
|
|
|
291
|
-
if (schema.default !== undefined) {
|
|
293
|
+
if (schema.default !== undefined && !resolveOptions.noDefaults) {
|
|
292
294
|
resolvedSchema = resolvedSchema.default(schema.default)
|
|
293
295
|
}
|
|
294
296
|
|
|
@@ -355,11 +357,6 @@ class SchemaResolver {
|
|
|
355
357
|
resolveType(schema, resolveOptions, baseUri) {
|
|
356
358
|
let joischema;
|
|
357
359
|
|
|
358
|
-
const typeDefinitionMap = {
|
|
359
|
-
title: 'label',
|
|
360
|
-
default: 'default'
|
|
361
|
-
};
|
|
362
|
-
|
|
363
360
|
const joitype = (type, options, format) => {
|
|
364
361
|
let joischema;
|
|
365
362
|
|
|
@@ -396,6 +393,19 @@ class SchemaResolver {
|
|
|
396
393
|
return joischema;
|
|
397
394
|
}
|
|
398
395
|
|
|
396
|
+
let nullIdx;
|
|
397
|
+
if (Array.isArray(schema.type)) {
|
|
398
|
+
nullIdx = schema.type.findIndex((type) => type === 'null');
|
|
399
|
+
if (nullIdx > -1) {
|
|
400
|
+
schema.type.splice(nullIdx, 1);
|
|
401
|
+
}
|
|
402
|
+
if (schema.type.length === 1) {
|
|
403
|
+
schema.type = schema.type[0];
|
|
404
|
+
} else if (schema.type.length === 0) {
|
|
405
|
+
schema.type = 'null';
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
399
409
|
if (Array.isArray(schema.type)) {
|
|
400
410
|
const schemas = [];
|
|
401
411
|
|
|
@@ -407,7 +417,13 @@ class SchemaResolver {
|
|
|
407
417
|
} else {
|
|
408
418
|
joischema = joitype(schema.type, resolveOptions, schema.format);
|
|
409
419
|
}
|
|
420
|
+
if (nullIdx > -1) {
|
|
421
|
+
joischema = joischema.allow(null);
|
|
422
|
+
}
|
|
410
423
|
|
|
424
|
+
const typeDefinitionMap = {
|
|
425
|
+
title: 'label'
|
|
426
|
+
};
|
|
411
427
|
Object.keys(typeDefinitionMap).forEach(function (key) {
|
|
412
428
|
if (schema[key] !== undefined) {
|
|
413
429
|
joischema = joischema[typeDefinitionMap[key]](schema[key]);
|