koa-param-validate
v1.0.1
Published
a validator for koa(koa请求参数校验)
Downloads
4
Maintainers
Readme
koa-param-validate
a validator for koa
Install
$ npm install koa-param-validate --save
Usage
Example
// app.js
const Koa = require("koa")
const koaParamValidate = require("koa-param-validate")
const app = new Koa()
koaParamValidate(app)
app.listen(3000, () => {
console.log("server is running...")
})
// controller/auth.js
module.exports = (app) => ({
async login() {
const { ctx } = app
try {
ctx.validate({
username: "string",
password: "string",
});
} catch (error) {
console.log(error);
}
ctx.body = {
code: 200,
msg: "success",
}
}
})
Api
validate
ctx.validate({
username: "string",
age: "number",
})
addRule
// add password rule
app.validator.addRule("password", (rule, value) => {
if (value.toString().length < 6) {
return "the length cannot be less than 6 characters"
}
})