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

koa-joi-validator-middleware

v1.2.0

Published

Validate objects on Koa's ctx with Joi schemas

Downloads

76

Readme

koa-joi-validator-middleware

Koa 2 middleware for validating Joi schemas

Codeship Status for ersoma/koa-joi-validator-middleware

TLDR

Problem:

  • Have a Koa 2 web server
  • Want to validate with Joi

Solution:

const Koa = require('koa');
const Joi = require('joi');
class koaJoiValidator = require('koa-joi-validator-middleware');

const schema = Joi.object({...});
const config = {...};

const app = new Koa();
app.use(koaJoiValidator(schema, config));

app.use(async ctx => {
  // Here you can be sure that the specified validation passed
});

Examples

1. Basic usage

By default, only the schema parameter is needed. In this case the middleware will validate the ctx.request.body object and throws an Error with a default message if it fails.

...
const schema = Joi.object({...});

const app = new Koa();
app.use(koaJoiValidator(schema));
...

2. Custom onError string parameter

You can specify an onError key in the second, config parameter. If this is a string it overwrites the default error message thrown.

...
const schema = Joi.object({...});
const config = {
  onError: 'Hey You, send me a proper request!'
};

const app = new Koa();
app.use(koaJoiValidator(schema, config));
...

3. Custom onError function parameter

The onError parameter can be a function as well which is called when the validation fails with the following parameters: ctx, next, error. Here ctx and next are the standard Koa middleware parameters and error is the ValidationError returned by Joi.

...
const schema = Joi.object({...});
const config = {
  onError: ctx => ctx.render('errorPage')
};

const app = new Koa();
app.use(koaJoiValidator(schema, config));
...

4. Custom getSubject function parameter

By default, the middleware validates the ctx.request.body object but this can be overwritten with the getSubject function. It takes ctx as the input parameter and is expected to return the object that should be validated.

...
const schema = Joi.object({...});
const config = {
  getSubject: ctx => ctx.session
};

const app = new Koa();
app.use(koaJoiValidator(schema, config));
...

API

Parameter | Optional | Type --- | --- | --- schema | no | Joi schema config | yes | Object config.onError | yes | String or Function(ctx, next, error) config.getSubject | yes | Function(ctx)

Note: This middleware does not contain Joi as an inner dependency, only as peer dependency. This means it can be updated individually when a new version comes out but it also means you need to install it separately.