Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
preetampt57 opened this issue Jul 29, 2021 · 1 comment
Open

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

preetampt57 opened this issue Jul 29, 2021 · 1 comment
Labels

Comments

@preetampt57
Copy link

preetampt57 commented Jul 29, 2021

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
Copy link
Collaborator

Mairu commented Jul 31, 2021

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"
    }
  }
}

@Mairu Mairu added the question label Jul 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants