Skip to content

Commit

Permalink
Merge pull request #4 from jehy/master
Browse files Browse the repository at this point in the history
update readme
  • Loading branch information
bollwar404 committed Jan 16, 2019
2 parents f047658 + 3a74360 commit a3f3edb
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 56 deletions.
136 changes: 81 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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

Expand All @@ -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.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit a3f3edb

Please sign in to comment.