JSPM

  • Created
  • Published
  • Downloads 1141048
  • Score
    100M100P100Q188185F
  • License MIT

Swagger JSON/YAML parser and validator for Node and browsers

Package Exports

  • swagger-parser
  • swagger-parser/lib/util

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-parser) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Swagger-Parser

Parses, validates, and dereferences JSON/YAML Swagger specs in Node and browsers

Build Status Dependencies Code Climate Score Codacy Score Coverage Status

npm Bower License

Features

  • Parses Swagger specs in JSON or YAML format
  • Validates against the official Swagger 2.0 schema
  • Dereferences all $ref pointers, including pointers to external files and URLs
  • Asynchronously downloads and caches external files and URLs
  • Tested in Node.js and all major web browsers on Windows, Mac, and Linux
  • Supports nested $ref pointers, even in external files and URLs
  • Multiple $ref pointers to the same definition are resolved to the same object instance, thus maintaining strict reference equality

Basic Example

swagger.parser.parse("swagger.yaml", function(err, api, metadata) {
  if (!err) {
    console.log("API name: %s, Version: %s", api.info.title, api.info.version);
  }
});

The api parameter that's passed to the callback function is the parsed, validated, and dereferenced Swagger object.

Installation and Use

Node

npm install swagger-parser

Then add this to your Node script:

var parser = require("swagger-parser");
parser.parse('swagger.yaml', function(err, api, metadata) { ... });

Bower

bower install swagger-parser

Then add this to your HTML page:

<script src="bower_components/swagger-parser/dist/swagger-parser.js"></script>
<script>
    swagger.parser.parse('http://mysite.com/swagger.yaml', function(err, api, metadata) { ... });
</script>

AMD (Require.js)

Just add swagger-parser to your AMD module's dependencies, or require("swagger-parser") explicitly.

define("myModule", ["swagger-parser"], function(parser) {
    parser.parse('http://mysite.com/swagger.yaml', function(err, api, metadata) { ... });
});

The API

Parser.parse(swaggerPath, [options], callback)

  • swaggerPath (required) - string
    The file path or URL of your Swagger file. Relative paths are allowed. In Node, the path is relative to process.cwd(). In the browser, it's relative to the URL of the page.

  • options (optional) - object
    An object containing one or more parsing options. See options below.

  • callback (required) - function(err, api, metadata)
    Called after the parsing is finished, or when an error occurs. See callback below for details.

Options

Property Type Default Description
parseYaml bool true Determines whether the parser will allow Swagger specs in YAML format. If set to false, then only JSON will be allowed.
resolve$Refs bool true Determines whether $ref pointers will be resolved. If set to false, then the resulting Swagger object will contain Reference objects instead of the objects they reference.
resolveExternal$Refs bool true Determines whether $ref pointers will be resolved if they point to external files or URLs. If set to false then the resulting Swagger object will contain Reference objects for external pointers instead of the objects they reference.
validateSchema bool true Determines whether your API will be validated against the official Swagger schema. If set to false, then the resulting Swagger object may be missing properties, have properties of the wrong data type, etc.

Callback

Parameter Type Description
err Error null unless an error occurred.
api Swagger object The complete Swagger API object. Or undefined if an error occurred
metadata object This parameter can usually be ignored. It provides useful information about the parsing operation itself.

The metadata parameter is an object with the following properties:

Property Type Description
baseDir string The directory of the main Swagger file, which is the base directory used to resolve any external $ref pointers.
files array of strings The full paths of all files that were parsed. This only includes local files, not URLs. If Parser.parse() was called with a local file path, then it will be the first item in this array.
urls array of URL objects The URLs that were parsed. If Parser.parse() was called with a URL, then it will be the first item in this array.
$refs object A map of all the $ref pointers that were resolved, and the objects they resolved to.

Contributing

I welcome any contributions, enhancements, and bug-fixes. File an issue on GitHub and submit a pull request. Use JSHint to make sure your code passes muster. (see .jshintrc).

Here are some things currently on the to-do list:

  • Circular $ref pointers - Circular $ref pointers are a bit of an edge case, but it would be nice to support them anyway. Currently, something like this won't work:
person:
  properties:
    name:
      type: string
    spouse:
      type:
        $ref: person   # circular reference

License

Swagger-Parser is 100% free and open-source, under the MIT license. Use it however you want.