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

lazy-validate

v1.0.0

Published

js表单校验器

Downloads

15

Readme

js表单校验器

引入

import Validator from 'lazy-validator';

初始化 new Validator([options])

// options 是校验器的一些配置项
let options = {
  required: true
}
let validator = new Validator(options);

文档

new Validator([options])

//options
required     必填项不写时,默认为false

validator.validate(data,rules)

  • data 需要校验的数据
  • rules 校验规则

使用规则校验数据

  let Validator = require('lazy-validator');
  let validator = new Validator();
  let data = {
    name: 1111, 
    age: 222,
  }

  //方法一:简写模式
  let rules = {
    name: '名字', //简写,这里直接跟校验字段的名字就行,参数默认判断是必填
    age: '年龄'
  }

  //方法二:详细模式(只有名字是必填)
  let rules = {
    name: {
     name:'名字', //跟数据对应的名字(必填)
     //type和types 只能有一个
     type: ["Number", "String"], //数组,一个为true就为true
     types: ["Number", "String"], //数组,全部为true就为true
     reg:'/d/', //正则
     errMsg: "名字填写错误", //出错时的提示(不填有默认提示)
     required: true, //是否必填,默认true(如果为false,有校验规则就校验,没有就忽略)
     //校验前对数据进行一些处理
     format:{ 
       trim: "between", //去掉空格(left,center,right,between,all)
     }
    },
    age: {
      name:"年龄"
    }
  }

  

  let [errMsg,errMsgArr] = validator.validate(data, rules);
  //[errMsg,errMsgArr]  如果验证通过,errMsg为null,如果没有通过,errMsg为第一条报错的错误提示,errMsgArr为所有的报错提示
  if(errMsg){console.log('校验没通过!原因是:'+ errMsg); return}