json-2-joi 2.1.0 → 2.2.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 +5 -1
- package/lib/resolver.js +15 -5
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
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`, `strictEnum`, `enableEnum` options
|
|
9
|
+
- Provided `refineDescription(schema)`, `allowNull`, `forbidArrayNull`, `strictRequired`, `strictEnum`, `enableEnum` 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`
|
|
@@ -54,6 +54,8 @@ Please file issues for other unsupported features.
|
|
|
54
54
|
- `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
55
|
- `extensions` - an array of extensions to pass [joi.extend](https://github.com/hapijs/joi/blob/master/API.md#extendextension).
|
|
56
56
|
- `allowNull` - Default as `false`. When `true` and the field has no `enum` setting, `null` value will be allowed.
|
|
57
|
+
- `forbidArrayNull` - Default as `true`. When `false` and `allowNull` is true, `null` value will be allowed for array type field.
|
|
58
|
+
- `strictRequired` - Default as `false`. When `true`, `null` (and empty string additionally for string type) will NOT be allowed for those `required` field in object.
|
|
57
59
|
- `strictEnum` - Default as `true`. When `false`, and `allowNull` is `true`, `null` (and empty string additionally for string type) will be added to `enum` list.
|
|
58
60
|
- `enableEnum` - Default as `true`. When `false`, `enum` restriction will not be added to Joi Schema.
|
|
59
61
|
|
|
@@ -216,6 +218,8 @@ It's possible to override these options for each conversion:
|
|
|
216
218
|
* refineSchema
|
|
217
219
|
* refineDescription
|
|
218
220
|
* allowNull
|
|
221
|
+
* forbidArrayNull
|
|
222
|
+
* strictRequired
|
|
219
223
|
* strictEnum
|
|
220
224
|
* enableEnum
|
|
221
225
|
|
package/lib/resolver.js
CHANGED
|
@@ -20,6 +20,8 @@ const optionsSchema = Joi.object({
|
|
|
20
20
|
refineSchema: Joi.func().allow(null),
|
|
21
21
|
refineDescription: Joi.func().allow(null),
|
|
22
22
|
allowNull: Joi.boolean().default(false),
|
|
23
|
+
forbidArrayNull: Joi.boolean().default(true),
|
|
24
|
+
strictRequired: Joi.boolean().default(false),
|
|
23
25
|
strictEnum: Joi.boolean().default(true),
|
|
24
26
|
enableEnum: Joi.boolean().default(true),
|
|
25
27
|
joiOptions: Joi.object().unknown(true).default({})
|
|
@@ -30,6 +32,8 @@ const overridableOptions = {
|
|
|
30
32
|
refineSchema: Joi.func().allow(null),
|
|
31
33
|
refineDescription: Joi.func().allow(null),
|
|
32
34
|
allowNull: Joi.boolean(),
|
|
35
|
+
forbidArrayNull: Joi.boolean(),
|
|
36
|
+
strictRequired: Joi.boolean(),
|
|
33
37
|
strictEnum: Joi.boolean(),
|
|
34
38
|
enableEnum: Joi.boolean()
|
|
35
39
|
};
|
|
@@ -59,13 +63,16 @@ function isNotEmptyObject(value) {
|
|
|
59
63
|
return isObject(value) && Object.keys(value).length > 0;
|
|
60
64
|
}
|
|
61
65
|
|
|
62
|
-
function allowNullIfNeeded(schema, allowNull = false) {
|
|
66
|
+
function allowNullIfNeeded(schema, { allowNull = false, forbidArrayNull = true }) {
|
|
63
67
|
if (allowNull) {
|
|
64
68
|
const definition = schema.describe();
|
|
65
69
|
if (definition.flags && definition.flags.only && definition.allow) {
|
|
66
70
|
// Already has enum restriction
|
|
67
71
|
return schema;
|
|
68
72
|
}
|
|
73
|
+
if (forbidArrayNull && schema.type === 'array') {
|
|
74
|
+
return schema;
|
|
75
|
+
}
|
|
69
76
|
return schema.allow(null);
|
|
70
77
|
}
|
|
71
78
|
return schema;
|
|
@@ -78,10 +85,10 @@ function addEnumRestriction(joiSchema, enumList, { allowNull = false, strictEnum
|
|
|
78
85
|
|
|
79
86
|
const valids = [].concat(enumList);
|
|
80
87
|
if (allowNull && !strictEnum) {
|
|
81
|
-
valids.push(null);
|
|
82
88
|
if (joiSchema.type === 'string') {
|
|
83
89
|
valids.push('');
|
|
84
90
|
}
|
|
91
|
+
valids.push(null);
|
|
85
92
|
}
|
|
86
93
|
return joiSchema.valid(...valids);
|
|
87
94
|
}
|
|
@@ -477,8 +484,11 @@ class SchemaResolver {
|
|
|
477
484
|
|
|
478
485
|
if (Array.isArray(jsonSchema.required) && !!~jsonSchema.required.indexOf(key)) {
|
|
479
486
|
joischema = joischema.required();
|
|
487
|
+
if (resolveOptions.strictRequired) {
|
|
488
|
+
joischema = joischema.invalid(null, '');
|
|
489
|
+
}
|
|
480
490
|
} else {
|
|
481
|
-
joischema = allowNullIfNeeded(joischema, resolveOptions
|
|
491
|
+
joischema = allowNullIfNeeded(joischema, resolveOptions);
|
|
482
492
|
}
|
|
483
493
|
|
|
484
494
|
schemas[key] = joischema;
|
|
@@ -505,10 +515,10 @@ class SchemaResolver {
|
|
|
505
515
|
resolveAsArray(value, resolveOptions, baseUri) {
|
|
506
516
|
if (Array.isArray(value)) {
|
|
507
517
|
// found an array, thus its _per type_
|
|
508
|
-
return value.map((v) => allowNullIfNeeded(this.resolve(v, resolveOptions, baseUri), resolveOptions
|
|
518
|
+
return value.map((v) => allowNullIfNeeded(this.resolve(v, resolveOptions, baseUri), resolveOptions));
|
|
509
519
|
}
|
|
510
520
|
// it's a single entity, so just resolve it normally
|
|
511
|
-
return [allowNullIfNeeded(this.resolve(value, resolveOptions, baseUri), resolveOptions
|
|
521
|
+
return [allowNullIfNeeded(this.resolve(value, resolveOptions, baseUri), resolveOptions)];
|
|
512
522
|
}
|
|
513
523
|
|
|
514
524
|
array(schema, resolveOptions, baseUri) {
|