What is oas-validator?
The oas-validator npm package is used to validate OpenAPI Specification (OAS) documents. It ensures that the OAS documents adhere to the OpenAPI standards and helps in identifying any discrepancies or errors in the API definitions.
What are oas-validator's main functionalities?
Validate OpenAPI 3.0 documents
This feature allows you to validate OpenAPI 3.0 documents to ensure they conform to the OpenAPI Specification. The code sample reads an OpenAPI document from a file and validates it using the oas-validator package.
const validator = require('oas-validator');
const fs = require('fs');
const apiSpec = JSON.parse(fs.readFileSync('path/to/openapi.json', 'utf8'));
validator.validate(apiSpec, {}, (err, options) => {
if (err) {
console.error('Validation failed:', err);
} else {
console.log('Validation succeeded');
}
});
Validate OpenAPI 2.0 (Swagger) documents
This feature allows you to validate OpenAPI 2.0 (Swagger) documents. The code sample reads a Swagger document from a file and validates it using the oas-validator package, specifying the version as '2.0'.
const validator = require('oas-validator');
const fs = require('fs');
const apiSpec = JSON.parse(fs.readFileSync('path/to/swagger.json', 'utf8'));
validator.validate(apiSpec, { version: '2.0' }, (err, options) => {
if (err) {
console.error('Validation failed:', err);
} else {
console.log('Validation succeeded');
}
});
Custom validation options
This feature allows you to use custom validation options such as linting and schema validation. The code sample demonstrates how to pass custom options to the validator.
const validator = require('oas-validator');
const fs = require('fs');
const apiSpec = JSON.parse(fs.readFileSync('path/to/openapi.json', 'utf8'));
const options = {
lint: true,
validateSchema: true
};
validator.validate(apiSpec, options, (err, options) => {
if (err) {
console.error('Validation failed:', err);
} else {
console.log('Validation succeeded');
}
});
Other packages similar to oas-validator
swagger-parser
The swagger-parser package is used to validate, parse, and dereference Swagger and OpenAPI documents. It provides similar functionalities to oas-validator but also includes features for parsing and dereferencing API definitions.
openapi-schema-validator
The openapi-schema-validator package is a lightweight validator for OpenAPI 3.0 schemas. It focuses on schema validation and is simpler compared to oas-validator, which offers more comprehensive validation options.
swagger-tools
The swagger-tools package provides a suite of tools for working with Swagger documents, including validation, routing, and middleware. It offers broader functionality compared to oas-validator, which is focused solely on validation.
oas-validator
Usage:
const validator = require('oas-validator');
const options = {};
validator.validate(openapi, options, function(err, options){
});
If the callback argument of validate
is omitted, a Promise is returned instead.
See here for complete documentation of the options
object.