Skip to content

dbebawy-modus/swagger-json

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

swagger-json

Generate swagger json schema based on Joi schemas.

Installation

npm install swagger-json

Usage

const { swaggerDoc } = require("swagger-json");
const Joi = require("joi");

const host = "localhost:3000";
const basePath = "/";
const info = {
  version: "1.0.0",
  title: "Title Example",
  description: "Description API example",
  termsOfService: "http://swagger.io/terms/",
  contact: {
    name: "Example team",
  },
  license: {
    name: "MIT",
  },
};

// This will generate initial doc
swaggerDoc.createJsonDoc(info, host, basePath);

const joiSchema = {
  body: Joi.object({
    email: Joi.string()
      .email()
      .required()
      .description("Email address for new user")
      .example("vladimir@gmail.com"),
    password: Joi.string()
      .min(7)
      .max(24)
      .required()
      .description("Password for new user")
      .example("Somestrongpasswrod#"),
    salt: Joi.string()
      .required()
      .description("Salt for new user")
      .example("asdsdgdsafs324eqwedagsdfafsdf"),
  }).meta({ modelName: "Register" }),
  headers: Joi.object({
    authorization: Validation.tokenValidation
      .string()
      .token()
      .required()
      .description("Auth token")
      .example("asdasfadfasdsasdas"),
  }),
  group: "Register routes",
  description: "Route to register user to the system",
};

// create a new route
swaggerDoc.addNewRoute(joiSchema, "/v1/user/register", "post");

// your swagger json file will have been written to "swagger.json"

Usage without writing the swagger.json file to the file system

// use swaggerDocJson instead of swaggerDoc from the swagger-json module
const { swaggerDocJson } = require("swagger-json");

// using the same joiSchema, info, host, and basePath as the above example.
swaggerDocJson.addNewRoute(joiSchema, "/v1/user/register", "post");

const swaggerSpec = swaggerDocJson.createJsonDoc(info, host, basePath);

About

Create json doc for swagger from joi schema

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • JavaScript 100.0%