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 🙏

© 2026 – Pkg Stats / Ryan Hefner

express-validate-openapi

v1.2.0

Published

Express middleware to validate payloads against an OpenAPI spec

Downloads

18

Readme

Express Validate OpenAPI

Express middleware to validate payloads against an OpenAPI 3.0 spec

Uses Enjoi and Joi to validate models and generate error messaging.

Installation

npm i express-validate-openapi

Example

Middleware setup

import express from 'express'
import path from 'path'
import { readFileSync } from 'fs'
import bodyParser from 'body-parser'
import { OpenApiValidator } from 'express-validate-openapi'

// Get Parsed JSON doc
const specPath = path.join(__dirname, '../mocks/openapi.json')
const doc = JSON.parse(readFileSync(specPath, 'utf-8'))
// optional
const logger = (error, data) => console.log(error, data)
// create instance
const validator = new OpenApiValidator({ doc, logger })

const app = express()
app.use(bodyParser.json()) // body must be JSON

Single Schema

To validate a single schema in the request body:

app.post('/', validator.validate('schema_05'), function(req, res) {
  res.send()
})

// valid request body...
{
  "schema_05": {
    "unit": "pixel",
    "width": 640,
    "height": 480,
  }
}

The body of the post request must be valid JSON.

Multiple schemas

If you have multiple properties in your payload object, include those keys in an array in your keys

const keys = ['someSchemaKey', 'anotherKey']

app.post('/', validator.validate(keys), function(req, res) {
  res.send()
})

// valid request body...
{
  "someSchemaKey": {
    "prop1": "something",
    "prop2": "something else",
  },
  "anotherKey": "some string or data"
}

Options

doc - object

Parsed JSON object

// example using path and fs modules to retrieve doc
const specPath = path.join(__dirname, '../mocks/openapi.json')
const doc = JSON.parse(readFileSync(specPath, 'utf-8'))

logger - function(error,data)

Callback function that receives two arguments:

  1. Error message string (VALIDATION_ERROR)
  2. Object with {_Data: errorMessages} for logging purposes

Methods

validate(key,keyInBody)

Express middleware to validate

key - string or [string, string] Schema key(s) to use to validate the payload against. Payload must include these key:schema pairs

keyInBody (optional) - string Key to use in the request body payload. The payload will be transformed to use the key parameter's value instead. Note: only supported when key is a single string value

Known Issues

Self-referencing $refs

Schemas that include $refs that refer to themselves will throw errors.

// openapi.json > components > schemas
...
"schemaABC": {
  "type": "object",
  "properties": {
    "someProp": {
      "type": "string"
    },
    "anotherProp": {
      "type": "array",
      "items": {
        "$ref": "#/components/schemas/schemaABC"
      }
    }
  }
}
...

One way to fix this is to manually modify anotherProp's item, defining a standard Joi type instead, such as object:

"anotherProp": {
  "type": "array",
  "items": {
    "type": "object"
  }
}