joi-to-json 4.2.1 → 4.3.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.
package/index.d.ts CHANGED
@@ -3,7 +3,7 @@ import Joi from 'joi-17';
3
3
  /**
4
4
  * @type {string}
5
5
  */
6
- export type Mode = 'json' | 'open-api' | 'json-draft-2019-09' | 'json-draft-04';
6
+ export type Mode = 'json' | 'open-api' | 'open-api-3.1' | 'json-draft-2019-09' | 'json-draft-04';
7
7
 
8
8
  /**
9
9
  * The parser function modifies the schema in-place.
@@ -8,20 +8,8 @@ class JoiJsonDraftSchemaParser extends JoiJsonSchemaParser {
8
8
  }, opts))
9
9
  }
10
10
 
11
- parse(joiSpec, definitions = {}, level = 0) {
12
- const schema = super.parse(joiSpec, definitions, level)
13
-
14
- if (!_.isEmpty(joiSpec.metas)) {
15
- _.each(joiSpec.metas, meta => {
16
- _.each(meta, (value, key) => {
17
- if (key === 'deprecated' || key === 'readOnly' || key === 'writeOnly') {
18
- schema[key] = value
19
- }
20
- })
21
- })
22
- }
23
-
24
- return schema
11
+ _isKnownMetaKey(key) {
12
+ return key === 'deprecated' || key === 'readOnly' || key === 'writeOnly'
25
13
  }
26
14
  }
27
15
 
@@ -75,9 +75,9 @@ const LOGICAL_OP_PARSER = {
75
75
  */
76
76
  function isJoiOverride(e) {
77
77
  return typeof e === 'object'
78
- && e !== null
79
- && Object.keys(e).length === 1
80
- && e.override === true
78
+ && e !== null
79
+ && Object.keys(e).length === 1
80
+ && e.override === true
81
81
  }
82
82
 
83
83
  class JoiJsonSchemaParser {
@@ -126,6 +126,8 @@ class JoiJsonSchemaParser {
126
126
  })
127
127
  }
128
128
 
129
+ this._copyMetasToSchema(joiSpec, schema)
130
+
129
131
  const schemaId = _.get(joiSpec, 'flags.id')
130
132
  if (schemaId) {
131
133
  definitions[schemaId] = schema
@@ -775,6 +777,25 @@ class JoiJsonSchemaParser {
775
777
  delete schema.type
776
778
  }
777
779
  }
780
+
781
+ _copyMetasToSchema(joiSpec, schema) {
782
+ if (!_.isEmpty(joiSpec.metas)) {
783
+ _.each(joiSpec.metas, meta => {
784
+ _.each(meta, (value, key) => {
785
+ if (this._isKnownMetaKey(key)) {
786
+ schema[key] = value
787
+ }
788
+ })
789
+ })
790
+ }
791
+ }
792
+
793
+ // Intended to be overridden by child parsers. By default no meta key will be
794
+ // is considered "known".
795
+ // eslint-disable-next-line no-unused-vars
796
+ _isKnownMetaKey(key) {
797
+ return false
798
+ }
778
799
  }
779
800
 
780
801
  module.exports = JoiJsonSchemaParser
