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-validators

v0.2.4

Published

Koa-validators Is an easy to use server-side parameter validator, which provides field validators and custom validators Field validation depends on "validator.js" So its validation rules are limited "validator.js" And koa-validators will validate: ctx.req

Downloads

9

Readme

Koa parameter validator

Koa-validators Is an easy to use server-side parameter validator, which provides field validators and custom validators Field validation depends on "validator.js" So its validation rules are limited "validator.js" And koa-validators will validate: ctx.request.bodyctx.request.queryctx.request.headerctx.param Perform uniform validation, so make sure your parameters are not repeated

中文文档

Installation

$ npm install koa-validators

Register global exception handling

Once you have registered the global exception-handling middleware, it will catch all your errors and either return them to the client or throw them directly, depending on the type of error.

const Koa = require('koa')
const { exception } = require('koa-validators')
const app = new Koa()

// Registered middleware
app.use(exception)

app.listen(3000)

Define validation rules

The fields you need to validate are organized in a class manner, and you need to inherit from the base class KoaValidator. Each validation Rule can be an array or a single Rule instance You can set the return status code and errorCode uniformly before defining the rule

const { Rule, KoaValidator } = require('koa-validators')

//The unified returned code and errorCode default to 400 1000

KoaValidator.defaults = {
  errorCode: 10000,
  code: 400
}
class CustomValidator extends KoaValidator {
  constructor() {
    super()
    //this.usename Is the field that you need to check
    //A Rule defined by a field that you want to check can be a single Rule instance or an array that contains multiple Rule rules
    this.username = [
      new Rule('isLength', 'Nicknames must be between 2 and 10 in length', 2, 10),
      //isOptional Indicates that this field is optional, and the third parameter can set the default value of this field
      new Rule('isOptional', '', 'Tom')
    ]
    this.group_id = new Rule('isInt', 'The grouping ID must be an integer and greater than 0', {
      min: 1
    })
  }
  //Custom validation rules
  // All custom validation functions must begin with validate
  // You can simply return true when the check passes
  // You can simply return false if validation fails but this will lose the error message
  // So you'd better use return [false, 'The two passwords do not match, please try again ']
  // You can also throw a "throw New Error" (" The password is different, please re-enter ")
  validateConfirmPassword(data) {
    if (!data.body.password || !data.body.confirm_password) {
      return [false, 'The passwords you entered do not match. Please try again']
    }
    let ok = data.body.password === data.body.confirm_password
    if (ok) {
      return true
    } else {
      return [false, 'The passwords you entered do not match. Please try again']
    }
  }
}

Field calibration

this.id = new Rule('isInt', 'The grouping ID must be an integer and greater than 0', {
  min: 1
})

When you define a validation Rule like the one above, the first argument to the Rule class is actually the method name of Validator.js, which is what validator.js looks like

isInt('X', { min: 1 })

isOptional

Setting isOptional makes an argument optional. If the client doesn't pass an argument to the server, it will use the default values you defined below. If the client does not pass a parameter, the isEmail validation rule will not take effect

new Rule('isOptional', '', '[email protected]'),
  new Rule(
    'isEmail',
    'Email address does not conform to the specification, please enter the correct email address'
  )

When a client passes a parameter to a server, the isEmail validation rule takes effect, which is exactly what isOptional means

Custom rule functions

Custom rule functions take an argument


validateConfirmPassword(data) {
  if (!data.body.password || !data.body.confirm_password) {
    return [false, 'The passwords you entered do not match. Please try again']
  }
  let ok = data.body.password === data.body.confirm_password
  if (ok) {
    return ok
  } else {
    return [false, 'The passwords you entered do not match. Please try again']
  }
}

The overall structure of this parameter is as follows:

this.data = {
  body: ctx.request.body, // body -> body
  query: ctx.request.query, // query -> query
  path: ctx.params, // params -> path
  header: ctx.request.header // header -> header
}

Use the defined validation rules

Call the validate function and pass in the CTX to trigger validation. Koa-validators will do the rest for you and return a promise Resolve by default in this promise and pass in instances of classes you define

const Router = require('koa-router')

const router = new Router({
  prefix: '/v1/test'
})

router.get('/search', async ctx => {
  const v = await new CustomValidator().validate(ctx)
  v.get('query.count')
  v.get('query.count', false)
  ctx.body = {
    code: 200
  }
})

// You can also control the error status code returned by setting code and errorCode
// It will not take effect when there is no error in the client's parameters and will override the default status code you set
const v = await new CustomValidator().validate(ctx, { errorCode: 10000, code: 400 })

Take out the parameter

When we get the returned instance, we can get the parameters through the GET method

const v = await new CustomValidator().validate(ctx)
v.get('query.count')
v.get('query.count', false)

If count is a string, then the parameter obtained by get has been converted to number for you. Of course, if you do not need this conversion, you can pass false on the third parameter

inherit

class RegisterValidator extends KoaValidator {
  username: Rule[]
  group_id: Rule
  constructor() {
    super()
    this.username = [
      new Rule('isOptional', '', 't'),
      new Rule('isLength', 'Nicknames must be between 2 and 10 in length', 2, 10)
    ]
    this.group_id = new Rule('isInt', 'The grouping ID must be an integer and greater than 0', {
      min: 1
    })
  }

  validateConfirmPassword(data: any) {
    return [false, 'The passwords you entered do not match. Please try again']
  }
}

class SubRegisterValidator extends RegisterValidator {
  email: Rule
  constructor() {
    super()
    this.email = new Rule(
      'isEmail',
      'Email address does not conform to the specification, please enter the correct email address'
    )
  }
  validateTest(data: any): boolean {
    return false
  }
}

Here koa-validators perform uniform validation on all fields and on the validate function on the prototype chain

alias

class RegisterValidator extends KoaValidator {
  constructor() {
    super()
    this.group_id = new Rule('isInt', 'The grouping ID must be an integer and greater than 0', {
      min: 1
    })
  }
}
const v = await new CustomValidator().validate(ctx, {
  alias: { group_id: id },
  errorCode: 10000,
  code: 400
})

When you call validate to validate, you can set the code returned by the second argument and you can also set the alias by using it For example: alias:{group_id:id} It USES the group_ID validation rule to validate the ID parameter passed in by the client