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 🙏

© 2025 – Pkg Stats / Ryan Hefner

egg-filter

v1.0.5

Published

filter params

Readme

egg-filter

参数过滤器。

参数符合预期

我们希望传入的参数是可预测的。

举个例子,修改用户资料时,只限定修改 nicknamegender,但请求方可能把用户所有字段都加上了,比如 password,这样是不是会产生一些潜在的漏洞,导致不可改的数据也被修改了。

egg-filter 通过一个过滤规则对象,把传入的参数筛选出来,确保只有你想要的参数。

参数类型符合预期

我们希望传入的参数是符合预期的。

举个例子,gender 参数通常会用数字类型表示,如 1 表示男,0 表示女。

但是 GET 请求的参数全是 string 类型,而 POST 请求则可以传递 JSON 数据,保证类型符合要求。

在服务器端,我们需要保证所有类型的请求都能被正确的处理,因此,参数过滤是非常必要的。

内置过滤器

egg-filter 内置了 8 个过滤器:

  1. string:确保参数是字符串类型
  2. number: 确保参数是数字类型
  3. boolean: 确保参数是布尔类型
  4. trim:确保参数是字符串类型,并且经过 trim 处理
  5. upper: 确保参数是字符串类型,并且全部大写
  6. lower: 确保参数是字符串类型,并且全部小写
  7. array: 确保参数是数组类型
  8. object: 确保参数是对象类型

自定义过滤器

如果内置过滤器不满足要求,可按如下方式添加:

app.filter.add('json', function (value) {
  if (typeof value === 'string') {
    try {
      return JSON.parse(value)
    }
    catch (err) {

    }
  }
  // 失败返回空字符串
  return ''
})

调用方式

let data = ctx.filter(
  {
    username: 'hahaha',
    password: 'hahaha',
    custom: 'haha'
  },
  {
    username: 'trim',
    password: ['trim', 'lower'],
    custom: function (value) {
      return value
    }
  }
)