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

jsonchk

v1.0.5

Published

JSON struct checker

Readme

JSON Struct Checker (for NodeJS)

1. 说明

这是一个对JSON数据(或其他JS对象)的数据结构进行校验的函数。 在编程过程中,可以通过编写一个JSON结构的Schema,来表示你所预期的数据结构,然后使用该函数对目标进行检查。 函数会返回Boolean类型的检查结果;如果检查未通过,函数会形成一个Array结构的日志文件供调试审阅。

用于Web的Repo: https://github.com/imnull/json-struct-checker

2. 安装

npm install jsonchk

npm上的项目地址:https://npmjs.org/package/jsonchk

3. 示例

示例代码请参看:https://github.com/imnull/jsonchk/blob/master/test.js

3.1 类型判断

3.1.1 类型字符串比对

//数据中应具备一个名为name,值的typeof为string的属性
var schema = { name : 'string' };
var data = { name : 'MK' };

//数据中应具备一个名为days,Object.prototype.toString回调其值为[object Array]的属性
var schema = { days : '[object Array]' };
var data = { days : [1,2,3] };

3.1.2 类型字符串的正则比对

//值类型为string或Array
var schema = { name : '/^(string|\[object Array\])$/' };
var data1 = { name : 'MK' };
var data2 = { name : ['MK'] };

3.2 函数对值的判断

var schema = {
  age : function(val){
    return typeof(val) == 'number' && val >= 0 && val <= 120;
  }
};

var data1 = { age : 24 };
var data2 = { age : -1 };

3.3 嵌套判断

var schema = {
  child : {
    name : 'string',
    age : 'number',
    wife : '^(undefined|\[object Object\])$'
  }
}
var data1 = {
  child : {
    name : 'Even',
    age : 6
  }
}
var data2 = {
  child : {
    name : 'John',
    age : 55,
    wife : {
      //...
    }
  }
}

4. 特殊字符

4.1 特殊类型字符

4.1.1 "*" 允许所有类型

var schema = { style : '*' };
var data1 = { style : "width:20px" };
var data2 = { style : { width : '20px' } }
var data3 = {};

4.2 特殊引用字符

4.2.1 "@" 引用父级schema进行检测

var schema = {
  name : 'string',
  age : 'number',
  wife : '@'
}

var data1 = {
  name : 'John',
  age : 55
}

var data2 = {
  name : 'Li Lei',
  age : 24,
  wife : {
    name : 'Han Meimei',
    age : 23
  }
}

5. 关于log参数

该参数为外部引用变量,类型为数组。如果代入该引用,那么当检查出现错误时,会向此参数推入一条记录。一条记录为一个数组,按顺序为: namespace, error-type, error-description, current-checker, current-value

6. 关于namespace参数

表述出错时的层级关系