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
Features
- Supports 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
- Tested in Node.js and all major web browsers on Windows, Mac, and Linux
- Asynchronously downloads and caches external files and URLs
- Nested $ref pointers are supported, 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()
This method can be called with two parameters (as shown above), or with three parameters, like this:
var options = {
dereferencePointers: false,
validateSpec: false
};
parser.parse("swagger.yaml", options, function(err, api, metadata) {
...
});
The three parameters are as follows:
swaggerFile
- string (required)
The file path or URL of the 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
- object (optional)
An object containing one or more of the following properties:
parseYaml (default: true) - Determines whether the parser will allow Swagger specs in YAML format. If set to
false
, then only JSON will be allowed.dereferencePointers (default: true) - Determines whether
$ref
pointers will be dereferenced. If set tofalse
, then the resulting SwaggerObject will contain Reference Objects instead of the objects they reference.dereferenceExternalPointers (default: true) - Determines whether
$ref
pointers will be dereferenced if they point to external files (e.g. "http://company.com/my/schema.yaml"). If set tofalse
then the resulting SwaggerObject will contain Reference Objects for external pointers instead of the objects they reference.validateSpec (default: true) - Determines whether your Swagger spec 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
- function (required)
The callback function is called when the Swagger file and all referenced files have been downloaded, parsed, validated, and dereferenced.
err (Error object) - If an error occurred during parsing, then this will be the
Error
object; otherwise, this parameter will benull
. You should always check theerr
parameter first, because the other parameters may beundefined
if an error occurred.api (Swagger object) - If the file(s) were parsed successfully, then this object is the complete Swagger API object. See the official Swagger 2.0 specification for details.
metadata (object) - This object provides useful information about the parsing operation itself, namely:
baseDir (string) - The directory of the main Swagger file, which is the base directory used to resolve any relative $ref pointers.
files (array of strings) - The full paths of all files that were parsed. This only includes local files, not URLs. If The main Swagger file was local, then it will be the first item in this array.
urls (array of objects) - The URLs that were parsed. Each item in the array is a URL object, which lets you easily access the full URL, or specific parts of it.
resolvedPointers (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:
- Recursive (circular) $ref pointers - Recursive (circular)
$ref
pointers are not currently supported. So something like this won't work:
person:
properties:
name:
type: string
spouse:
type:
$ref: person
License
Swagger-Parser is 100% free and open-source, under the MIT license. Use it however you want.