@@ -11,16 +11,6 @@ class JoiOpenApiSchemaParser extends JoiJsonSchemaParser {
11
11
  parse(joiSpec, definitions = {}, level = 0) {
12
12
  const schema = super.parse(joiSpec, definitions, level)
13
13
 
14
- if (!_.isEmpty(joiSpec.metas)) {
15
- _.each(joiSpec.metas, meta => {
16
- _.each(meta, (value, key) => {
17
- if (key.startsWith('x-') || key === 'deprecated' || key === 'readOnly' || key === 'writeOnly') {
18
- schema[key] = value
19
- }
20
- })
21
- })
22
- }
23
-
24
14
  if (level === 0 && !_.isEmpty(definitions)) {
25
15
  schema.schemas = definitions
26
16
  }
@@ -32,6 +22,10 @@ class JoiOpenApiSchemaParser extends JoiJsonSchemaParser {
32
22
  _getLocalSchemaBasePath() {
33
23
  return '#/components/schemas'
34
24
  }
25
+
26
+ _isKnownMetaKey(key) {
27
+ return key.startsWith('x-') || key === 'deprecated' || key === 'readOnly' || key === 'writeOnly'
28
+ }
35
29
  }
36
30
 
37
31
  module.exports = JoiOpenApiSchemaParser
@@ -16,50 +16,7 @@ class JoiOpenApiSchemaParser extends JoiJsonSchemaParser {
16
16
 
17
17
  parse(joiSpec, definitions = {}, level = 0) {
18
18
  const fullSchema = super.parse(joiSpec, definitions, level)
19
- const schema = _.pick(fullSchema, [
20
- '$ref',
21
- 'title',
22
- 'multipleOf',
23
- 'maximum',
24
- 'exclusiveMaximum',
25
- 'minimum',
26
- 'exclusiveMinimum',
27
- 'maxLength',
28
- 'minLength',
29
- 'pattern',
30
- 'maxItems',
31
- 'minItems',
32
- 'uniqueItems',
33
- 'maxProperties',
34
- 'minProperties',
35
- 'required',
36
- 'enum',
37
- 'description',
38
- 'format',
39
- 'default',
40
- 'type',
41
-
42
- 'allOf',
43
- 'oneOf',
44
- 'anyOf',
45
- 'not',
46
- 'items',
47
- 'properties',
48
- 'additionalProperties',
49
-
50
- 'example',
51
- 'nullable'
52
- ])
53
-
54
- if (!_.isEmpty(joiSpec.metas)) {
55
- _.each(joiSpec.metas, meta => {
56
- _.each(meta, (value, key) => {
57
- if (key.startsWith('x-') || key === 'deprecated' || key === 'readOnly' || key === 'writeOnly') {
58
- schema[key] = value
59
- }
60
- })
61
- })
62
- }
19
+ const schema = _.pickBy(fullSchema, (value, key) => isKnownKey(key))
63
20
 
64
21
  if (level === 0 && !_.isEmpty(definitions)) {
65
22
  schema.schemas = definitions
@@ -126,6 +83,52 @@ class JoiOpenApiSchemaParser extends JoiJsonSchemaParser {
126
83
  delete fieldSchema.type
127
84
  }
128
85
  }
86
+
87
+ _isKnownMetaKey(key) {
88
+ return isKnownMetaKey(key)
89
+ }
90
+ }
91
+
92
+ function isKnownMetaKey(key) {
93
+ return key.startsWith('x-') || key === 'deprecated' || key === 'readOnly' || key === 'writeOnly'
94
+ }
95
+
96
+ function isKnownKey(key) {
97
+ const knownKeys = new Set([
98
+ '$ref',
99
+ 'title',
100
+ 'multipleOf',
101
+ 'maximum',
102
+ 'exclusiveMaximum',
103
+ 'minimum',
104
+ 'exclusiveMinimum',
105
+ 'maxLength',
106
+ 'minLength',
107
+ 'pattern',
108
+ 'maxItems',
109
+ 'minItems',
110
+ 'uniqueItems',
111
+ 'maxProperties',
112
+ 'minProperties',
113
+ 'required',
114
+ 'enum',
115
+ 'description',
116
+ 'format',
117
+ 'default',
118
+ 'type',
119
+
120
+ 'allOf',
121
+ 'oneOf',
122
+ 'anyOf',
123
+ 'not',
124
+ 'items',
125
+ 'properties',
126
+ 'additionalProperties',
127
+
128
+ 'example',
129
+ 'nullable'
130
+ ])
131
+ return knownKeys.has(key) || isKnownMetaKey(key)
129
132
  }
130
133
 
131
- module.exports = JoiOpenApiSchemaParser
134
+ module.exports = JoiOpenApiSchemaParser
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "joi-to-json",
3
- "version": "4.2.1",
3
+ "version": "4.3.1",
4
4
  "description": "joi to JSON / OpenAPI Schema Converter",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -28,11 +28,11 @@
28
28
  "semver-compare": "^1.0.0"
29
29
  },
30
30
  "devDependencies": {
31
- "@jest/transform": "^29.6.2",
32
- "ajv": "^8.12.0",
31
+ "@jest/transform": "^29.7.0",
32
+ "ajv": "^8.16.0",
33
33
  "ajv-draft-04": "^1.0.0",
34
- "eslint": "^8.47.0",
35
- "jest": "^29.6.2",
34
+ "eslint": "^8.57.0",
35
+ "jest": "^29.7.0",
36
36
  "joi-12": "npm:@commercial/joi@^12.1.0",
37
37
  "joi-13": "npm:joi@^13.7.0",
38
38
  "joi-14": "npm:joi@^14.3.1",