Twipped / joi-to-swagger

A library to convert Joi schema objects into Swagger schema definitions
Other
164 stars 63 forks source link

How to create joi schema of allOf: swagger syntax #86

Open preetampt57 opened 3 years ago

preetampt57 commented 3 years ago

How to create JOI schema of following swagger example Can anybody provide a proper example to solve the below example

 "/a/demo/list": {
    "post": {
      "summary": "demo List",
      "operationId": "demo",
      "description": "demo List",
      "produces": [
        "application/json"
      ],
      "consumes": [
        "application/json"
      ],
      "parameters": [
        {
          "name": "body",
          "description": "Fetch demo list",
          "in": "body",
          "schema": {
            "allOf": [
              {
                "$ref": "#/components/schemas/pagination"
              },
              {
                "$ref": "#/components/schemas/sort"
              }
            ]
          }
        }
      ],
      "responses": {
        "200": {
          "description": "successful operation"
        }
      }
    }
  }
 "components": {
    "schemas": {
      "pagination": {
        "type": "object",
        "properties": {
          "perPageRecords": {
            "type": "integer"
          },
          "pageNo": {
            "type": "integer"
          }
        },
        "additionalProperties": false
      },
      "sort": {
        "title": "sort",
        "properties": {
          "sorts": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "fieldName": {
                  "type": "string"
                },
                "direction": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  }
Mairu commented 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"
    }
  }
}