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

eggjs-validate

v1.0.5

Published

A validate plugin for egg.js

Downloads

15

Readme

eggjs-validate

A validate middleware for egg.js

Install

$ npm i eggjs-validate --save

Usage

1.Create a middleware file app/middleware/validator.js

'use strict';

module.exports = require('eggjs-validate');

2.Open a middleware in config/config.default.js

config.middleware = [
    'validator'
];

3.Add rules to app/rules/

// app/rules/user.js

"use strict";

module.exports = {
  getUserInfo: {
    id: {
      required: true,
      message: 'id不能为空'
    }
  }
}

// app/rules/news.js

"use strict";

module.exports = {
  addNews: {
    link: [
      {
        required: true,
        message: 'link不能为空'
      },
      {
        type: 'string',
        message: 'link 必须为是链接',
        validator: (rule, value) => {
          return rules.isURL(value);
        },
      },
    ]
  }
}

4.Use in app/controller/user.js

    const { body } = this.ctx.request
    try {
      ...
      await this.ctx.validator('user.getUserInfo', body)
      ...
    }catch (e) {
      this.ctx.body = {
        message: e.message
      }
    }

Api

type

  • string: Must be of type string. This is the default type.
  • number: Must be of type number.
  • boolean: Must be of type boolean.
  • method: Must be of type function.
  • regexp: Must be an instance of RegExp or a string that does not generate an exception when creating a new RegExp.
  • integer: Must be of type number and an integer.
  • float: Must be of type number and a floating point number.
  • array: Must be an array as determined by Array.isArray.
  • object: Must be of type object and not Array.isArray.
  • enum: Value must exist in the enum.
  • date: Value must be valid as determined by Date
  • url: Must be of type url.
  • hex: Must be of type hex.
  • email: Must be of type email.
  • any: Can be any type.

message

error message.

asyncValidator

Asynchronous check.

{
        type: 'string',
        message: 'link 必须为是链接',
        asyncValidator: async(rule, value) => {
          const res = await request.get(...)
          ...
          return true
        },
},

validator

Synchronous check.

Rules

Please refer to https://www.npmjs.com/package/validator

License

Everything is MIT.