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 your Swagger specs
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 URLs
- Nested $ref pointers are supported, even in external 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("path/to/my/swagger.yaml", function(err, swagger) {
if (err) {
console.error("There's an error in the Swagger file: " + err.message);
return;
}
console.log("Your API is " + swagger.info.title + ", version " + swagger.info.version);
});
The swagger
parameter that is passed to the callback function is a fully-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('path/to/my/swagger.yaml', function(err, swagger) { ... });
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/path/to/my/swagger.yaml', function(err, swagger) { ... });
</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/path/to/my/swagger.yaml', function(err, swagger) { ... });
});
Options
The parse
function accepts an optional options
parameter, like this:
var options = {
dereferencePointers: false,
validateSpec: false
};
parser.parse("path/to/my/swagger.yaml", options, function(err, swaggerObject) {
...
});
Available options are as follows:
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.
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.