https://github.com/armenhajian/koa2-validate
Request params validator for koa.js
https://github.com/armenhajian/koa2-validate
koa2 koa2-validate validatorjs
Last synced: 17 days ago
JSON representation
Request params validator for koa.js
- Host: GitHub
- URL: https://github.com/armenhajian/koa2-validate
- Owner: armenhajian
- Created: 2019-01-18T14:05:40.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-01-21T09:15:20.000Z (over 7 years ago)
- Last Synced: 2026-05-17T09:49:01.935Z (17 days ago)
- Topics: koa2, koa2-validate, validatorjs
- Language: JavaScript
- Homepage:
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# koa2-validate
[](https://www.npmjs.com/package/koa2-validate)
A library that wrap [validator.js][vldtr.git] package for koa.js to validate request parameters.
### Installation and usage
The lib requires [Koa.js](https://koajs.com/) to run.
```sh
$ npm install koa2-validate
```
#### No ES6
```javascript
var validate = require('koa2-validate');
```
#### ES6
```javascript
import validate from 'koa2-validate';
```
##### Usage example
#
```javascript
const app = new Koa()
.use(cors())
.use(bodyParser())
.use(validate()) //use before api routes!!
.use(api.routes())
/**
Use as {param: 'required|methodNameWithoutPrefix'}
*/
api.get('/simple-list',
async (ctx, next) => {
ctx.validate({
limit: 'required|numeric^{no_symbols: true}',
page: 'required|numeric^{no_symbols: true}'
})
})
```
The `|` symbol is a separator for rules.\
Only `required` is build-in rule.\
For other rules use method name from the Validate.js [validators][vldrs-link] which STARTS with `is` prefix.\
For example if you write `'..|numeric'` it will call `isNumeric` method from `Validate.js` lib.
For options you can use `^{}`.\
e.g. if you want to call `isNumeric(myVar, {no_symbols: true})` then use `..|numeric^{no_symbols: true}`
If the params not pass the rules it will respond with `422` status and error text `${key} should be ${rule}`
#### ToDo
- Make error texts more dynamic (maybe with option to pass from code)
- Add support of methods without `is` prefix
- Write tests
License
----
MIT
[//]: # (These are reference links used in the body of this note and get stripped out when the markdown processor does its job. There is no need to format nicely because it shouldn't be seen. Thanks to amazing https://dillinger.io builder)
[vldtr.npm]:
[vldtr.git]:
[vldrs-link]: