json-2-joi 2.5.1 → 3.0.1

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.
Files changed (2) hide show
  1. package/lib/resolver.js +26 -6
  2. package/package.json +10 -11
package/lib/resolver.js CHANGED
@@ -1,4 +1,3 @@
1
- /* global Map */
2
1
  const Joi = require('joi');
3
2
  const Hoek = require('@hapi/hoek');
4
3
  const Bourne = require('@hapi/bourne');
@@ -124,7 +123,9 @@ function getJoiInstance(joiOptions, extensions, joiInstance) {
124
123
  }
125
124
  try {
126
125
  return { value: Bourne.parse(value) };
127
- } catch (ignoreErr) { } // eslint-disable-line no-empty
126
+ } catch (ignoreErr) { /* eslint-disable-line no-unused-vars */
127
+ // Purposefully ignoring error
128
+ }
128
129
  }
129
130
  }
130
131
  },
@@ -139,7 +140,9 @@ function getJoiInstance(joiOptions, extensions, joiInstance) {
139
140
  }
140
141
  try {
141
142
  return { value: Bourne.parse(value) };
142
- } catch (ignoreErr) { } // eslint-disable-line no-empty
143
+ } catch (ignoreErr) { /* eslint-disable-line no-unused-vars */
144
+ // Purposefully ignoring error
145
+ }
143
146
  }
144
147
  }
145
148
  },
@@ -282,7 +285,6 @@ class SchemaResolver {
282
285
  }
283
286
  if (partialSchemas.length === 0) {
284
287
  //Fall through to whatever.
285
- //eslint-disable-next-line no-console
286
288
  console.warn('WARNING: schema missing a \'type\' or \'$ref\' or \'enum\': \n%s', JSON.stringify(schema, null, 2));
287
289
  //TODO: Handle better
288
290
  partialSchemas.push(this.joi.any());
@@ -298,7 +300,9 @@ class SchemaResolver {
298
300
  resolvedSchema = resolvedSchema.default(schema.default)
299
301
  }
300
302
 
301
- if (schemaId) {
303
+ resolvedSchema = this.copyMetaAnnotations(schema, resolvedSchema);
304
+
305
+ if (schemaId && !(this.joiSharedSchemas.get(schemaId))) {
302
306
  resolvedSchema = resolvedSchema.id(normalizedId(schemaId));
303
307
  this.joiSharedSchemas.set(schemaId, resolvedSchema);
304
308
  }
@@ -469,6 +473,22 @@ class SchemaResolver {
469
473
  return addEnumRestriction(joischema, schema.enum, resolveOptions);
470
474
  }
471
475
 
476
+ copyMetaAnnotations(schema, joischema) {
477
+ const metaDataNames = ['title', 'deprecated', 'readOnly', 'writeOnly'];
478
+ const metas = {};
479
+ for (const metaName of metaDataNames) {
480
+ if (typeof schema[metaName] !== 'undefined') {
481
+ metas[metaName] = schema[metaName];
482
+ }
483
+ }
484
+
485
+ if (Object.keys(metas).length === 0) {
486
+ return joischema;
487
+ }
488
+
489
+ return joischema.meta(metas);
490
+ }
491
+
472
492
  resolveOneOf(schema, resolveOptions, baseUri) {
473
493
  Hoek.assert(Array.isArray(schema.oneOf), 'Expected oneOf to be an array.');
474
494
 
@@ -524,7 +544,7 @@ class SchemaResolver {
524
544
 
525
545
  if (isObject(schema.additionalProperties)) {
526
546
  joischema = joischema.pattern(/^/, this.resolve(schema.additionalProperties, resolveOptions, baseUri));
527
- } else if (typeof this.joiOptions.allowUnknown === 'undefined') {
547
+ } else if (typeof this.joiOptions.allowUnknown === 'undefined' || typeof schema.additionalProperties !== 'undefined') {
528
548
  joischema = joischema.unknown(schema.additionalProperties !== false);
529
549
  }
530
550
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-2-joi",
3
- "version": "2.5.1",
3
+ "version": "3.0.1",
4
4
  "license": "Apache 2.0",
5
5
  "description": "Converts json-schema to Joi schema. (Enhanced enjoi)",
6
6
  "main": "./index.js",
@@ -13,9 +13,6 @@
13
13
  "type": "git",
14
14
  "url": "git://github.com/kenspirit/json-2-joi.git"
15
15
  },
16
- "engines": {
17
- "node": ">=12"
18
- },
19
16
  "keywords": [
20
17
  "json-to-joi",
21
18
  "json-2-joi",
@@ -35,17 +32,19 @@
35
32
  "homepage": "https://github.com/kenspirit/json-2-joi",
36
33
  "dependencies": {
37
34
  "@hapi/bourne": "^3.0.0",
38
- "@hapi/hoek": "^9.3.0",
39
- "debug": "^4.3.4"
35
+ "@hapi/hoek": "^11.0.4",
36
+ "debug": "^4.3.5"
40
37
  },
41
38
  "peerDependencies": {
42
- "joi": "^17.6.0"
39
+ "joi": "^17.13.3"
43
40
  },
44
41
  "devDependencies": {
45
- "joi": "^17.6.0",
46
- "eslint": "^6.8.0",
47
- "nyc": "^15.1.0",
48
- "tape": "^5.5.3"
42
+ "@eslint/js": "^9.6.0",
43
+ "eslint": "^9.6.0",
44
+ "globals": "^15.7.0",
45
+ "joi": "^17.13.3",
46
+ "nyc": "^17.0.0",
47
+ "tape": "^5.8.1"
49
48
  },
50
49
  "files": [
51
50
  "lib/**/*.js",