Git Product home page Git Product logo

swagger-ajv-converter's Introduction

ajv-to-swagger

Convert schema from AJV to Swagger

npm license Build Status dependencies Status devDependencies Status Coverage Status Known Vulnerabilities

That package converts ajv schemas (JSON schema draft 07) to Swagger schema (OpenApi 3).

All you need to do after conversion is to insert converted schema into corresponding place in swagger schema.

Install

npm i ajv-to-swagger

Usage

const convertor = require('ajv-to-swagger');

// some kind of AJV schema which we provide as a response
const ajvResponseSchema = {
  type: 'object',
  properties: {
    currency: { type: 'string', minLength: 3, maxLength: 3 },
    id: { type: 'string', minLength: 36, maxLength: 36 },
    version: { enum: [2] },
    rates: {
      type: 'object',
      example: {
        USDEUR: '0.881367883',
        USDRUB: '67.905200000'
        },
      patternProperties: {
        '^[A-Z]{6}$': { type: 'string' },
      },
      additionalProperties: false,
    },
  },
  additionalProperties: false,
};
  
// make a draft of your schema without a response schema:
const baseSchema = 
    {
      "openapi": "3.0.1",
      "info": {
        "title": "defaultTitle",
        "description": "defaultDescription",
        "version": "0.1"
      },
      "servers": [
        {
          "url": "https://google.com"
        }
      ],
      "paths": {
        "/get/some/": {
          "get": {
            "description": "Auto generated using Swagger Inspector",
            "parameters": [
              {
                "name": "cs",
                "in": "query",
                "schema": {
                  "type": "string"
                },
                "example": "E"
              }
            ],
            "responses": {
              "200": {
                "description": "Auto generated using Swagger Inspector",
                "content": {
                  "application/json": {
                    "schema": {}
                  }
                }
              }
            },
            "servers": [
              {
                "url": "https://google.com"
              }
            ]
          },
          "servers": [
            {
              "url": "https://google.com"
            }
          ]
        }
      }
    };

const responseSwagger = convertor.convertSchema(ajvResponseSchema);

// put your schema where it needs to be
baseSchema.paths['/get/some/'].get.responses['200'].content['application/json'].schema = responseSwagger.schema;

Now swaggerDoc has a valid Swagger schema. You can serialize it in JSON and ouput when needed.

Limitations

Currently ajv-to-swagger does not support many patternProperties, only one. But it is usually enough.

swagger-ajv-converter's People

Contributors

bollwar404 avatar jehy avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.