当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

koa-context-validator

授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 叶恩
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

koa-context-validator

Build Status

A robust context validator for koajs. Use Joi behind the scenes.

Installation

$ npm install koa-context-validator joi

Usage

query Validation

import 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 Validation

import 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 Validation

import 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(),
  }),
);

With 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;
    },
  ]),
);

With 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(),
      }),
    }),
  ),
);

With @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());

With 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,
    },
  ),
);

Asynchronous Operations

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),
    }),
  }),
);

With 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')),
    }),
  }),
);

Koa 1.x

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(),
      }),
    }),
  ),
);

API

validator(schema, options?)

schema

RequiredType: object

A object which has optional query, body, headers and params schema to validate.

options

Just be passed to Joi's validate function as options:

https://joi.dev/api/?v=17.4.2#anyvalidatevalue-options

Joi

The Joi object from the joi package.

TypeScript

As of version 1.0.0 we've added initial support for TypeScript.

License

MIT © C.T. Lin

 相关资料
  • 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

    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

    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

    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], [