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 🙏

© 2025 – Pkg Stats / Ryan Hefner

micro-ajv

v0.1.3

Published

An Ajv (Another JSON Schema Validator) middleware for Micro to validate request body, query parameters and etc.

Downloads

4

Readme

micro-ajv

An Ajv (Another JSON Schema Validator) middleware for Micro to validate request body, url, query parameters and etc.

CircleCI

Installation

npm install --save micro-ajv

Usage

By default, it validates request body and replies with 400 status code if validation fails.

const { send } = require('micro')
const microAjvValidation = require('micro-ajv')

const schema = { type: 'string', maxLength: 1024 }
const validate = microAjvValidation(schema)

const handler = (req, res) => send(res, 200, 'Ok')

module.exports = validate(handler)

Middleware Options

It's possible to configure the middleware behavior by passing options object

| Option | Description | Default | | ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------- | | ajv | Ajv options | {} | | target | Path to data for validations (e.g. query.accountId). It validates request body by default | "body" | | errorMode | Enables change middleware behavior strategy when request validation fails. There are three possible modes: reply — respond with an error; throw — throw an exception; inject – call handler with injected validation errors to the req object | "reply" | | injectKey | Enables pass validation errors to the req object when error mode is inject | "microAjvErrors" | | createError | Use this option if you need to change the default error object. As a first argument, it expects Ajv validation errors | errors => micro.createError(400, 'Bad request') |

Examples

Validate req.url and handle validation errors inside the handler:

const { send } = require('micro')
const microAjvValidation = require('micro-ajv')

const schema = { type: 'string', maxLength: 1024 }
const options = { target: 'url', errorMode: 'inject', injectKey: 'errors' }
const validate = microAjvValidation(schema, options)

module.exports = validate((req, res) => {
  console.error(req.errors)
  send(res, 414, 'Request url is too long')
})

Validate req.body and reply with a custom error if it fails

const { send } = require('micro')
const microAjvValidation = require('micro-ajv')

const schema = { type: 'string', maxLength: 1024 }
const options = {
  createError: errors =>
    Object.assign(Error(errors.map(error => error.message)), { statusCode: 400 }),
}

const validate = microAjvValidation(schema, options)

const handler = (req, res) => send(res, 200, 'Ok')

module.exports = validate(handler)

Sometime you may need to throw an exception and probably catch it somewhere else in the project instead of replying with an error immediately.

// handler.js
const { send } = require('micro')
const microAjvValidation = require('micro-ajv')

const schema = { type: 'string', maxLength: 1024 }
const options = {
  errorMode: 'throw',
  createError: errors =>
    Object.assign(Error('Payload validation failed'), { type: 'ApiError', statusCode: 400 }),
}
const validate = microAjvValidation(schema, options)

const handler = (req, res) => send(res, 200, 'Ok')

module.exports = validate(handler)
// middleware.js
module.exports.logApiErrors = handler => (req, res) =>
  handler(req, res).catch(err => {
    if (err.type === 'ApiError') {
      console.log(`ApiError: ${err.message}`)
    }
    throw err
  })
// index.js
const { router, post } = require('microrouter')
const { logApiErrors } = require('./middleware')
const handler = require('./handler')

module.exports = logApiErrors(router(post('/foo', handler)))