json-2-joi 1.1.0 → 2.0.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 +13 -0
- package/README.md +4 -4
- package/lib/resolver.js +26 -27
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
### 2.0.2
|
|
2
|
+
|
|
3
|
+
- Remove `min` setting from JOI schema if `minLength` is not defined in JSON schema.
|
|
4
|
+
- Allow `null` value as Array's item if `allowNull` option is set to true.
|
|
5
|
+
|
|
6
|
+
### 2.0.1
|
|
7
|
+
|
|
8
|
+
- Set correct examples data for Array type field.
|
|
9
|
+
|
|
10
|
+
### 2.0.0
|
|
11
|
+
|
|
12
|
+
* [BREAKING] Remove `useDefaults` and `strictMode` options
|
|
13
|
+
|
|
1
14
|
### 1.1.0
|
|
2
15
|
|
|
3
16
|
- Support providing `joiOptions` for JOI default instance creation.
|
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
Converts a JSON schema to a Joi schema for object validation.
|
|
6
6
|
|
|
7
|
-
## Change based on the forked version (
|
|
7
|
+
## Change based on the forked version (enjoi):
|
|
8
8
|
|
|
9
9
|
- Provided `refineDescription(schema)`, `allowNull` options
|
|
10
10
|
- Change cycle reference resovling without self-generating id and rely on $id if defined in JSON schema
|
|
@@ -15,6 +15,7 @@ Converts a JSON schema to a Joi schema for object validation.
|
|
|
15
15
|
- Support providing `joiOptions` for JOI default instance creation.
|
|
16
16
|
- Support directly passing a Joi instance for schema building.
|
|
17
17
|
- Remove `defaults` API. Please refer to section **Reuse JOI Schema Resolver** for similar usage.
|
|
18
|
+
- Remove `strictMode` and `useDefaults` option.
|
|
18
19
|
- **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:
|
|
19
20
|
* id
|
|
20
21
|
* [baseUri]#anchor
|
|
@@ -53,7 +54,6 @@ Please file issues for other unsupported features.
|
|
|
53
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
|
|
54
55
|
- `extensions` - an array of extensions to pass [joi.extend](https://github.com/hapijs/joi/blob/master/API.md#extendextension).
|
|
55
56
|
- `allowNull` - Allows null value when the field is NOT required with a default value of `false`.
|
|
56
|
-
- `strictMode` - make schemas `strict(value)` with a default value of `false`.
|
|
57
57
|
|
|
58
58
|
Example:
|
|
59
59
|
|
|
@@ -89,9 +89,9 @@ schema.additionalProperties !== false
|
|
|
89
89
|
When this option is provided, a Joi instance will be created as `joiInstance = Joi.defaults((schema) => schema.options(joiOptions));` for building Joi Schema.
|
|
90
90
|
|
|
91
91
|
As a result:
|
|
92
|
-
* `useDefaults` will take effect only if `noDefaults` is NOT true in `joiOptions`.
|
|
93
92
|
* `unknown` setting (originally set by schema.additionalProperties !== false) of the schema will take effect only if `allowUnknown` is NOT provided in `joiOptions`.
|
|
94
|
-
|
|
93
|
+
* Original `useDefaults` option will be replaced by `noDefaults` in `joiOptions`.
|
|
94
|
+
* Original `strictMode` option will be replaced by `convert` in `joiOptions`.
|
|
95
95
|
|
|
96
96
|
### Sub Schemas
|
|
97
97
|
|
package/lib/resolver.js
CHANGED
|
@@ -20,9 +20,7 @@ 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
|
-
joiOptions: Joi.object().unknown(true).default({})
|
|
24
|
-
strictMode: Joi.boolean().default(false),
|
|
25
|
-
useDefaults: Joi.boolean().default(false)
|
|
23
|
+
joiOptions: Joi.object().unknown(true).default({})
|
|
26
24
|
});
|
|
27
25
|
|
|
28
26
|
function isNumber(value) {
|
|
@@ -37,6 +35,13 @@ function isNotEmptyObject(value) {
|
|
|
37
35
|
return isObject(value) && Object.keys(value).length > 0;
|
|
38
36
|
}
|
|
39
37
|
|
|
38
|
+
function allowNullIfNeeded(schema, allowNull = false) {
|
|
39
|
+
if (allowNull) {
|
|
40
|
+
return schema.allow(null);
|
|
41
|
+
}
|
|
42
|
+
return schema;
|
|
43
|
+
}
|
|
44
|
+
|
|
40
45
|
function normalizedId(id) {
|
|
41
46
|
return id.replace(/[^a-zA-Z0-9]/g, '_');
|
|
42
47
|
}
|
|
@@ -99,14 +104,12 @@ class SchemaResolver {
|
|
|
99
104
|
|
|
100
105
|
Hoek.assert(!validatedResult.error, validatedResult.error);
|
|
101
106
|
|
|
102
|
-
const { subSchemas = {}, refineType, refineSchema,
|
|
107
|
+
const { subSchemas = {}, refineType, refineSchema, extensions = [], allowNull = false, refineDescription, joiOptions } = validatedResult.value;
|
|
103
108
|
|
|
104
109
|
this.subSchemas = subSchemas;
|
|
105
110
|
this.refineType = refineType;
|
|
106
111
|
this.refineDescription = refineDescription;
|
|
107
112
|
this.refineSchema = refineSchema;
|
|
108
|
-
this.strictMode = strictMode;
|
|
109
|
-
this.useDefaults = useDefaults;
|
|
110
113
|
this.allowNull = allowNull;
|
|
111
114
|
this.joiOptions = joiOptions;
|
|
112
115
|
this.joiSharedSchemas = new Map();
|
|
@@ -232,7 +235,7 @@ class SchemaResolver {
|
|
|
232
235
|
resolvedSchema = this.refineSchema(resolvedSchema, schema);
|
|
233
236
|
}
|
|
234
237
|
|
|
235
|
-
if (
|
|
238
|
+
if (schema.default !== undefined) {
|
|
236
239
|
resolvedSchema = resolvedSchema.default(schema.default)
|
|
237
240
|
}
|
|
238
241
|
|
|
@@ -337,7 +340,7 @@ class SchemaResolver {
|
|
|
337
340
|
|
|
338
341
|
Hoek.assert(joischema, 'Could not resolve type: ' + schema.type + '.');
|
|
339
342
|
|
|
340
|
-
return joischema
|
|
343
|
+
return joischema;
|
|
341
344
|
}
|
|
342
345
|
|
|
343
346
|
if (Array.isArray(schema.type)) {
|
|
@@ -377,7 +380,7 @@ class SchemaResolver {
|
|
|
377
380
|
example = schema.enum[0];
|
|
378
381
|
}
|
|
379
382
|
if (example) {
|
|
380
|
-
joischema = joischema.example(example);
|
|
383
|
+
joischema = joischema.type === 'array' && !Array.isArray(example) ? joischema.example([example]) : joischema.example(example);
|
|
381
384
|
}
|
|
382
385
|
|
|
383
386
|
return joischema;
|
|
@@ -430,8 +433,8 @@ class SchemaResolver {
|
|
|
430
433
|
|
|
431
434
|
if (Array.isArray(schema.required) && !!~schema.required.indexOf(key)) {
|
|
432
435
|
joischema = joischema.required();
|
|
433
|
-
} else
|
|
434
|
-
joischema = joischema.
|
|
436
|
+
} else {
|
|
437
|
+
joischema = allowNullIfNeeded(joischema, this.allowNull);
|
|
435
438
|
}
|
|
436
439
|
|
|
437
440
|
schemas[key] = joischema;
|
|
@@ -454,25 +457,25 @@ class SchemaResolver {
|
|
|
454
457
|
return joischema;
|
|
455
458
|
}
|
|
456
459
|
|
|
460
|
+
resolveAsArray(value, baseUri) {
|
|
461
|
+
if (Array.isArray(value)) {
|
|
462
|
+
// found an array, thus its _per type_
|
|
463
|
+
return value.map((v) => allowNullIfNeeded(this.resolve(v, baseUri), this.allowNull));
|
|
464
|
+
}
|
|
465
|
+
// it's a single entity, so just resolve it normally
|
|
466
|
+
return [allowNullIfNeeded(this.resolve(value, baseUri), this.allowNull)];
|
|
467
|
+
}
|
|
468
|
+
|
|
457
469
|
array(schema, baseUri) {
|
|
458
470
|
let joischema = this.joi.array();
|
|
459
471
|
let items;
|
|
460
472
|
|
|
461
|
-
const resolveAsArray = (value) => {
|
|
462
|
-
if (Array.isArray(value)) {
|
|
463
|
-
// found an array, thus its _per type_
|
|
464
|
-
return value.map((v) => this.resolve(v, baseUri));
|
|
465
|
-
}
|
|
466
|
-
// it's a single entity, so just resolve it normally
|
|
467
|
-
return [this.resolve(value, baseUri)];
|
|
468
|
-
}
|
|
469
|
-
|
|
470
473
|
if (schema.items) {
|
|
471
|
-
items = resolveAsArray(schema.items);
|
|
474
|
+
items = this.resolveAsArray(schema.items, baseUri);
|
|
472
475
|
|
|
473
476
|
joischema = joischema.items(...items);
|
|
474
477
|
} else if (schema.ordered) {
|
|
475
|
-
items = resolveAsArray(schema.ordered);
|
|
478
|
+
items = this.resolveAsArray(schema.ordered, baseUri);
|
|
476
479
|
joischema = joischema.ordered(...items);
|
|
477
480
|
}
|
|
478
481
|
|
|
@@ -558,11 +561,7 @@ class SchemaResolver {
|
|
|
558
561
|
regularString(schema, joischema) {
|
|
559
562
|
schema.pattern && (joischema = joischema.regex(new RegExp(schema.pattern)));
|
|
560
563
|
|
|
561
|
-
if (typeof schema.minLength === 'undefined') {
|
|
562
|
-
schema.minLength = 0;
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
if (schema.minLength === 0 && !schema.pattern && !schema.format) {
|
|
564
|
+
if ((typeof schema.minLength === 'undefined' || schema.minLength === 0) && !schema.pattern && !schema.format) {
|
|
566
565
|
joischema = joischema.allow('');
|
|
567
566
|
}
|
|
568
567
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-2-joi",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"license": "Apache 2.0",
|
|
5
|
-
"description": "Converts json-schema to Joi schema.",
|
|
5
|
+
"description": "Converts json-schema to Joi schema. (Enhanced enjoi)",
|
|
6
6
|
"main": "./index.js",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "tape test/*.js",
|