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

checkobj

v1.0.17

Published

js 对象校验的 模块

Readme

函数检验

安装

 npm install checkobj

总览

var checkObj=require("checkobj").checkObj;
checkObj(obj,checker)//支数组,对象,字符串、

var gen_checker=require("checkobj").gen_checker;// 生成校验器
gen_checker(obj)// 自动生成校验器

基础

var assert=require("assert");
var checkobj=require("../index");
assert.strictEqual(checkobj.checkObj(true,"boolean"),true);
assert.strictEqual(checkobj.checkObj(false,"string"),false);
assert.strictEqual(checkobj.checkObj(1,"number"),true);
assert.strictEqual(checkobj.checkObj(null,"object"),true);
assert.strictEqual(checkobj.checkObj({},"object"),true);

其中 基础类型只是对 type var 用来做验证

字符串正则校验

checkobj.checkObj("aaaa1",/a{4}/)
true
checkobj.checkObj("aaa1",/a{4}/)
false

标记扩展属性

多级别校验

dat={a:20,b:"asdhasud",c:{a:20,b:"asdhasud"}};
生成的校验器: {"a":"number","b":"string","c":{"a":"number","b":"string"}}

   // 注意 所有的校验器和元素具有一样的树结构;

注意 所有的校验器和元素具有一样的树结构;

增加校验器的声明

为了方便解耦合 ,增加了声明校验器的方法, 用字符串代替重复的校验器 例如

checkObj.delChecker("a4")

checkObj.addChecker("a4",/a{4}/)
checkObj.checkObj("aaaa1","a4")

函数式校验

//对于对象{a:val} 校验器为:{a:function(val,key){ }}  
//回调参数为(val,"a") //值和键
var result=checkObj([4,3,2,1],[function (val,key) {// 逆序检测
    console.log("依次检测"+val+":"+key);
    return val+key===4
}]);
console.log("[4,3,2,1] 函数检验"+result);

该功能检测到的每个数组的值和键 ,并检测是不是值和键 总值为4;;

 var a={"a":2,"b":[3,6,9]};
var checkFunc = {
    a: function (val) {
        return val % 2 === 0;
    }, b: [
        function (val) {
            return val % 3 === 0;
        }
    ]
};

console.log(JSON.stringify(a)+":"+checkObj(a,checkFunc));// true  函数 检测 数据
console.log(JSON.stringify(a.b)+":"+checkObj(a.b,checkFunc.b));// true 函数 检测 数据

数组校验

检测整个数组是不是都为 数值 console.log([3,7,8]+":"+checkObj([3,7,8],["number"]));// true 数组 ,检测器为数组

其他例子

var checker = {
    "a": "undefined", "b": "number",c:""
};
var checker1 = {
    "a": "undefined", "b": "number",c:"",d:{
        "a": "undefined", "b": "number",c:""
    }
};


console.log("空字符串匹配任意类型(false, undefined 不允许设值):"+checkObj({a:"555",b:555,c:undefined},checker));

console.log("默认不允许扩展属性(false):"+checkObj({a:undefined,b:555,c:"test String",d:{a:undefined,b:555,c:"num"}},checker));//false
console.log("正常测试用例(true)"+checkObj({a:undefined,b:555,c:"test String",d:{a:undefined,b:555,c:"num"}},checker1));//true
console.log("正常测试用例(true)"+checkObj({a:undefined,b:555,c:"test String"},checker));//true
console.log("类型错误(false)"+checkObj({a:555,b:"555",c:undefined},checker));

console.log("检测val,key 函数检测器");

###自动生成校验器

var dat=[{a:20,b:"asdhasud"},{a:20,b:"asdhasud"},{a:20,b:"asdhasud"}];
console.log(gen_checker(dat));

生成校验器: [{"a":"number","b":"string"}]

 dat={a:20,b:"asdhasud",c:{a:20,b:"asdhasud"}};
console.log(gen_checker(dat));//{ a: 'number', b: 'string', c: { a: 'number', b: 'string' } }

生成校验器: {"a":"number","b":"string","c":{"a":"number","b":"string"}}

var dat=[{a:20,b:"asdhasud"},{a:20,b:"asdhasud"},{a:20,b:"asdhasud"}];
console.log(gen_checker(dat));  //  [ { a: 'number', b: 'string' } ]

生成校验器:[ { a: 'number', b: 'string' } ]

生成动态ts

根据运行中的 对象 生成类型定义 ;方便使用时查询

例子

require("checkobj").gentsDoc(Object.prototype)
{__defineGetter__:()=>any;
__defineSetter__:()=>any;
hasOwnProperty:()=>any;
__lookupGetter__:()=>any;
__lookupSetter__:()=>any;
isPrototypeOf:()=>any;
propertyIsEnumerable:()=>any;
toString:()=>any;
valueOf:()=>any;
toLocaleString:()=>any}

require("checkobj").gentsDoc(require("parameter").prototype)
///结果 class Object//
{t:()=>any;
validate:(rules, obj)=>any;
addRule:(type, check)=>any}