
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
Stop to write CIERR(Check-If-Error-Return-Repeatedly) style code, it can be done automatically!
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;
});
input.onError(function (name) {
var error = new Error(util.format('Invalid get %s', name));
error.status = 200;
error.code = 77;
return 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')));
//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
//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'));
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';
}));
//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'}));
FAQs
a middleware for koa to validate input (query, param and body)
We found that koa-input demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.