Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
swagger-parser
Advanced tools
Parses a Swagger spec (in JSON or YAML format), validates it against the Swagger schema, and dereferences all $ref pointers
swagger-parser is a powerful npm package that allows you to parse, validate, and dereference Swagger (OpenAPI) definitions. It helps in ensuring that your API definitions are correct and can be used to resolve $ref pointers to simplify the API documentation.
Parse
This feature allows you to parse a Swagger (OpenAPI) definition file. The code sample demonstrates how to parse a Swagger file and log the API name and version.
const SwaggerParser = require('swagger-parser');
SwaggerParser.parse('path/to/your/swagger.yaml')
.then(api => {
console.log('API name: %s, Version: %s', api.info.title, api.info.version);
})
.catch(err => {
console.error(err);
});
Validate
This feature allows you to validate a Swagger (OpenAPI) definition file. The code sample demonstrates how to validate a Swagger file and log whether the API is valid or not.
const SwaggerParser = require('swagger-parser');
SwaggerParser.validate('path/to/your/swagger.yaml')
.then(api => {
console.log('API is valid:', api);
})
.catch(err => {
console.error('API is invalid:', err);
});
Dereference
This feature allows you to dereference $ref pointers in a Swagger (OpenAPI) definition file. The code sample demonstrates how to dereference a Swagger file and log the dereferenced API.
const SwaggerParser = require('swagger-parser');
SwaggerParser.dereference('path/to/your/swagger.yaml')
.then(api => {
console.log('Dereferenced API:', api);
})
.catch(err => {
console.error(err);
});
Bundle
This feature allows you to bundle all external $ref pointers into a single file. The code sample demonstrates how to bundle a Swagger file and log the bundled API.
const SwaggerParser = require('swagger-parser');
SwaggerParser.bundle('path/to/your/swagger.yaml')
.then(api => {
console.log('Bundled API:', api);
})
.catch(err => {
console.error(err);
});
openapi-schema-validator is a package that validates OpenAPI 3.0 schemas. It focuses on schema validation and does not provide parsing or dereferencing functionalities like swagger-parser.
swagger-jsdoc is a package that generates Swagger (OpenAPI) definitions from JSDoc comments in your code. It is more focused on generating documentation from code comments rather than parsing and validating existing Swagger files.
swagger-client is a package that provides a JavaScript client for interacting with Swagger (OpenAPI) APIs. It includes functionalities for making API requests and handling responses, but it does not focus on parsing or validating Swagger definitions.
var parser = require("swagger-parser");
// Parse a JSON or YAML Swagger spec
parser.parse("path/to/my/swagger.yaml", function(err, swaggerObject) {
// This callback will be invoked once the Swagger spec is
// parsed, validated, and dereferenced.
if (err) {
console.error("Swagger Spec could not be parsed because: " + err.message);
return;
}
// If there's no error, then `swaggerObject` is the parsed SwaggerObject
// (see https://github.com/wordnik/swagger-spec/blob/master/versions/2.0.md#swagger-object-)
console.log(
"Your API title is " + swaggerObject.info.title +
", version " + swaggerObject.info.version
);
});
npm install swagger-parser
And then use var parser = require("swagger-parser")
in your Node script.
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 to false
, then the resulting SwaggerObject will contain [ReferenceObjects](see https://github.com/wordnik/swagger-spec/blob/master/versions/2.0.md#reference-object-) 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.json"). If set to false
then the resulting SwaggerObject will contain [ReferenceObjects](see https://github.com/wordnik/swagger-spec/blob/master/versions/2.0.md#reference-object-) for external pointers instead of the objects they reference.
validateSpec (default: true) -
Determines whether the Swagger spec will be validated against the Swagger schema. If set to false
, then the resulting SwaggerObject may be missing properties, have properties of the wrong data type, etc.
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:
Unit tests - Especially surrounding $ref
dereferencing logic
Browser Support - Some slight refactoring is needed to make Swagger-Parser work in web browsers. All of the dependencies already work in the browser, and most of them are available as Bower packages, so it won't be difficult at all.
Recursive $ref pointers - Recursive $ref
pointers are not currently supported, but I plan to add support for them.
Swagger-Parser is 100% free and open-source, under the MIT license. Use it however you want.
FAQs
Swagger 2.0 and OpenAPI 3.0 parser and validator for Node and browsers
The npm package swagger-parser receives a total of 780,366 weekly downloads. As such, swagger-parser popularity was classified as popular.
We found that swagger-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.