Security News
NVD Backlog Tops 20,000 CVEs Awaiting Analysis as NIST Prepares System Updates
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
koa-context-validator
Advanced tools
A robust context validator for koajs. Use
Joi
behind the scenes.
$ npm install koa-context-validator joi
query
Validationimport Koa from 'koa';
import validator, { Joi } from 'koa-context-validator';
const app = new Koa();
app.use(
validator({
query: Joi.object().keys({
username: Joi.string().required(),
}),
}),
);
body
Validationimport Koa from 'koa';
import bodyParser from 'koa-bodyparser';
import validator, { Joi } from 'koa-context-validator';
const app = new Koa();
app.use(bodyParser());
app.use(
validator({
body: Joi.object().keys({
username: Joi.string().required(),
age: Joi.number().required(),
}),
}),
);
headers
Validationimport Koa from 'koa';
import validator, { Joi } from 'koa-context-validator';
const app = new Koa();
app.use(
validator({
headers: Joi.object()
.keys({
username: Joi.string().required(),
})
.unknown(),
}),
);
koa-compose
import Koa from 'koa';
import compose from 'koa-compose';
import validator, { Joi } from 'koa-context-validator';
const app = new Koa();
app.use(
compose([
validator({
query: Joi.object().keys({
username: Joi.string().required(),
}),
}),
async (ctx) => {
ctx.body = ctx.request.query;
},
]),
);
koa-mount
import Koa from 'koa';
import mount from 'koa-mount';
import validator, { Joi } from 'koa-context-validator';
const app = new Koa();
app.use(
mount(
'/api',
validator({
query: Joi.object().keys({
username: Joi.string().required(),
}),
}),
),
);
@koa/router
import Koa from 'koa';
import Router from '@koa/router';
import validator, { Joi } from 'koa-context-validator';
const router = new Router();
router.get(
'/api/:username',
validator({
params: Joi.object().keys({
username: Joi.string().required(),
}),
}),
async (ctx) => {
ctx.body = ctx.params;
},
);
const app = new Koa();
app.use(router.middleware());
stripUnknown
import Koa from 'koa';
import validator, { Joi } from 'koa-context-validator';
app.use(
validator(
{
query: Joi.object().keys({
username: Joi.string().required(),
}),
},
{ stripUnknown: true },
),
);
Joi.ref
import Koa from 'koa';
import validator, { Joi } from 'koa-context-validator';
app.use(
validator({
body: Joi.object().keys({
username: Joi.string().default(Joi.ref('$defaultUsername')),
age: Joi.number().default(Joi.ref('$defaultAge')),
}),
}),
);
Use convert.back
from koa-convert
.
import koa from 'koa'; // koa 1.x
import convert from 'koa-convert';
import validator, { Joi } from 'koa-context-validator';
const app = koa();
app.use(
convert.back(
validator({
query: Joi.object().keys({
username: Joi.string().required(),
}),
}),
),
);
Required
Type: object
A object which has optional query
, body
, headers
and params
schema to validate.
Just be passed to Joi's validate function as options:
https://github.com/hapijs/joi/blob/master/API.md#validatevalue-schema-options-callback.
The Joi
object from the joi package.
As of version 1.0.0 we've added initial support for TypeScript.
MIT © C.T. Lin
1.0.0 / 2021-08-24
ref
export. Use Joi.ref
instead.FAQs
A robust context validator for koajs
We found that koa-context-validator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers 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
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.