json-2-joi 2.2.0 → 2.3.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/README.md +3 -1
- package/lib/resolver.js +6 -5
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
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
|
|
|
@@ -356,8 +358,7 @@ class SchemaResolver {
|
|
|
356
358
|
let joischema;
|
|
357
359
|
|
|
358
360
|
const typeDefinitionMap = {
|
|
359
|
-
title: 'label'
|
|
360
|
-
default: 'default'
|
|
361
|
+
title: 'label'
|
|
361
362
|
};
|
|
362
363
|
|
|
363
364
|
const joitype = (type, options, format) => {
|