npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@astro-my/joi-swagger-generator

v3.1.0

Published

generate swagger.json file from joi schema file

Downloads

170

Readme

A library to generate a swagger file from joi file. The output will be in json format.

Usage: Define your joi validator class

Define the header.json file that will be used as the header information in the swagger file The basePath will be join together with the path in validator class, in the example all url will be /users/*

{
    "swagger": "2.0",
    "info": {
        "description": "Identity account",
        "version": "1.0.0",
        "title": "Identity Service",
        "contact": {
            "email": "[email protected]"
        }
    },
    "basePath": "/users",
    "schemes": [
        "http",
        "https"
    ]
}

There are 2 ways to define the file First way is to define 1 specific file that will export all the apiList

You can install the package globally or just include them in the scripts in the package.json

    "swagger-generator": "joi-swagger-generator -v ./utils/validator.js -h ./swagger/header.json -o ./swagger/swagger.json"
-h is the path to the header file
-v is the path to your validator class
-o is the location of the swagger file will be generated
-g is to defined the api gateway url base path

Example for 1 validator file

const Joi = require('joi')
const JoiPhone = Joi.extend(require('joi-phone-number'))

module.exports =  {
    name: 'Account Services',
    apiList: {
        healthCheck: {
            name: 'health check',
            path: '/health',
            type: 'get'
        },
        login: {
            name: 'login',
            path: '/login',
            type: 'post',
            JoiSchema: {
                body: Joi.object().keys({
                    password: Joi.string().required(),
                    email: Joi.string().email(),
                    phoneNumber: JoiPhone.string().phoneNumber({ defaultCountry: 'MY' })
                }).xor('email', 'phoneNumber'),

                response: {
                    200: {
                        description: "successfully login",
                        header: Joi.object().keys({
                                    Authorization: Joi.string().required()
                                }),
                        body: Joi.object().keys({
                            resultMessage: Joi.string().required(),
                            resultDescription: Joi.string().required(),
                            body: Joi.object().keys({
                                accessToken: Joi.string().required(),
                                refreshToken: Joi.string().required()
                            })
                        })
                    },
                    400: {
                        description: "invalid request body",
                        body: Joi.object().keys({
                            resultMessage: Joi.string().required(),
                            resultDescription: Joi.string().required()
                        })
                    },
                    401: {
                        description: "invalid credential",
                        body: Joi.object().keys({
                            resultMessage: Joi.string().required(),
                            resultDescription: Joi.string().required()
                        })
                    }
                }
            }
        }
    }
}

Second way is to define the directory, and the library will recursively look for *.validator.js files. Just add -r in the command to enable this option

joi-swagger-generator -r -v ./utils/ -h ./swagger/header.json -o ./swagger/swagger.json

Example for multiple validator file

const Joi = require('joi')
const JoiPhone = Joi.extend(require('joi-phone-number'))

module.exports =  {
    name: 'login',
    path: '/login',
    type: 'post',
    JoiSchema: {
        body: Joi.object().keys({
            password: Joi.string().required(),
            email: Joi.string().email(),
            phoneNumber: JoiPhone.string().phoneNumber({ defaultCountry: 'MY' })
        }).xor('email', 'phoneNumber'),
        response: {
            200: {
                description: "successfully login",
                header: Joi.object().keys({
                            Authorization: Joi.string().required()
                        }),
                body: Joi.object().keys({
                    resultMessage: Joi.string().required(),
                    resultDescription: Joi.string().required(),
                    body: Joi.object().keys({
                        accessToken: Joi.string().required(),
                        refreshToken: Joi.string().required()
                    })
                })
            },
            400: {
                description: "invalid request body",
                body: Joi.object().keys({
                    resultMessage: Joi.string().required(),
                    resultDescription: Joi.string().required()
                })
            },
            401: {
                description: "invalid credential",
                body: Joi.object().keys({
                    resultMessage: Joi.string().required(),
                    resultDescription: Joi.string().required()
                })
            }
        }
    }
}

Result example

{
    "swagger": "2.0",
    "info": {
        "description": "Identity account",
        "version": "1.0.0",
        "title": "Identity Service",
        "contact": {
            "email": "[email protected]"
        }
    },
    "basePath": "/users",
    "schemes": [
        "http",
        "https"
    ],
    "paths": {
        "/users/health": {
            "get": {
                "summary": "health check",
                "consumes": [
                    "application/json"
                ],
                "produces": [
                    "application/json"
                ],
                "parameters": [],
                "responses": {
                    "200": {
                        "description": "success operation"
                    }
                },
                "x-amazon-apigateway-integration": {
                    "passthroughBehavior": "when_no_match",
                    "httpMethod": "get",
                    "type": "http_proxy",
                    "uri": "http://${stageVariables.url}/users/health",
                    "responses": {
                        "default": {
                            "statusCode": "200"
                        }
                    }
                }
            }
        },
        "/users/login": {
            "post": {
                "summary": "login",
                "consumes": [
                    "application/json"
                ],
                "produces": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "name": "body",
                        "in": "body",
                        "schema": {
                            "$ref": "#/definitions/loginPostBody"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "successfully login",
                        "schema": {
                            "$ref": "#/definitions/loginPost200Response"
                        },
                        "headers": {
                            "Authorization": {
                                "type": "string"
                            }
                        }
                    },
                    "400": {
                        "description": "invalid request body",
                        "schema": {
                            "$ref": "#/definitions/loginPost400Response"
                        }
                    },
                    "401": {
                        "description": "invalid credential",
                        "schema": {
                            "$ref": "#/definitions/loginPost401Response"
                        }
                    }
                },
                "x-amazon-apigateway-integration": {
                    "passthroughBehavior": "when_no_match",
                    "httpMethod": "post",
                    "type": "http_proxy",
                    "uri": "http://${stageVariables.url}/users/login",
                    "responses": {
                        "default": {
                            "statusCode": "200"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "loginPostBody": {
            "type": "object",
            "required": [
                "password"
            ],
            "properties": {
                "password": {
                    "type": "string"
                },
                "email": {
                    "type": "string",
                    "format": "email"
                },
                "phoneNumber": {
                    "type": "string"
                }
            }
        },
        "loginPost200Response": {
            "type": "object",
            "required": [
                "resultMessage",
                "resultDescription"
            ],
            "properties": {
                "resultMessage": {
                    "type": "string"
                },
                "resultDescription": {
                    "type": "string"
                },
                "body": {
                    "type": "object",
                    "required": [
                        "accessToken",
                        "refreshToken"
                    ],
                    "properties": {
                        "accessToken": {
                            "type": "string"
                        },
                        "refreshToken": {
                            "type": "string"
                        }
                    }
                }
            }
        },
        "loginPost400Response": {
            "type": "object",
            "required": [
                "resultMessage",
                "resultDescription"
            ],
            "properties": {
                "resultMessage": {
                    "type": "string"
                },
                "resultDescription": {
                    "type": "string"
                }
            }
        },
        "loginPost401Response": {
            "type": "object",
            "required": [
                "resultMessage",
                "resultDescription"
            ],
            "properties": {
                "resultMessage": {
                    "type": "string"
                },
                "resultDescription": {
                    "type": "string"
                }
            }
        }
    }
}