joi-to-json 4.2.0 → 4.2.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/README.md +15 -3
- package/lib/parsers/json.js +3 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -46,9 +46,12 @@ Although the versions chosen are the latest one for each major version, It shoul
|
|
|
46
46
|
|
|
47
47
|
## Usage
|
|
48
48
|
|
|
49
|
-
Only one API `parse` is available.
|
|
49
|
+
Only one API `parse` is available.
|
|
50
|
+
|
|
51
|
+
Its signature is `parse(joiObj, type = 'json', definitions = {}, parserOptions = {})`
|
|
52
|
+
|
|
53
|
+
### Output Type
|
|
50
54
|
|
|
51
|
-
Currently supported output types:
|
|
52
55
|
* `json` - Default. Stands for JSON Schema Draft 07
|
|
53
56
|
* `open-api` - Stands for OpenAPI 3.0 Schema - an extended subset of JSON Schema Specification Wright Draft 00 (aka Draft 5)
|
|
54
57
|
* `open-api-3.1` - Stands for OpenAPI 3.1 Schema - a superset of JSON Schema Specification Draft 2020-12
|
|
@@ -91,6 +94,15 @@ const jsonSchema = parse(joiSchema)
|
|
|
91
94
|
// const openApiSchema = parse(joiSchema, 'open-api')
|
|
92
95
|
```
|
|
93
96
|
|
|
97
|
+
### definitions
|
|
98
|
+
|
|
99
|
+
This should be a JSON object containing all schemas referenced by the `joiObj` definition. It's useful if **Named Link** case is used but the referenced schemas are provided externally. This object uses the schema id as key and schema itself as value.
|
|
100
|
+
|
|
101
|
+
### parserOptions
|
|
102
|
+
|
|
103
|
+
* `includeSchemaDialect`: Default to be `false`. `true` makes the parsed schema containing `$schema` field automatically. Value of the `$schema` is default for different output JSON format if it's not provided in options together.
|
|
104
|
+
* `logicalOpParser`: Refer to **Special Joi Operator Support** below for detail usage.
|
|
105
|
+
|
|
94
106
|
## Features
|
|
95
107
|
|
|
96
108
|
### Special Joi Operator Support
|
|
@@ -105,7 +117,7 @@ Supports named link for schema reuse, such as `.link('#person')`. **For `open-a
|
|
|
105
117
|
|
|
106
118
|
Starting from Draft 7, JSON Specification supports `If-Then-Else` style expression. Before that, we can also use something called [Implication](http://json-schema.org/understanding-json-schema/reference/conditionals.html#implication) using Schema Composition Approach to simulate that.
|
|
107
119
|
|
|
108
|
-
By default, the `If-Then-Else` approach is used if the output spec supports it. However, if the joi conditional expression (`alternatives` or `when`) is annotated using Meta `.meta({ 'if-style':
|
|
120
|
+
By default, the `If-Then-Else` approach is used if the output spec supports it. However, if the joi conditional expression (`alternatives` or `when`) is annotated using Meta `.meta({ 'if-style': false })`, the JSON schema conversion will use the Composition approach using `allOf` and/or `anyOf` instead.
|
|
109
121
|
|
|
110
122
|
**Limitation**: Currently, if the joi condition definition is referring to another field, the `If-Then-Else` style output is not supported. Instead, it simply uses the `anyOf` composing the `then` and `otherwise` on the defined field.
|
|
111
123
|
|
package/lib/parsers/json.js
CHANGED
|
@@ -121,7 +121,9 @@ class JoiJsonSchemaParser {
|
|
|
121
121
|
this._addNullTypeIfNullable(schema, joiSpec)
|
|
122
122
|
|
|
123
123
|
if (!_.isEmpty(joiSpec.shared)) {
|
|
124
|
-
|
|
124
|
+
_.forEach(joiSpec.shared, (sharedSchema) => {
|
|
125
|
+
this.parse(sharedSchema, definitions, level)
|
|
126
|
+
})
|
|
125
127
|
}
|
|
126
128
|
|
|
127
129
|
const schemaId = _.get(joiSpec, 'flags.id')
|