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-input

v1.0.5

Published

a middleware for koa to validate input (query, param and body)

Downloads

63

Readme

koa-input

NPM version build status David deps node version Gittip

A middleware for koa to validate the input (query, params, body and headers etc.)

Stop to write CIERR(Check-If-Error-Return-Repeatedly) style code, it can be done automatically!

Install

NPM

Usage

  • Quick start
var app = require('koa')();
var input = require('koa-input');
app.use(input('query', 'name', /^[a-zA-Z]+$/, 'default value', 'Your name is invalid'));
//same as:
//app.use(input.query('name', /^[a-zA-Z]+$/, 'default value', 'Your name is invalid'));
app.use(function *() {
    this.body = this.request.query.name
});
var app = require('koa')();
var input = require('koa-input');
app.use(input('query', 'name', /^[a-zA-Z]+$/, 'default value', 'Your name is invalid'));
//or
app.use(input('params', 'name', /^[a-zA-Z]+$/, 'default value', 'Your name is invalid'));
//or
app.use(input('body', 'name', /^[a-zA-Z]+$/, 'default value', 'Your name is invalid'));
//or
app.use(input('headers', 'name', /^[a-zA-Z]+$/, 'default value', 'Your name is invalid'));
app.use(function *() {
    this.body = this.request.query.name;
    //or
    this.body = this.request.params.name;
    //or 
    this.body = this.params.name;//You don't need to change existed koa-router code
    //or
    this.body = this.request.body.name;
    //or
    this.body = this.request.headers.name;
});
  • Support custom error handler
input.onError(function (name) {
    var error = new Error(util.format('Invalid get %s', name));
    error.status = 200;
    error.code = 77;
    return error;
});
  • Support special error
//String as error.message
app.use(input('query', 'name', /^[a-zA-Z]+$/, undefined, 'invalid query name'));
//Number as error.status
app.use(input('query', 'name', /^[a-zA-Z]+$/, undefined, 400));
//Object will be extended by input.error()
app.use(input('query', 'name', /^[a-zA-Z]+$/, undefined, {status: 400, message: 'invalid query name'}));
//The same as Object
app.use(input('query', 'name', /^[a-zA-Z]+$/, undefined, new Error('invalid query name')));
  • Support default value as an optional input
//set a not-`undefined` default value (function will be call and using it's return value)
app.use(input('query', 'type', /^(cat|dog)$/, 'dog'));
app.use(input('query', 'time', /^[0-9]{13}$/, Date.now));

app.use(input('query', 'type', /^(cat|dog)$/, null));//you can pass `null` if you want a `nil` default value
//or
app.use(input('query', 'type', /^(cat|dog)$/, function() {}));//just pass an empty function to get a `undefined` as default value
  • Support Regex, Function, Object, Array, Basic-Type pattern to validate the input
//Function(you can use any other module like validator)
app.use(input('query', 'email', validator.isEmail));
//Object(it will get the value if match the key)
app.use(input('query', 'status', {'normal': 0, 'invalid': 1}));
//Array(it must be element of the array)
app.use(input('query', 'type', ['cat', 'dog']));
//String(it must be equal to)
app.use(input('query', 'type', 'cat'));
  • Support multiple patterns
app.use(input('query', 'type', ['cat', 'dog']));
app.use(input('query', 'type', {cat: 1, dog: 2}));
app.use(input('query', 'type', function (value) {
    return value === 'cat';
}));
  • Support builder or object options
//if you want to ignore some middle-arguments:
app.use(input('query', 'name', undefined, undefined, 'invalid input'));
//OR
app.use(input.source('query').name('name').error('invalid input').build());
//OR
app.use(input({source: 'query', name: 'name', error: 'invalid input'}));
//OR
app.use(input('query', {name: 'name', error: 'invalid input'}));

Licences

MIT