npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

restana-swagger-validator

v1.0.0

Published

High performance Swagger/OpenAPI validator middleware for Restana

Downloads

46

Readme

restana-swagger-validator

Validating REST APIs using OpenAPI (formerly known as Swagger) specifications is crucial to ensure that the API complies with the standard set by the specification. OpenAPI validation helps in maintaining a consistent design, improving API usability, and providing a way for developers to quickly and easily understand the functionality and expected inputs and outputs of an API.

However, API validators are required to be fast, particularly in the context of REST APIs, where response time is a critical performance factor. A validation process that adds significant overhead can cause the API to become slow and unresponsive, resulting in a poor user experience for clients.

The restana-swagger-validator module provides a validation layer that is specifically designed for use with the restana framework. This validation layer is optimized for high performance, ensuring that the validation process does not significantly impact the overall performance of the API:

Performance Benchmarks

Main features

  • OpenAPI/Swagger v2 and v3 spec supported through the api-schema-builder module
  • Fast validator middlewares based on restana app.events.BEFORE_ROUTE_REGISTER event hook
  • Low-level validator is based on https://www.npmjs.com/package/ajv

Module interface:

SwaggerValidator(app: restana, spec: string | Object, config: Object)

Please note that when using the SwaggerValidator function with the spec argument as a string, the function will use require(spec) to lookup the Swagger specification file.
It's important to note that only JSON format is currently supported for the Swagger specification file. If you have a YAML specification file, you will need to convert it to JSON before passing it to the SwaggerValidator function.

To convert a YAML file to JSON, you can use a YAML to JSON converter tool, such as the js-yaml and JSON.stringify() methods in JavaScript, or online YAML to JSON converter services. Once your YAML file is converted to JSON, you can pass it to the SwaggerValidator function.

Configuration options

  • buildResponses: If TRUE, response validation schemas will be parsed and cached for use. Default value: TRUE
  • requireSchemaSpec: If TRUE, an existing schema specification will be required when registering route endpoints. Default value: TRUE
  • apiSpecEndpoint: Server endpoint to expose the Swagger specification. Default value: /swagger.json
  • uiEndpoint: Server endpoint to expose the Swagger documentation UI. Default value: /docs
  • publicApiEndpoint: Public HTTP server endpoint. Default value: http://localhost:3000

Usage

const restana = require('restana')
const bodyParser = require('body-parser')
const path = require('path')

const {
  SwaggerValidationError,
  SwaggerValidator
} = require('restana-swagger-validator')

const app = restana({
  errorHandler: (err, req, res) => {
    if (err instanceof SwaggerValidationError) {
      res.statusCode = err.statusCode
      res.send({
        message: err.message,
        errors: err.errors
      })
    } else {
      res.send(err)
    }
  }
})

SwaggerValidator(app, path.join(__dirname, '/spec.json'), {
  buildResponses: false
})

app.use(bodyParser.json())

// register application endpoints

app.start()

Exposed endpoints

Swagger spec

curl -L http://localhost:3000/swagger.json

Swagger UI

curl -L http://localhost:3000/docs

Privacy notice

Please be aware that when using the built-in Swagger UI to visualize and interact with the OpenAPI specification, static resources such as font, CSS stylesheets and JavaScript files are fetched from third-party services hosted by the following providers:

  • cdnjs.cloudflare.com
  • fonts.googleapis.com

These third-party services may have their own privacy policies and terms of service, which you should review and understand. Additionally, if you have concerns about the privacy or security implications of fetching resources from these services, you may want to consider self-hosting the Swagger UI resources instead.

Please note that this information is provided for your awareness only, and we do not endorse or control the content of third-party services. We encourage you to exercise caution and use your own discretion when accessing external resources.

Limitations

  • OpenAPI 3 known issues: https://www.npmjs.com/package/api-schema-builder#open-api-3---known-issues