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

mod-validator

v0.0.9

Published

A js validation module inspired by laravel validation.

Readme

validator.js Build Status NPM version

一个类似laravel的js验证模块.

安装

  1. 在Nodejs使用
$ npm install mod-validator
  1. 浏览器里使用

下载本项目lib/validator.js 到您的项目目录,引入即可:

<script src="path/to/validator.js"></script>

基本用法

var Validator = require("validator");// 浏览器就不用这句了,Validator是全局变量
var rules = {
  username: 'required|min:5',
  password: 'required|confirmed|min:6|max:16',
}

var data = {
  username: 'test',
  password: '123456',
}

var v = Validator.make(data, rules); // 或者: var  v = new Validator(data, rules)

if(v.fails()) {
  console.log(v.messages());
  //or
  console.log(v.errors());
}

定义验证规则

  • "|" 分隔的字符串形式
var rules = {
  username: 'required|min:5',
  password: 'required|confirmed|min:6|max:16',
  email: 'email'
}
  • 数组形式
var rules = {
  username: ['required', 'min:5'],
  password: ['required', ['confirmed'], ['min:6'], ['max:16'],
  email: ['required', 'email']
}

API

  • 获取验证结果

    • 语法:
      • {Boolean} Validator.passes() 是否通过
      • {Boolean} Validator.fails() 是否验证失败
    • 举例:
     var rules = {
       username: 'required|min:5',
       password: 'required|confirmed|min:6|max:16',
     }
    
     var data = {
       username: 'test',
       password: '123456',
     }
    
     v = validator.make(data, rules);
    
     if (v.passes()) {
       // 如果全部通过验证
     }
     // 或者
     if (v.fails()) {
       // 如果没通过验证
     }
  • 自定义错误消息

    • 语法:{Void} Validator.mergeMessage(attribute [, message])
      • Validator.mergeMessage({attributeName, message})
      • Validator.mergeMessage('attributeName', 'message')
    • 属性替换:在消息字符串用使用:attribute 作为属性名占位符。
    • 举例:
    var messages = {
       required: ':attribute 不能为空.',
       // ...
    }
    ...
    v = validator.make(data, rules);
    v.mergeMessage(messages);
    if(v.fails()) {
      console.log(v.errors());
    }
    
    //------------------------------------------
    
    v.mergeMessage({required: ':attribute 不能为空.'})
    //以上用法等同于:
    v.mergeMessage('required', ':attribute 不能为空.');
  • 自定义属性别名

    • 语法:{Void} Validator.mergeAttribute(attribute [, alias])
      • Validator.mergeAttribute({attributeName, alias})
      • Validator.mergeAttribute('attributeName', 'alias')
    • 举例:
    var attributes = {
       username: '用户名',
       password: '密码'
       //...
    }
    v = validator.make(data, rules);
    v.mergeAttribute(attributes);
    
    if(v.fails()) {
      console.log(v.messages());
    }
    
    //------------------------------------------
    
    v.mergeAttribute({username: '用户名'})
    // 以上等同于:
    v.mergeAttribute('username', '用户名');

#License

MIT