JSPM

  • Created
  • Published
  • Downloads 52119
  • Score
    100M100P100Q141821F

Various tools for using and integrating with Swagger.

Package Exports

  • swagger-tools
  • swagger-tools/middleware/swagger-metadata
  • swagger-tools/middleware/swagger-validator

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (swagger-tools) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

The project provides various tools for integrating and interacting with Swagger. This project is in its infancy but what is within the repository should be fully tested and reusable. Please visit the issue tracker to see what issues we are aware of and what features/enhancements we are working on.

Supported Swagger Versions

Features

  • Schema validation: For the file(s) supported by the Swagger specification, ensure they pass structural validation based on the JSON Schema associated with that version of the specification
  • Semantic validation: Validates Swagger files individually and as a whole (resource listing combined with API declarations) (See Issue #1 for more details)
  • Connect middleware for using Swagger resource documents for pre-route validation
    • Validate the request Content-Type based on the operation's consumes value(s)
    • Validate the request parameter types
    • Validate the request parameter values

Installation

swagger-tools is distributed via NPM so installation is the usual: npm install swagger-tools --save

Usage

The swagger-tools module currently exposes one property: v1_2. This is a reference to an object that has the following structure:

  • docsUrl: This is a link to the Swagger documentation for the corresponding specification version
  • schemasUrl: This is a link to the Swagger JSON Schema files for the corresponding specification version
  • version: This is the Swagger specification version
  • schemas: This is an object where the keys are the Swagger JSON Schema file names and the object is the loaded schema contents
  • validate: This is a function used to validate a Swagger document, as a JavaScript object, against a Swagger schema file
  • validateApi: This is a function used to validate a "full API" of Swagger documents including a resource listing and an array of API declarations

Here is an example showing how to use both versions of the validate function (For more details, the sources are documented):

var spec = require('swagger-tools').v1_2;
var petJson = require('./samples/1.2/pet.json');
var rlJson = require('./samples/1.2/resource-listing.json');
var petResults = spec.validate(petJson); // The default schema used is 'apiDeclaration.json'
var rlResults = spec.validate(rlJson, 'resourceListing.json');
var apiResults = spec.validateApi(rlJson, [petJson]);

Here is an example of using the Swagger middleware for validating requests based on your Swagger resource documents:

var connect = require('connect');
var petJson = require('./samples/1.2/pet.json');
var storeJson = require('./samples/1.2/user.json');
var userJson = require('./samples/1.2/store.json');
var swaggerValidator = require('swagger-tools/middleware/swagger-validator');
var app = connect();

app.use(swaggerValidator([petJson, storeJson, userJson]));

// ...

Contributing

This project uses Gulp for building so npm install -g gulp once you clone this project. Running gulp in the project root will lint check the source code and run the unit tests.