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-message-parameter

v1.3.0

Published

A parameter verify tools.

Downloads

2

Readme

koa-message-parameter

基于parameter和koa封装成的验证工具,可以自定义message

Install

$ npm install koa-message-parameter --save

Usage

const Koa = require('koa');
const parameter = require('koa-message-parameter');

const app = new Koa();

parameter(app); // add verifyParams method, but don't add middleware to catch the error
// app.use(parameter(app)); // also add a middleware to catch the error.

app.use(async function (ctx) {
  ctx.verifyParams({
    name: {
      type: 'string',
      message: '请输入用户名!',
    },
    age: {
      type: 'int',
      min: [5, '年龄不得小于5岁!'],
    },
  });
});

Translate

You can override the translate method of parameter to implement I18n, by passing a function like this :

const Koa = require('koa');
const parameter = require('koa-message-parameter');

const app = new Koa();

parameter(app, function() {
  // Same example with node-parameter
  var args = Array.prototype.slice.call(arguments);
  // Assume there have I18n.t method for convert language.
  return I18n.t.apply(I18n, args);
});

app.use(async function (ctx) {
  ctx.verifyParams({
    name: 'string'
  });
});

Rule

common rule

  • required - if required is set to false, this property can be null or undefined. default to true.
  • type - The type of property, every type has it's own rule for the validate.
  • convertType - Make parameter convert the input param to the specific type, support int, number, string and boolean, also support a function to customize your own convert method.
  • default - The default value of property, once the property is allowed non-required and missed, parameter will use this as the default value. This may change the original input params.
  • widelyUndefined - override options.widelyUndefined
  • message - Custom error tips when checking errors

Note: you can combile require and type end with a notation ? like: int? or string? to specific both type and non-required.

int

If type is int, there has tow addition rules:

  • max - The maximum of the value, value must <= max.
  • min - The minimum of the value, value must >= min.

Default convertType is int.

Note: default convertType will only work when options.convert set to true in parameter's constructor.

integer

Alias to int.

number

If type is number, there has tow addition rules:

  • max - The maximum of the value, value must <= max.
  • min - The minimum of the value, value must >= min.

Default convertType is number.

date

The date type want to match YYYY-MM-DD type date string.

Default convertType is string.

dateTime

The dateTime type want to match YYYY-MM-DD HH:mm:ss type date string.

Default convertType is string.

datetime

Alias to dateTime.

id

The id type want to match /^\d+$/ type date string.

Default convertType is string.

boolean

Match boolean type value.

Default convertType is boolean.

bool

Alias to boolean

string

If type is string, there has four addition rules:

  • allowEmpty(alias to empty) - allow empty string, default to false. If rule.required set to false, allowEmpty will be set to true by default.
  • format - A RegExp to check string's format.
  • max - The maximum length of the string.
  • min - The minimum length of the string.
  • trim - Trim the string before check, default is false.

Default convertType is string.

email

The email type want to match RFC 5322 email address.

  • allowEmpty - allow empty string, default is false.

Default convertType is string.

password

The password type want to match /^$/ type string.

  • compare - Compare field to check equal, default null, not check.
  • max - The maximum length of the password.
  • min - The minimum length of the password, default is 6.

Default convertType is string.

url

The url type want to match web url.

Default convertType is string.

enum

If type is enum, it requires an addition rule:

  • values - An array of data, value must be one on them. this rule is required.

object

If type is object, there has one addition rule:

  • rule - An object that validate the properties ot the object.

array

If type is array, there has four addition rule:

  • itemType - The type of every item in this array.
  • rule - An object that validate the items of the array. Only work with itemType.
  • max - The maximun length of the array.
  • min - The minimun lenght of the array.

abbr

  • 'int' => {type: 'int', required: true}
  • 'int?' => {type: 'int', required: false }
  • 'integer' => {type: 'integer', required: true}
  • 'number' => {type: 'number', required: true}
  • 'date' => {type: 'date', required: true}
  • 'dateTime' => {type: 'dateTime', required: true}
  • 'id' => {type: 'id', required: true}
  • 'boolean' => {type: 'boolean', required: true}
  • 'bool' => {type: 'bool', required: true}
  • 'string' => {type: 'string', required: true, allowEmpty: false}
  • 'string?' => {type: 'string', required: false, allowEmpty: true}
  • 'email' => {type: 'email', required: true, allowEmpty: false, format: EMAIL_RE}
  • 'password' => {type: 'password', required: true, allowEmpty: false, format: PASSWORD_RE, min: 6}
  • 'object' => {type: 'object', required: true}
  • 'array' => {type: 'array', required: true}
  • [1, 2] => {type: 'enum', values: [1, 2]}
  • /\d+/ => {type: 'string', required: true, allowEmpty: false, format: /\d+/}

much message


 {
   ... 
   min: [2, 'can't smaller then 2']
   ...
 }

=== equal to
 {
   ... 
   min:2,
   message: 'can't smaller then 2'
   ...
 }

(min,max,format,compare all available)

errors examples

{
  code: '100000',
  message: 'required'
}