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

sd-defender

v1.0.1

Published

a validator for params

Downloads

3

Readme

sd-defender

sd-defender 是一个参数校验器

安装:

npm install sd-defender

它的输入是:

  • 一个参数对象(在 express 中通常是 req.queryreq.body
  • 一个校验规则对象,其中包含了参数名称和对应校验规则的描述关系

它的输出是:校验结果(一个数组,数组里存放校验不通过的参数信息;如所有参数都校验通过,则为空数组)

例如:

const V = require('sd-defender')

let params = {
  name: 'jarone',
  age: '18'
}

let schema = {
  mobile: 'required|numeric',
  name : 'max:10|min:1',
  age : 'numeric'
}

let invalidMsg = V(params, schema)

console.log(invalidMsg);

/*
[
  {
    paramName: 'mobile',
    value: undefined,
    invalid: '必须传递mobile'
  }
]
*/

sd-defender 输出的校验结果中默认展示中文,如需展示英文,则需要在调用时传递第 3 个参数,值为 en

const V = require('sd-defender')

let params = {
  name: 'jarone',
  age: '18'
}

let schema = {
  mobile: 'required|numeric',
  name : 'max:10|min:1',
  age : 'numeric'
}

let invalidMsg = V(params, schema, 'en')

console.log(invalidMsg);

/*
[
  {
    paramName: 'mobile',
    value: undefined,
    invalid: 'mobile is required.'
  }
]
*/

sd-defender 默认情况下只返回第一个不合格参数的校验结果

如需返回所有不合格参数的校验结果,需要在调用时传递第 4 个参数,值为 true

const V = require('sd-defender')

let params = {
  name: 'jarone',
  age: '18'
}

let schema = {
  mobile: 'required|numeric',
  name : 'max:5|min:1',
  age : 'numeric'
}

let invalidMsg = V(params, schema, 'en', true)

console.log(invalidMsg);

/*
[
  {
    paramName: 'mobile',
    value: undefined,
    invalid: 'mobile is required.'
  },
  {
    paramName: 'name',
    value: 'jarone',
    invalid: 'name can not gt 5. jarone given.'
  }
]
*/

sd-defender 支持扩展自定义校验规则

如需添加自定义校验规则,请在调用时传递第 5 和 第 6 个参数:

const V = require('sd-defender')

let params = {name: 'luy'}

let schema = {name: 'isJarone'}

let extRules = {isJarone: (val) => val === 'jarone'}

let extInvalidMsg = {
  isJarone: (paramName, val) => `${paramName} is not jarone, ${val} given.`
}

let invalidMsg = V(params, schema, 'en', true, extRules, extInvalidMsg)

console.log(invalidMsg);

/*
[
  {
    paramName: 'name',
    value: 'luy',
    invalid: 'name is not jarone, luy given.'
  }
]
*/

sd-defender 默认提供了如下校验规则:

  1. required 要求参数必传,如参数未传递,或值为: undefinednullNaN、空字符串、空数组、空对象,则判定为校验失败
  2. requiredWithOut 允许传入一组参数名单,如参数名单中的任何一项不存在时(是否存在的判定规则参考 required ),则当前参数必传
  3. bool 要求参数值为布尔类型
  4. numeric 要求参数值为数字类型,允许整型和浮点型
  5. array 要求参数值为数组类型
  6. max 要求参数值或参数值的长度的上限;如参数值是数字类型,则判定其值;如参数值是数组或字符串类型,则判定其长度(也就是 length);闭合区间(包含设置值)
  7. min 要求参数值或参数值的长度的下限;如参数值是数字类型,则判定其值;如参数值是数组或字符串类型,则判定其长度(也就是 length);闭合区间(包含设置值)
  8. dateGt 要求日期类型参数值的下限(不包含下限值)
  9. dateLt 要求日期类型参数值的上限(不包含上限值)
  10. dateGte 要求日期类型参数值的下限(包含下限值)
  11. dateLte 要求日期类型参数值的上限(包含上限值)

sd-defender 校验规则语法:

sd-defender 参考了 Laravel 的校验语法,使用 | 分割不同的校验规则,例如:

{
  name: 'required|min:5|max:10'
}