All Projects → carlansley → Swagger2

carlansley / Swagger2

Licence: mit
Loading, parsing and validating requests to HTTP services based on Swagger v2.0 documents

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Swagger2

Openapi Spec Validator
OpenAPI Spec validator
Stars: ✭ 161 (+705%)
Mutual labels:  swagger, openapi, validation
Swagger Parser
Swagger 2.0 and OpenAPI 3.0 parser/validator
Stars: ✭ 710 (+3450%)
Mutual labels:  swagger, openapi, validation
Openapi Cop
A proxy that validates responses and requests against an OpenAPI document.
Stars: ✭ 338 (+1590%)
Mutual labels:  swagger, openapi, validation
Openapi Spring Webflux Validator
🌱 A friendly kotlin library to validate API endpoints using an OpenApi 3.0 and Swagger 2.0 specification
Stars: ✭ 67 (+235%)
Mutual labels:  swagger, openapi, validation
openapi-schema-validator
OpenAPI schema validator for Python
Stars: ✭ 35 (+75%)
Mutual labels:  validation, swagger, openapi
openapi-schemas
JSON Schemas for every version of the OpenAPI Specification
Stars: ✭ 22 (+10%)
Mutual labels:  validation, swagger, openapi
Dredd
Language-agnostic HTTP API Testing Tool
Stars: ✭ 3,770 (+18750%)
Mutual labels:  swagger, openapi, validation
Widdershins
OpenAPI / Swagger, AsyncAPI & Semoasa definitions to (re)Slate compatible markdown
Stars: ✭ 856 (+4180%)
Mutual labels:  swagger, openapi
Optic
Optic documents and tests your API as you build it
Stars: ✭ 760 (+3700%)
Mutual labels:  swagger, openapi
Kin Openapi
OpenAPI 3.0 implementation for Go (parsing, converting, validation, and more)
Stars: ✭ 776 (+3780%)
Mutual labels:  swagger, openapi
Apispec
A pluggable API specification generator. Currently supports the OpenAPI Specification (f.k.a. the Swagger specification)..
Stars: ✭ 831 (+4055%)
Mutual labels:  swagger, openapi
Full Stack Fastapi Postgresql
Full stack, modern web application generator. Using FastAPI, PostgreSQL as database, Docker, automatic HTTPS and more.
Stars: ✭ 7,635 (+38075%)
Mutual labels:  swagger, openapi
Schemathesis
A modern API testing tool for web applications built with Open API and GraphQL specifications.
Stars: ✭ 768 (+3740%)
Mutual labels:  swagger, openapi
Spectral
A flexible JSON/YAML linter for creating automated style guides, with baked in support for OpenAPI v2 & v3.
Stars: ✭ 876 (+4280%)
Mutual labels:  swagger, openapi
Apicurio Studio
Open Source API Design
Stars: ✭ 638 (+3090%)
Mutual labels:  swagger, openapi
Swagger Editor Electron
Run standalone Swagger Editor in Electron shell
Stars: ✭ 10 (-50%)
Mutual labels:  swagger, openapi
Angular Springboot Rest Jwt
Springboot, Angular and JWT security - Example Project based on Northwind Order Processing
Stars: ✭ 603 (+2915%)
Mutual labels:  swagger, openapi
Oapi Codegen
Generate Go client and server boilerplate from OpenAPI 3 specifications
Stars: ✭ 806 (+3930%)
Mutual labels:  swagger, openapi
Furion
Make .NET development easier, more versatile, and more popular.
Stars: ✭ 902 (+4410%)
Mutual labels:  swagger, validation
Django Ninja
💨 Fast, Async-ready, Openapi, type hints based framework for building APIs
Stars: ✭ 875 (+4275%)
Mutual labels:  swagger, openapi

Build Status Coverage Status Dependencies Known Vulnerabilities

swagger2

Loading, parsing and validating requests to HTTP services based on Swagger v2.0 documents.

Benefits

  • Fast. Pre-compiled regular expressions and code generation used to validate the inputs and outputs of Swagger 2.0 operations at run-time.
  • Typed. swagger2 is implemented in TypeScript 2, including a fully annotated TypeScript definition of the Swagger 2.0 document object. Makes working with Swagger objects more pleasant in the IDE of your choosing (WebStorm, Atom, etc).

Installation

$ npm install swagger2 --save

Usage

Basic loading and validation of swagger 2.0 document:

import * as swagger from 'swagger2';

// load YAML swagger file
const document = swagger.loadDocumentSync('./swagger.yml');

// validate document
if (!swagger.validateDocument(document)) {
  throw Error(`./swagger.yml does not conform to the Swagger 2.0 schema`);
}

You can compile the document for fast validation of operation requests and responses within the framework of your choosing. Koa 2 example:

let app = new Koa();

...
app.use(body());
app.use(createKoaMiddleware(document));
...


function createKoaMiddleware(document: swagger.Document) {

  // construct a validation object, pre-compiling all schema and regex required
  let compiled = swagger.compileDocument(document);

  return async(context, next) => {

    if (!context.path.startsWith(document.basePath)) {
      // not a path that we care about
      await next();
      return;
    }

    let compiledPath = compiled(context.path);
    if (compiledPath === undefined) {
      // if there is no single matching path, return 404 (not found)
      context.status = 404;
      return;
    }

    // check the request matches the swagger schema
    let validationErrors = swagger.validateRequest(compiledPath,
      context.method, context.request.query, context.request.body);
    if (validationErrors === undefined) {
      // operation not defined, return 405 (method not allowed)
      context.status = 405;
      return;
    }

    if (validationErrors.length > 0) {
      context.status = 400;
      context.body = {
        code: 'SWAGGER_REQUEST_VALIDATION_FAILED',
        errors: validationErrors
      };
      return;
    }

    // wait for the operation to execute
    await next();

    // check the response matches the swagger schema
    let error = swagger.validateResponse(compiledPath, context.method, context.status, context.body);
    if (error) {
      error.where = 'response';
      context.status = 500;
      context.body = {
        code: 'SWAGGER_RESPONSE_VALIDATION_FAILED',
        errors: [error]
      };
    }
  };
}


There is a complete implementation of this example/use-case in the swagger2-koa module, so if you're using Koa 2 it may make sense to use that instead of swagger2 directly.

Limitations

  • currently only supports synchronous loading of full documents (via swagger.loadDocumentSync)
  • does not support validation of file attachments
  • does not support validation of mime-types
  • requires node v6.0 or above

Development

First, grab the source from GitHub.

From within the swagger2 directory, to run tests:

$ npm install
$ npm test

To see code coverage in a web-browser:

$ npm run cover:browser

To clean up:

$ npm run clean

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].