npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-best-validator

v1.0.7

Published

koa-best-validator is request validator for node.js.

Downloads

13

Readme

koa-best-validator

对于参数的校验,koa-best-validator提供了类校验方式,对以下四纵参数进行统一校验

  • ctx.params(路由参数)
  • ctx.request.query(上下文请求query参数)
  • ctx.request.header(上下文请求头)
  • ctx.request.body(上下文请求体)

安装

yarn add koa-best-validator 
# 或
npm i koa-best-validator 

使用

  • 继承基类Validator
  • 参考aysnc-validator,在属性descriptor添加校验规则
  • 验证 await (new RegistryValidator()).validate(ctx)
//./test/validator/person-validator.js

const { Validator } = require('koa-best-validator')

// 第一步:继承基类Validator
// 第二部:参考aysnc-validator,在属性descriptor添加校验规则
class PersonValidator extends Validator {
  constructor() {
    super()
    //添加校验规则,参考async-validator
    this.descriptor = {
      name: [
        { type: "string", required: true },
        { min: 3, max: 5, message: '长度在 3 到 5 个字符' }
      ],
      age: [
        {
          required: true, type: 'number', message: 'is error', validator: (rule, value) => {
            return value > 10
          }
        }
      ],
      grade: [
        {
          validator: (rule, value, callback) => {
            //自定义校验规则,参考async-validator
            if (value) {
              callback()
            } else {
              callback(new Error('必填啊'))
            }
          }
        }
      ]
    };
  }
}

module.exports = PersonValidator

我们以PersonValidatorr为例来详细的分析类校验器的使用

  • koa-best-validator会返回 Validator,RuleResult 两个类。
  • Validator 校验器的基类。
  • RuleResult 校验结果的基类。
  • 必须继承自Validator这个基类,且需要在构造函数中初始化校验规则。如我们在 PersonValidator 的构造函数中定义了name,age,grade 等校验规则,必须是数组。参考async-validator

路由中使用编写的Validaotr类,传入ctx

//./test/index.js

const Koa = require('koa')
const bodyParser = require('koa-bodyparser')
const Router = require('koa-router')
//异常错误
const PersonValidator = require('./validator/person-validator')
const RegistryValidator = require('./validator/registry-validator')

const {RuleResult}=require('koa-best-validator')


const app = new Koa()
const router = new Router()

app.use(bodyParser())

/**
 * 捕获全局异常
 */
app.use(async (ctx, next) => {
  try {
    await next()
  } catch (error) {
    if(error instanceof RuleResult){
    //Koa-best-validator抛出的错误实例
    console.log(error)
    }
    ctx.body = {
      message: error
    }
  }
})

router.get('/api/getuserinfo', async (ctx, next) => {
  //验证
  const { valid, validator } = await (new PersonValidator()).validate(ctx)
  //通过getValue方法可以拿到值
  const name = validator.getValue('name')
  const age = validator.getValue('age')
  const grade = validator.getValue('grade')
  ctx.body = {
    code: 'success',
    name,
    age,
    grade
  }
})


router.post('/api/registry', async (ctx, next) => {
  //验证
  const v = await (new RegistryValidator()).validate(ctx)
  ctx.body = {
    code: 'success'
  }
})
app.use(router.routes())
app.use(router.allowedMethods())

app.listen(3001)

Validator 属性

descriptor:Object。校验规则,具体可以参考async-validator

Validator API

validate

验证

function(ctx:any,throwError:boolean): Promise
  • ctx:Koa的请求上下文(必须)
  • throwError:验证失败的时候,是否通过throw new Error(new RuleResult())方式抛出错误,默认:false,非必填。如果为true,可以通过给Koa注册中间件,捕获异常。例如
const { RuleResult } = require('koa-best-validator')
app.use(async (ctx, next) => {
  try {
    await next()
  } catch (error) {
    if (error instanceof RuleResult) {
      //Koa-best-validator抛出的错误实例
      console.log(error)
    }
    ctx.body = {
      message: error
    }
  }
})

findParam

自定义校验规则时,通过key获取value

function(key:string):any

getValue

获取参数值,key可以是path路径,比如params.name ,也可以是参数名name。如果校验规则中定义了type类型,取值会自动转换。例如age的规则中,type:number。会自动转成 int类型。如果对应过个type类型,以第一个为准,目前支持的类型换换有 string,number,float,boolean

function(key:string):any

例子:

 const { valid, validator } = await (new PersonValidator()).validate(ctx)
  //通过getValue方法可以拿到值
  const name = validator.getValue('name')
  const age = validator.getValue('age')
  const grade = validator.getValue('grade')
  ctx.body = {
    code: 'success',
    name,
    age,
    grade
  }

演示

具体的demo地址

nodemon test/index.js

自定义规则函数

// ./test/validator/registry-validator.js
const { Validator } = require('koa-best-validator')

// 第一步:继承基类Validator
// 第二部:参考aysnc-validator,在属性descriptor添加校验规则
class RegistryValidator extends Validator {
  constructor() {
    super()
    this.descriptor = {
      username: [
        {
          type: "string",
          required: true
        }
      ],
      passwd1: [
        {
          //自定义验证规则
          validator: (rule, value, cb) => {
            if (!value) {
              cb(new Error('请输入密码'))
            } else {
              cb()
            }
          }
        }
      ],
      passwd2: [
        {
          validator: (rule, value, cb) => {
            if (!value) {
              cb(new Error('请再次输入密码'))
            } else {
              //获取某一个字段
              const passwd1 = this.findParam('passwd1').value
              if (value !== passwd1) {
                cb(new Error('两次输入密码不一致!'));
              } else {
                cb();
              }
            }
          }
        }
      ]
    }
  }
}


module.exports = RegistryValidator