Open preetampt57 opened 3 years ago
Hello,
joi-to-swagger will only create schemas, the definition for example of parameters is not in the scope of that library.
I also want to point out, that joi-to-swagger will create OpenAPI v3 compliant schemas. In your example, you use a parameter with in: 'body'
, which is not OpenAPI v3 but Swagger (v2).
For your question, to create the components tree you described I have written the following code:
const j2s = require('joi-to-swagger');
const joi = require('joi');
const pagination = joi.object({
perPageRecords: joi.number().integer(),
pageNo: joi.number().integer(),
}).meta({ className: 'pagination' });
const sort = joi.object({
sorts: joi.array().items(joi.object({
fieldName: joi.string(),
direction: joi.string(),
}).unknown()),
}).unknown().label('sort').meta({ className: 'sort' });
const components = { schemas: {} };
Object.assign(components.schemas, j2s(pagination, components).components.schemas);
Object.assign(components.schemas, j2s(sort, components).components.schemas);
console.log(JSON.stringify(components, null, 2));
resulting in
{
"schemas": {
"pagination": {
"type": "object",
"properties": {
"perPageRecords": {
"type": "integer"
},
"pageNo": {
"type": "integer"
}
},
"additionalProperties": false
},
"sort": {
"type": "object",
"properties": {
"sorts": {
"type": "array",
"items": {
"type": "object",
"properties": {
"fieldName": {
"type": "string"
},
"direction": {
"type": "string"
}
}
}
}
},
"title": "sort"
}
}
}
How to create JOI schema of following swagger example Can anybody provide a proper example to solve the below example