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());
options
import Koa from 'koa';
import validator, { Joi } from 'koa-context-validator';
app.use(
validator(
{
query: Joi.object().keys({
username: Joi.string().required(),
}),
},
{
abortEarly: true,
allowUnknown: false,
cache: true,
stripUnknown: true,
debug: false,
},
),
);
import Koa from 'koa';
import validator, { Joi } from 'koa-context-validator';
const lookup = async (username) => {
const user = await db.get('user', username);
if (!user) {
throw new Error('Invalid username');
}
};
app.use(
validator({
body: Joi.object().keys({
username: Joi.string().external(lookup),
}),
}),
);
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(),
}),
}),
),
);
RequiredType: 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://joi.dev/api/?v=17.4.2#anyvalidatevalue-options
The Joi
object from the joi package.
As of version 1.0.0 we've added initial support for TypeScript.
MIT © C.T. Lin
问题来源 为了简化 ctx.body 赋值操作,想要在 ctx 扩展两个自定义方法, success 及 error 使用起来如下 // 响应成功状态请求 ctx.success({ username: 'test' }); // 等价于 ctx.body = { code: 1, data: { username: 'test' } }; // 响应失败状态请求 ct
前言 1、官方文档:https://www.sequelize.com.cn/ 2、sequelize-typescript文档:https://npmmirror.com/package/sequelize-typescript 一、引包 这里用到的是 mysql 如果要用其它 sql ,请查看官方文档 npm i sequelize -S npm i sequelize-typescript
import "context" Package context defines the Context type, which carries deadlines,cancelation signals, and other request-scoped values across API boundaries and between processes. Incoming requests t
ConTEXT 是一个小型、快速而且功能强大的文本编辑器,可作为程序员的附件工具,支持多种语言的语法着色。
Koa art-template view render middleware. support all feature of art-template. Install npm install --save art-template npm install --save koa-art-template Example const Koa = require('koa'); const ren
koa是Express的下一代基于Node.js的web框架,目前有1.x和2.0两个版本。 历史 1. Express Express是第一代最流行的web框架,它对Node.js的http进行了封装,用起来如下: var express = require('express'); var app = express(); app.get('/', function (req, res) {
Koa 是下一代的 Node.js 的 Web 框架。由 Express 团队设计。旨在提供一个更小型、更富有表现力、更可靠的 Web 应用和 API 的开发基础。 Koa可以通过生成器摆脱回调,极大地改进错误处理。Koa核心不绑定任何中间件,但提供了优雅的一组可以快速和愉悦地编写服务器应用的方法。 示例代码: var koa = require('koa');var app = koa();//
function getData() { return [ ['', 'Tesla', 'Nissan', 'Toyota', 'Honda', 'Mazda', 'Ford'], ['2017', 10, 11, 12, 13, 15, 16], ['2018', 10, 11, 12, 13, 15, 16], ['2019', 10, 11, 12, 13, 15, 16], [