joi-to-json 2.2.1 → 2.2.4

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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ## Objective
4
4
 
5
- I have been using [joi](https://hapi.dev/family/joi/) a lot in different Node.js projects which guards the API.
5
+ I have been using [joi](https://joi.dev/) a lot in different Node.js projects to guard the API.
6
6
  It's **The most powerful schema description language and data validator for JavaScript.** as it said.
7
7
 
8
8
  Many times, we need to utilize this schema description to produce other output, such as Swagger OpenAPI doc.
@@ -103,6 +103,10 @@ You can optionally set below environment variables:
103
103
 
104
104
  * `CASE_PATTERN=joi-obj-12` to control which version of joi obj to test
105
105
 
106
+ ## Known Limitation
107
+
108
+ * For `object.pattern` usage in Joi, `pattern` parameter can only be a regular expression now as I cannot convert Joi object to regex yet.
109
+
106
110
  ## License
107
111
 
108
112
  MIT
@@ -96,14 +96,18 @@ class JoiJsonSchemaParser {
96
96
  }
97
97
 
98
98
  _getEnum(fieldDefn) {
99
- if (_.isEmpty(fieldDefn[this.enumFieldName])) {
100
- return undefined
99
+ const enumList = fieldDefn[this.enumFieldName]
100
+ if (fieldDefn.flags && fieldDefn.flags.only && !_.isEmpty(enumList)) {
101
+ return _.uniq(enumList)
101
102
  }
103
+ }
102
104
 
103
- const enumList = _.filter(fieldDefn[this.enumFieldName], (item) => {
104
- return !_.isEmpty(item)
105
- })
106
- return _.isEmpty(enumList) ? undefined : enumList
105
+ _getUnknown(joiSpec) {
106
+ let allowUnknown = _.get(joiSpec, `${this.optionsFieldName}.allowUnknown`, false)
107
+ if (joiSpec.flags && typeof joiSpec.flags[this.allowUnknownFlagName] !== 'undefined') {
108
+ allowUnknown = joiSpec.flags[this.allowUnknownFlagName]
109
+ }
110
+ return allowUnknown
107
111
  }
108
112
 
109
113
  _setIfNotEmpty(schema, field, value) {
@@ -139,10 +143,7 @@ class JoiJsonSchemaParser {
139
143
  schema.properties = {}
140
144
  schema.required = []
141
145
 
142
- schema.additionalProperties = _.get(joiSpec, `${this.optionsFieldName}.allowUnknown`, false)
143
- if (joiSpec.flags && typeof joiSpec.flags[this.allowUnknownFlagName] !== 'undefined') {
144
- schema.additionalProperties = joiSpec.flags[this.allowUnknownFlagName]
145
- }
146
+ schema.additionalProperties = this._getUnknown(joiSpec)
146
147
 
147
148
  _.map(joiSpec[this.childrenFieldName], (fieldDefn, key) => {
148
149
  const fieldSchema = this.parse(fieldDefn)
@@ -157,9 +158,9 @@ class JoiJsonSchemaParser {
157
158
  * For dynamic key scenarios to store the pattern as key
158
159
  * and have the properties be as with other examples
159
160
  */
160
- if (joiSpec.patterns) {
161
+ if (joiSpec.patterns) {
161
162
  _.each(joiSpec.patterns, patternObj => {
162
- if (typeof patternObj.rule !== 'object') {
163
+ if (typeof patternObj.rule !== 'object' || typeof patternObj.regex === 'undefined') {
163
164
  return
164
165
  }
165
166
 
@@ -170,6 +171,7 @@ class JoiJsonSchemaParser {
170
171
  schema.properties[patternObj.regex].required = []
171
172
 
172
173
  const childKeys = patternObj.rule.keys || patternObj.rule.children
174
+ schema.properties[patternObj.regex].additionalProperties = this._getUnknown(patternObj.rule)
173
175
 
174
176
  _.each(childKeys, (ruleObj, key) => {
175
177
  schema.properties[patternObj.regex].properties[key] = this.parse(ruleObj)
@@ -178,6 +180,14 @@ class JoiJsonSchemaParser {
178
180
  schema.properties[patternObj.regex].required.push(key)
179
181
  }
180
182
  })
183
+
184
+ schema.patternProperties = schema.patternProperties || {}
185
+
186
+ let regexString = patternObj.regex
187
+ regexString = regexString.indexOf('/') === 0 ? regexString.substring(1) : regexString
188
+ regexString = regexString.lastIndexOf('/') > -1 ? regexString.substring(0, regexString.length - 1) : regexString
189
+
190
+ schema.patternProperties[regexString] = schema.properties[patternObj.regex]
181
191
  })
182
192
  }
183
193
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "joi-to-json",
3
- "version": "2.2.1",
3
+ "version": "2.2.4",
4
4
  "description": "joi to JSON / OpenAPI Schema Converter",
5
5
  "main": "index.js",
6
6
  "repository": {