Skip to content

Commit

Permalink
Collect shared schema for all element.
Browse files Browse the repository at this point in the history
  • Loading branch information
kenchen-cs committed Jun 9, 2022
1 parent ba0e48b commit d7ae706
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 29 deletions.
7 changes: 4 additions & 3 deletions fixtures/joi-obj-16.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const unitSchema = joi.object().keys({
quantity: joi.number().precision(2).positive().greater(0).less(200),
unit: joi.string().required()
}).id('unit')
const unifiedString = joi.string().id('unifiedString')

module.exports = joi.object().keys({
guid: joi.string().guid({ version: ['uuidv2', 'uuidv4'] }),
Expand All @@ -24,12 +25,12 @@ module.exports = joi.object().keys({
genderSpecific: joi.when('gender', {
is: 'Female',
then: joi.number().valid(0, 1, 2).required(),
otherwise: joi.string()
otherwise: joi.link('#unifiedString')
}),
height: joi.link('#unit').shared(unitSchema),
heightRank: joi.alternatives().conditional('height', {
switch: [
{ is: 0, then: joi.string() },
{ is: 0, then: joi.link('#unifiedString') },
{ is: joi.number().greater(160), then: joi.number() },
{ is: joi.number().greater(300), then: joi.object().keys({ name: joi.string(), level: joi.number() }) }
]
Expand Down Expand Up @@ -76,4 +77,4 @@ module.exports = joi.object().keys({
.description('Some kind of list')
.optional(),
children: joi.array().items(joi.link('#person'))
}).id('person')
}).id('person').shared(unifiedString)
7 changes: 4 additions & 3 deletions fixtures/joi-obj-17.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const unitSchema = joi.object().keys({
quantity: joi.number().precision(2).positive().greater(0).less(200),
unit: joi.string().required()
}).id('unit')
const unifiedString = joi.string().id('unifiedString')

module.exports = joi.object().keys({
guid: joi.string().guid({ version: ['uuidv2', 'uuidv4'] }),
Expand All @@ -24,7 +25,7 @@ module.exports = joi.object().keys({
genderSpecific: joi.when('gender', {
is: 'Female',
then: joi.number().valid(0, 1, 2).required(),
otherwise: joi.string()
otherwise: joi.link('#unifiedString')
}),
maleSpecific: joi.alternatives().conditional('gender', {
is: 'Male',
Expand All @@ -34,7 +35,7 @@ module.exports = joi.object().keys({
height: joi.link('#unit').shared(unitSchema),
heightRank: joi.alternatives().conditional('height', {
switch: [
{ is: 0, then: joi.string() },
{ is: 0, then: joi.link('#unifiedString') },
{ is: joi.number().greater(160), then: joi.number() },
{ is: joi.number().greater(300), then: joi.object().keys({ name: joi.string(), level: joi.number() }) }
]
Expand Down Expand Up @@ -81,4 +82,4 @@ module.exports = joi.object().keys({
.description('Some kind of list')
.optional(),
children: joi.array().items(joi.link('#person'))
}).id('person')
}).id('person').shared(unifiedString)
12 changes: 6 additions & 6 deletions lib/parsers/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ class JoiJsonSchemaParser {
this._setAnyProperties(schema, joiSpec, definitions, level)
this._addNullTypeIfNullable(schema, joiSpec)
this._setMetaProperties(schema, joiSpec)
this._setLinkFieldProperties(schema, joiSpec, definitions, level)
this._setLinkFieldProperties(schema, joiSpec)

if (!_.isEmpty(joiSpec.shared)) {
this.parse(joiSpec.shared[0], definitions, level)
}

const schemaId = _.get(joiSpec, 'flags.id')
if (schemaId) {
Expand Down Expand Up @@ -441,7 +445,7 @@ class JoiJsonSchemaParser {
})
}

_setLinkFieldProperties(schema, joiSpec, definitions, level) {
_setLinkFieldProperties(schema, joiSpec) {
if (schema.type !== 'link') {
return
}
Expand All @@ -450,10 +454,6 @@ class JoiJsonSchemaParser {
schema.$ref = `${this._getLocalSchemaBasePath()}/${joiSpec.link.ref.path.join('/')}`
delete schema.type
}

if (!_.isEmpty(joiSpec.shared)) {
this.parse(joiSpec.shared[0], definitions, level)
}
}
}

Expand Down
7 changes: 5 additions & 2 deletions outputs/json-draft-04/joi-obj-16.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"$ref": "#/definitions/person",
"definitions": {
"unifiedString": {
"type": "string"
},
"unit": {
"type": "object",
"additionalProperties": false,
Expand Down Expand Up @@ -97,7 +100,7 @@
]
},
{
"type": "string"
"$ref": "#/definitions/unifiedString"
}
]
},
Expand All @@ -110,7 +113,7 @@
"heightRank": {
"oneOf": [
{
"type": "string"
"$ref": "#/definitions/unifiedString"
},
{
"type": "number"
Expand Down
7 changes: 5 additions & 2 deletions outputs/json-draft-04/joi-obj-17.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"$ref": "#/definitions/person",
"definitions": {
"unifiedString": {
"type": "string"
},
"unit": {
"type": "object",
"additionalProperties": false,
Expand Down Expand Up @@ -97,7 +100,7 @@
]
},
{
"type": "string"
"$ref": "#/definitions/unifiedString"
}
]
},
Expand All @@ -110,7 +113,7 @@
"heightRank": {
"oneOf": [
{
"type": "string"
"$ref": "#/definitions/unifiedString"
},
{
"type": "number"
Expand Down
7 changes: 5 additions & 2 deletions outputs/json-draft-2019-09/joi-obj-16.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"$ref": "#/$defs/person",
"$defs": {
"unifiedString": {
"type": "string"
},
"unit": {
"type": "object",
"additionalProperties": false,
Expand Down Expand Up @@ -97,7 +100,7 @@
]
},
{
"type": "string"
"$ref": "#/$defs/unifiedString"
}
]
},
Expand All @@ -110,7 +113,7 @@
"heightRank": {
"oneOf": [
{
"type": "string"
"$ref": "#/$defs/unifiedString"
},
{
"type": "number"
Expand Down
7 changes: 5 additions & 2 deletions outputs/json-draft-2019-09/joi-obj-17.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"$ref": "#/$defs/person",
"$defs": {
"unifiedString": {
"type": "string"
},
"unit": {
"type": "object",
"additionalProperties": false,
Expand Down Expand Up @@ -97,7 +100,7 @@
]
},
{
"type": "string"
"$ref": "#/$defs/unifiedString"
}
]
},
Expand All @@ -110,7 +113,7 @@
"heightRank": {
"oneOf": [
{
"type": "string"
"$ref": "#/$defs/unifiedString"
},
{
"type": "number"
Expand Down
7 changes: 5 additions & 2 deletions outputs/json/joi-obj-16.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"$ref": "#/$defs/person",
"$defs": {
"unifiedString": {
"type": "string"
},
"unit": {
"type": "object",
"additionalProperties": false,
Expand Down Expand Up @@ -97,7 +100,7 @@
]
},
{
"type": "string"
"$ref": "#/$defs/unifiedString"
}
]
},
Expand All @@ -110,7 +113,7 @@
"heightRank": {
"oneOf": [
{
"type": "string"
"$ref": "#/$defs/unifiedString"
},
{
"type": "number"
Expand Down
7 changes: 5 additions & 2 deletions outputs/json/joi-obj-17.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"$ref": "#/$defs/person",
"$defs": {
"unifiedString": {
"type": "string"
},
"unit": {
"type": "object",
"additionalProperties": false,
Expand Down Expand Up @@ -95,7 +98,7 @@
]
},
{
"type": "string"
"$ref": "#/$defs/unifiedString"
}
]
},
Expand All @@ -108,7 +111,7 @@
"heightRank": {
"oneOf": [
{
"type": "string"
"$ref": "#/$defs/unifiedString"
},
{
"type": "number"
Expand Down
7 changes: 5 additions & 2 deletions outputs/open-api/joi-obj-16.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"$ref": "#/components/schemas/person",
"schemas": {
"unifiedString": {
"type": "string"
},
"unit": {
"type": "object",
"additionalProperties": false,
Expand Down Expand Up @@ -102,7 +105,7 @@
]
},
{
"type": "string"
"$ref": "#/components/schemas/unifiedString"
}
]
},
Expand All @@ -116,7 +119,7 @@
"heightRank": {
"oneOf": [
{
"type": "string"
"$ref": "#/components/schemas/unifiedString"
},
{
"type": "number"
Expand Down
7 changes: 5 additions & 2 deletions outputs/open-api/joi-obj-17.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"$ref": "#/components/schemas/person",
"schemas": {
"unifiedString": {
"type": "string"
},
"unit": {
"type": "object",
"additionalProperties": false,
Expand Down Expand Up @@ -102,7 +105,7 @@
]
},
{
"type": "string"
"$ref": "#/components/schemas/unifiedString"
}
]
},
Expand All @@ -116,7 +119,7 @@
"heightRank": {
"oneOf": [
{
"type": "string"
"$ref": "#/components/schemas/unifiedString"
},
{
"type": "number"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "joi-to-json",
"version": "2.3.2",
"version": "2.3.3",
"description": "joi to JSON / OpenAPI Schema Converter",
"main": "index.js",
"repository": {
Expand Down

0 comments on commit d7ae706

Please sign in to comment.