Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@readme/openapi-parser
Advanced tools
Swagger 2.0 and OpenAPI 3.x parser and validator for Node and browsers
@readme/openapi-parser is an npm package designed to parse, validate, and dereference OpenAPI (formerly known as Swagger) definitions. It helps developers work with OpenAPI specifications by providing tools to ensure the correctness and completeness of API definitions.
Parsing OpenAPI Definitions
This feature allows you to parse an OpenAPI definition from a file. The parsed object can then be used for further processing or validation.
const OpenAPIParser = require('@readme/openapi-parser');
async function parseOpenAPIDefinition(filePath) {
const api = await OpenAPIParser.parse(filePath);
console.log(api);
}
parseOpenAPIDefinition('path/to/openapi.yaml');
Validating OpenAPI Definitions
This feature allows you to validate an OpenAPI definition. It checks for errors and inconsistencies in the API definition and throws an error if any issues are found.
const OpenAPIParser = require('@readme/openapi-parser');
async function validateOpenAPIDefinition(filePath) {
try {
const api = await OpenAPIParser.validate(filePath);
console.log('API definition is valid:', api);
} catch (err) {
console.error('API definition is invalid:', err);
}
}
validateOpenAPIDefinition('path/to/openapi.yaml');
Dereferencing OpenAPI Definitions
This feature allows you to dereference an OpenAPI definition. It resolves all $ref pointers in the API definition, replacing them with the actual referenced objects.
const OpenAPIParser = require('@readme/openapi-parser');
async function dereferenceOpenAPIDefinition(filePath) {
const api = await OpenAPIParser.dereference(filePath);
console.log(api);
}
dereferenceOpenAPIDefinition('path/to/openapi.yaml');
swagger-parser is a popular npm package for parsing, validating, and dereferencing Swagger and OpenAPI definitions. It offers similar functionalities to @readme/openapi-parser but is more widely used and has a larger community.
openapi-schema-validator is a package focused on validating OpenAPI 3.0 schemas. While it does not offer parsing or dereferencing capabilities, it provides robust validation features for OpenAPI schemas.
openapi-types is a TypeScript library that provides type definitions for OpenAPI specifications. It is useful for TypeScript developers who want to ensure type safety when working with OpenAPI definitions, but it does not offer parsing or validation functionalities.
$ref
pointers, including external files and URLs$ref
pointers$ref
pointers, giving you a normal JavaScript object that's easy to work with$ref
pointers to the same value always resolve to the same object instanceOpenAPIParser.validate(myAPI, (err, api) => {
if (err) {
console.error(err);
}
else {
console.log("API name: %s, Version: %s", api.info.title, api.info.version);
}
});
Or use async
/await
or Promise syntax instead. The following example is the same as above:
try {
let api = await OpenAPIParser.validate(myAPI);
console.log("API name: %s, Version: %s", api.info.title, api.info.version);
}
catch(err) {
console.error(err);
}
For more detailed examples, please see the API Documentation
Install using npm:
npm install @readme/openapi-parser
When using Swagger Parser in Node.js apps, you'll probably want to use CommonJS syntax:
const OpenAPIParser = require("@readme/openapi-parser");
When using a transpiler such as Babel or TypeScript, or a bundler such as Webpack or Rollup, you can use ECMAScript modules syntax instead:
import OpenAPIParser from "@readme/openapi-parser";
@apidevtools/swagger-parser
@apidevtools/swagger-parser
returns schema validation errors as the raw error stack from Ajv. For example:
To reduce the amount of potentially unnecessary noise that these JSON pointer errors provide, @readme/openapi-parser
utilizes better-ajv-errors, along with some intelligent reduction logic, to only surface the errors that actually matter.
Additionally with these error reporting differences, this library ships with a validation.colorizeErrors
option that will disable colorization within these prettified errors.
Swagger Parser supports recent versions of every major web browser. Older browsers may require Babel and/or polyfills.
To use Swagger Parser in a browser, you'll need to use a bundling tool such as Webpack, Rollup, Parcel, or Browserify. Some bundlers may require a bit of configuration, such as setting browser: true
in rollup-plugin-resolve.
Full API documentation is available right here
FAQs
Swagger 2.0 and OpenAPI 3.x parser and validator for Node and browsers
The npm package @readme/openapi-parser receives a total of 174,033 weekly downloads. As such, @readme/openapi-parser popularity was classified as popular.
We found that @readme/openapi-parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 11 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
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.