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

middy-ajv

v3.0.0

Published

Slim ajv validator for Middy.js

Downloads

186

Readme

Middy AJV

This middleware automatically validates incoming events and outgoing responses against custom schemas defined with the JSON schema syntax.

If an incoming event fails validation a BadRequest error is raised. If an outgoing response fails validation a InternalServerError error is raised.

This middleware can be used in combination with httpErrorHandler to automatically return the right response to the user.

It can also be used in combination with httpcontentnegotiation to load localised translations for the error messages (based on the currently requested language). This feature uses internally ajv-i18n module, so reference to this module for options and more advanced use cases. By default the language used will be English (en), but you can redefine the default language by passing it in the ajvOptions options with the key defaultLanguage and specifying as value one of the supported locales.

Also, this middleware accepts an object with plugins to be applied to customize the internal ajv instance.

Install

To install this middleware you can use NPM:

npm install --save middy-ajv

Requires: @middy/core:>=2.0.0

Options

  • eventSchema (function) (default undefined): The JSON schema compiled ajv validator that will be used to validate the input (request.event) of the Lambda handler.
  • contextSchema (function) (default undefined): The JSON schema object or compiled ajv validator that will be used to validate the input (request.context) of the Lambda handler. Has additional support for typeof keyword to allow validation of "typeof":"function".
  • responseSchema (function) (default undefined): The JSON schema compiled ajv validator that will be used to validate the output (request.response) of the Lambda handler.
  • availableLanguages (object) (optional): Error messages can be returned in multiple languages using ajv-i18n. Language is selected based on event.preferredLanguage set by @middy/http-content-negotiation. Should be in the format: { 'en': require('ajv-i18n/localize/en') }.
  • defaultLanguage (string) (default: en): The default language to use when availableLanguages is provided and event.preferredLanguage is not supported.

NOTES:

  • At least one of inputSchema or outputSchema is required.

Sample usage

Example for validation using precompiled schema:

import middy from '@middy/core'
import validator from 'middy-ajv'

const handler = middy((event, context) => {
  return {}
})

const eventSchema = require('schema.js')

handler.use(validator({ eventSchema }))

// invokes the handler, note that property foo is missing
const event = {
  body: JSON.stringify({something: 'somethingelse'})
}
handler(event, {}, (err, res) => {
  t.is(err.message,'Event object failed validation')
})

Build step

Folder Structure

{project}
|-- handlers
| |-- {enpoint}
| | |-- index.js
| | |-- eventSchema.json

After the build script has been run on the endpoint folder, it will contain schema.js and index.js.

Install

$ npm install -D ajv-cli

Run

#!/usr/bin/env bash
# Compile JSON Schemas
function ajv {
	node ./node_modules/ajv-cli/dist/index.js compile -c ajv-formats -c ajv-formats-draft2019 --strict=true --coerce-types=array --all-errors=true --use-defaults=empty --messages=false -s $1'eventSchema.json' -o $1'eventSchema.js'
}
for dir in handlers/*/; do
  if [ ! -n "$(ajv $dir | grep ' is valid')" ]; then
	  exit 1
  fi
done

Middy documentation and examples

For more documentation and examples, refers to the main Middy monorepo on GitHub or Middy official website.

Contributing

Everyone is very welcome to contribute to this repository. Feel free to raise issues or to submit Pull Requests.

License

Licensed under MIT License. Copyright (c) 2017-2021 will Farrell and the Middy team.