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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@wisefox/egg-validate-plus

v1.0.5

Published

egg validate plugin

Readme

egg-validate-plus

说明

本模块基于 egg-validate-plus 进行扩展

依赖的 egg 版本

egg-validate-plus 版本 | egg 1.x --- | --- 1.x | 😁 0.x | ❌

用法

开启插件

// config/plugin.{env}.js
exports.validatePlus = {
  enable: true,
  package: 'egg-validate-plus',
};

配置插件

suppressWarning 、first、firstFields 具体参数含义请参考 async-validator

// config/config.{env}.js
config.validatePlus = {
  suppressWarning: true,
  first: false,
  firstFields: true,
  resolveError(ctx, errors) {
    if (errors.length) {
      ctx.type = 'json';
      ctx.status = 400;
      ctx.body = {
        code: 400,
        error: errors,
        message: '参数错误',
      };
    }
  }
};

使用插件

目录结构

|- MY-PROJECT
    |- app
        |- controller
            |- user.js
            |- post.js
        |- rules
            |- user
                |- login.js [用户登录参数校验规则]
            |- post
                |- add.js [创建 post 参数校验规则]
    |- config
        |- config.default.js
        |- plugin.js
    |- package.json
    |- README.md

规则的传入方式

1.传入字符串
// app/middleware/validate.js
const { ValidationException } = require("../exception");
// rule: 'user.login'
module.exports = rule => {
    return async function validateHandler(ctx, next) {
        // 判断ctx.request.body,ctx.request.query,ctx.params是否为空对象,不为空对象的拼接成一个新对象
        const params = Object.assign({}, ctx.request.body, ctx.request.query, ctx.params);
        // 验证参数
        let result = await ctx.validate(rule, params);
        if (!result.validate) {
            let errors = result.errors;
            // 遍历错误数组,将每个错误对象转换为对象
            errors = errors.map(e => { return { [e.field]: e.message } });
            // 将错误数组转换为对象
            errors = Object.assign({}, ...errors);
            // 抛出异常
            throw new ValidationException(errors);
        }
        // 验证通过,继续执行
        await next();
    }
};

注意:不要带上 rules

2.直接传入验证规则对象
// app/controller/xx.js
// 直接引入 rules 文件下的验证规则,也可以是自己写的验证规则对象
const rule = this.app.rules.user.login
// 数据格式
// const rule = {
//   id: [
//     { required: true },
//     { type: 'number', message: 'id 必须为数字 }
//   ],
//   password: [
//     { required: true },
//     { type: 'string', message: 'password 必须为字符串 }
//   ]
// }

// 从客户端传入的参数 
const { query } = this.ctx.request;
// 数据格式: 
// query = {
//   username: 123456,
//   password: 'abcdefg'
// }

// 拿到验证结果
const validateResult = await this.ctx.validate(rule, query)
// 验证不通过时,阻止后面的代码执行
if (!validateResult.validate) return

使用场景

  • Why and What: 描述为什么会有这个插件,它主要在完成一件什么事情。 尽可能描述详细。
  • How: 描述这个插件是怎样使用的,具体的示例代码,甚至提供一个完整的示例,并给出链接。

详细配置

请到 config/config.default.js 查看详细配置项说明。

单元测试

提问交流

请到 egg issues 异步交流。

License

MIT