• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

nswbmw/koa-scheme: koa-scheme is a parameter validation middleware for koa.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

nswbmw/koa-scheme

开源软件地址:

https://github.com/nswbmw/koa-scheme

开源编程语言:

JavaScript 100.0%

开源软件介绍:

koa-scheme

koa-scheme is a parameter validation middleware for koa.

version v3.0.0 support for koa2

Install

npm i koa-scheme --save

Usage

scheme(config, options)
  • config: {Object|String} scheme object or path.
  • options: {Object}
    • debug: {Boolean} If true, print compiled config and throw error, Default false.

app.js

var koa = require('koa');
//var bodyParser = require('koa-bodyparser');
var scheme = require('koa-scheme');
var conf = require('./scheme');
var route = require('./route');

var app = koa();
//app.use(bodyParser());
app.use(scheme(conf));

route(app);

app.listen(3000, function() {
  console.log("listening on 3000")
});

scheme.json

{
  "/(.*)": {
    "request": {
      "header": {
        "version": "[1-9]+"        
      }
    }
  },
  "/": {
    "response": {
      "status": 200
    }
  },
  "GET /user/:username": {
    "response": {
      "body": {
        "name": /[a-z]+/i,
        "age": "[0-9]{1,3}"
      }
    }
  },
  "/user/:username": {
    "request": {
      "method": "(POST|patch)",
      "body": {
        "name": "[a-zA-Z]+",
        "age": /[0-9]{1,3}/
      }
    },
    "response": {
      "status": 200
    }
  },
  "(delete|OPTIONS) /user/:username": {
    ...
  }
}

see path-to-regexp.

NB: when use body-parser middleware (like: koa-bodyparser) before koa-scheme, you could configure 'body' field in 'request'.

use function:

scheme.js

module.exports = {
  "/users": {
    "request": {
      "method": "POST",
      "body": {
        "nameArr": testRequestNameArr
      }
    }
  }
}

function testRequestNameArr(arr) {
  if (arr.length === 3 && arr[1] === 'nswbmw') {
    return true;
  } else {
    return false;
  }
}

with validator:

scheme.js

var validator = require('validator');

module.exports = {
  "GET /user/:username": {
    "response": {
      "body": {
        "age": validator.isNumeric,
        "email": validator.isEmail,
        "webset": validator.isURL,
        ...
      }
    }
  }
}

Even you could write flat object like this:

{
  "GET /user/:username": {
    "response": {
      "body.age": validator.isNumeric,
      "body.email": validator.isEmail,
      "body.family.mother.age": validator.isNumeric
    }
  }
}

Example

scheme.js

var validator = require('validator');

module.exports = {
  "/(.*)": {
    "request": {
      "header.version": "[1-9]+"
    }
  },
  "/users": {
    "request": {
      "method": "GET"
    },
    "response": {
      "body": testRes
    }
  },
  "GET /user/:username": {
    "response": {
      "body": {
        "name": "[a-zA-Z]+",
        "age": validator.isNumeric,
        "email": validator.isEmail
      }
    }
  },
  "/user/:username": {
    "request": {
      "method": "(POST|PATCH)",
      "body.name": /[a-zA-Z]+/,
      "body.age": "[0-9]{1,3}",
      "body.email": validator.isEmail
    }
  },
  "(delete|OPTIONS) /user/:username": {
    "response": {
      "status": 200
    }
  }
}

// throw a error is ok
function testRes(arr) {
  throw new Error('badRequest');
}

Test

npm test

License

MIT




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
aria42/koala: Clojure dataframe library发布时间:2022-07-09
下一篇:
bananaappletw/koa2-examples: Koa2 examples发布时间:2022-07-09
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap