Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/songshuangfei/koa-paramcheck
Koa middlewares, for parsing and checking query string or JSON body.
https://github.com/songshuangfei/koa-paramcheck
json-body koa koa-middleware
Last synced: about 1 month ago
JSON representation
Koa middlewares, for parsing and checking query string or JSON body.
- Host: GitHub
- URL: https://github.com/songshuangfei/koa-paramcheck
- Owner: songshuangfei
- Created: 2020-05-07T14:36:38.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T14:50:42.000Z (almost 2 years ago)
- Last Synced: 2024-10-01T02:21:19.268Z (about 1 month ago)
- Topics: json-body, koa, koa-middleware
- Language: TypeScript
- Homepage:
- Size: 194 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
koa-paramcheck
Koa middlewares, for parsing and checking query string or JSON body.
## Description
Koa middlewares for parsing and checking query and JSON body. Define a rule for this middleware as the first parameter. If the parameters of the http request do not match this rule, http will response with a 400 status code and a detailed error message.## Install
```bash
npm install koa-paramcheck
```## Example
```js
const Koa = require('koa');
const { queryCheck } = require('koa-paramcheck');
const app = new Koa();app.use(queryCheck({
properties: {
keyword: {
type: 'string',
allowEmpty: false
},
page: {
type: 'number',
min: 1
},
pageSize: {
type: 'number',
min: 1,
max: 20
}
},
requiredKeys: ['page', 'pageSize']
})).use(async (ctx) => {
console.log(ctx.request.passedParams.query);
ctx.body = '';
});app.listen(3000);
```
Test.
```bash
curl "localhost:3000?page=1&pageSize=30"
```
Response.
```
status: 400 Bad Request
body: {"queryErrors":"pageSize does not in range [1, 20]"}
```## Documentation
* [Guide](doc/guide.md)
* [API](doc/API.md)## License
MIT