Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/koajs/parameter
parameter validate middleware for koa, powered by parameter
https://github.com/koajs/parameter
Last synced: 2 months ago
JSON representation
parameter validate middleware for koa, powered by parameter
- Host: GitHub
- URL: https://github.com/koajs/parameter
- Owner: koajs
- Created: 2014-10-15T14:25:00.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2022-02-12T04:35:11.000Z (almost 3 years ago)
- Last Synced: 2024-04-14T13:08:40.647Z (8 months ago)
- Language: JavaScript
- Size: 14.6 KB
- Stars: 65
- Watchers: 7
- Forks: 11
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: History.md
Awesome Lists containing this project
- awesome-koa - parameter - parameter validate middleware for koa, powered by parameter (Middleware)
README
koa-parameter
---------------[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![David deps][david-image]][david-url]
[![node version][node-image]][node-url]
[![Gittip][gittip-image]][gittip-url][npm-image]: https://img.shields.io/npm/v/koa-parameter.svg?style=flat-square
[npm-url]: https://npmjs.org/package/koa-parameter
[travis-image]: https://img.shields.io/travis/koajs/parameter.svg?style=flat-square
[travis-url]: https://travis-ci.org/koajs/parameter
[coveralls-image]: https://img.shields.io/coveralls/koajs/parameter.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/koajs/parameter?branch=master
[david-image]: https://img.shields.io/david/koajs/parameter.svg?style=flat-square
[david-url]: https://david-dm.org/koajs/parameter
[node-image]: https://img.shields.io/badge/node.js-%3E=_0.10-green.svg?style=flat-square
[node-url]: http://nodejs.org/download/
[gittip-image]: https://img.shields.io/gittip/dead-horse.svg?style=flat-square
[gittip-url]: https://www.gittip.com/dead-horse/parameter validate middleware for koa, powered by [parameter](https://github.com/node-modules/parameter).
## Installation
```bash
$ npm install koa-parameter --save
```## Usage
```js
const Koa = require('koa');
const parameter = require('koa-parameter');const app = new Koa();
parameter(app); // add verifyParams method, but don't add middleware to catch the error
// app.use(parameter(app)); // also add a middleware to catch the error.app.use(async function (ctx) {
ctx.verifyParams({
name: 'string'
});
});
```Checkout [parameter](https://github.com/node-modules/parameter) to get all the rules.
## Translate
You can override the translate method of parameter to implement I18n, by passing a function like this :
```js
const Koa = require('koa');
const parameter = require('koa-parameter');const app = new Koa();
parameter(app, function() {
// Same example with node-parameter
var args = Array.prototype.slice.call(arguments);
// Assume there have I18n.t method for convert language.
return I18n.t.apply(I18n, args);
});app.use(async function (ctx) {
ctx.verifyParams({
name: 'string'
});
});
```## [Example](example/index.js)
### License
MIT