Code Monkey home page Code Monkey logo

koa-router-checker's Introduction

koa-router-checker

Validator middleware for koa-router.

Install

npm i koa-router-checker --save

Example

'use strict';

const Koa = require('koa');
const Router = require('koa-router');
const koaRouterChecker = require('koa-router-checker');
const bodyparser = require('koa-bodyparser');
const app = new Koa()
const router = new Router()

app.use(bodyparser());


router.get('/', koaRouterChecker({
  // message [String, Function(key: string)] low priority
  message: function(key: string) {
    ...
  },
  // status [Number] low priority
  status: 400,

 /**
  * ctx.body
  * ctx.query
  * ctx.headers
  * ....
 */
  rule: {
    // body: ['name','password'] surrport Array
    body: {
      name: {
        status: 400,
        //[String, Function(key: id)] // high priority
        message: 'name is required'
      }
    },
    query: {
      id: {
        status: 400,
        //[String, Function(key: id)] // high priority
        message: 'id is required'
      }
    },
    headers: {
      token: {
        status: 401,
        // message [String, Function(key: token)] // high priority
        message: 'no auth'
      }
    }

    ...


  }
}), async(ctx,next) => {
  ...

  await next()

  ....
});

app.use(router.routes());

app.listen(5000);
// ctx.body
ctx.status = 400  //  default 400, the status of define
ctx.body = {
  message: 'xx is required' // default key is required, the message of define
}

Test

see test.

npm run test

koa-router-checker's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.