koa-param-validate

1.0.1 • Public • Published

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"
  }
})

Package Sidebar

Install

npm i koa-param-validate

Weekly Downloads

1

Version

1.0.1

License

ISC

Unpacked Size

6.54 kB

Total Files

4

Last publish

Collaborators

  • ytianm