diff --git a/README.md b/README.md index 95f397d..b366085 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ -# Convert form AJV to Swagger +# ajv-to-swagger +Convert schema from AJV to Swagger [![npm](https://img.shields.io/npm/v/ajv-to-swagger.svg)](https://npm.im/ajv-to-swagger) [![license](https://img.shields.io/npm/l/ajv-to-swagger.svg)](https://npm.im/ajv-to-swagger) @@ -10,7 +11,7 @@ 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. +All you need to do after conversion is to insert converted schema into corresponding place in swagger schema. ## Install @@ -23,64 +24,89 @@ npm i ajv-to-swagger ```js const convertor = require('ajv-to-swagger'); -const ajvResponseSchema = {}; // imagine something here, check test/test.js for an example - +// 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 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 -swaggerDoc.paths['/get/some/'].get.responses['200'].content['application/json'].schema = convertedSchema.schema; +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. diff --git a/package.json b/package.json index b73d118..b826535 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ajv-to-swagger", - "version": "1.0.1", + "version": "1.0.2", "description": "convert from ajv to swagger", "main": "Swagger.js", "scripts": {