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

type-json-validator

v2.0.5

Published

JSON类型实现测试 - 一款深度校验JSON数据类型及结构的工具

Downloads

1

Readme

Type-JSON

项目介绍

  • JSON类型实现测试 - 一款深度校验JSON数据类型及结构的工具

功能

  • 支持浏览器和 node 端
  • 深度检测
  • 数组支持强测试数组长度
  • 支持 JSON 所有数据类型

安装

  • (c)npm install type-json-validator -D

使用

Browser
<script src="lib/browser.min.js"></script>
const typeJsonValidator = new TypeJsonValidator({
    username: String,
    status: Boolean
  });
  try {
    typeJsonValidator.test({
      username: 'long',
      status: 1
    });
  } catch (e) {
    // Error 实例
    console.log(e);
    console.log(e.message); // 'status' error: expected value type is 'boolean'
}
ES6
import TypeJsonValidator from 'type-json-validator';
const typeJsonValidator = new TypeJsonValidator();
typeJsonValidator.test();
node
const TypeJsonValidator = require('type-json-validator/lib/node.lib');
const typeJsonValidator = new TypeJsonValidator();
typeJsonValidator.test();

API

  • constructor
    • TypeJsonValidator
      • param
        • interface <Array | Object>
  • methods
    • test
      • param
        • data <Array | Object>
      • return
        • void
  • example
    • constructor
      • new TypeJsonValidator([ String ])
      • new TypeJsonValidator({ username: String, list: [ String ] })
    • methods
      • typeJsonValidator.test([ 1, 2 ])
      • typeJsonValidator.test({ username: 'long', list: [] })

异常处理

由于使用深度递归检测、为保证更快的效率和检测继续,采用抛出一个 Error 对象来终止检测。需要使用 try 块里面来去执行代码

const TypeJsonValidator = require('type-json-validator/lib/node.lib');
const typeJsonValidator = new TypeJsonValidator({
  username: String
});
try {
  typeJsonValidator.test({
    username: '2020'
  });
} catch (e) {
  console.log(e)
}

也可以提供一个封装函数+配合ES6来使代码看起来显得更加友好

const TypeJsonValidator = require('type-json-validator/lib/node.lib');
const typeJsonValidator = new TypeJsonValidator({
  username: String
});
/**
 * @param typeJsonValidatorIns {Object}
 * @param jsonData {Object | Array}
 * */
function implementTest(typeJsonValidatorIns, jsonData) {
  return new Promise((resolve, reject) => {
    try {
      typeJsonValidatorIns.test(jsonData);
      resolve();
    } catch (e) {
      reject(e);
    }
  });
}
implementTest(typeJsonValidator, {
  username: '2020'
})
  .then(() => {
    console.log('success')
  })
  .catch(error => {
    console.log(error)
  })

其他

  • 强测试数组长度
const TypeJsonValidator = require('type-json-validator/lib/node.lib');
const typeJsonValidator = new TypeJsonValidator({
  list: [Number] // 该数组下只需实现数组的每一项都是 Number 类型即可
});
try {
  typeJsonValidator.test({
    list: [1, 2]
  });
  // 通过
} catch (e) {
  console.log(e)
}
// ---------------
const typeJsonValidator2 = new TypeJsonValidator({
  list: [Number, {
    age: 20
  }] // 该数组下需要实现第 0 项为 Number 类型、 第 1 项 为 Object 类型
});
try {
  typeJsonValidator2.test({
    list: [1, 2]
  });
} catch (e) {
  console.log(e) // 'list.1' error: expected value type is 'object'
}
try {
  typeJsonValidator2.test({
    list: [1, {
      age: 2020
    }]
  });
  // 通过
} catch (e) {
  console.log(e)
}
  • 字面量表示接口类型

推荐使用 NumberStringBooleannullArrayObject 类型来约定。由于 ArrayObject 是复杂数据类型,其可以包含其他数据就可以是用 []{} 来约定

const TypeJsonValidator = require('type-json-validator/lib/node.lib');
const typeJsonValidator = new TypeJsonValidator({
  a: Number,
  b: String,
  c: Boolean,
  d: null,
  e: Array,
  f: Object
});
try {
  typeJsonValidator.test({
    a: 1,
    b: 'string',
    c: true,
    d: null,
    e: [],
    f: {}
  });
  // 通过
} catch (e) {
  console.log(e)
}

也可以使用字面量方式约定,但是这样显得不太友好

const typeJsonValidator = new TypeJsonValidator({
  a: 0,
  b: '',
  c: true,
  d: null,
  e: [],
  f: {}
});

也就是说在这里不管是构造函数和字面量值都能准确判断该值是什么类型,实现该判断的核心函数

/**
 * @description 获取 JSON 可选值的数据类型
 * @param value {Null | Array | Object | Number | String | Boolean}
 * @return {Object<type: String, interface: Function>}
 * */
function getValueType(value) {
  const types = [
    { type: 'null', interface: null },
    { type: 'array', interface: Array },
    { type: 'object', interface: Object },
    { type: 'number', interface: Number },
    { type: 'string', interface: String },
    { type: 'boolean', interface: Boolean }
  ]
  if (typeof value === 'function') {
    return types.find(item => item.interface === value) || {}
  }
  const type = /\[object ([\w\W]+)\]/.exec(({}).toString.call(value))[1].toLowerCase()
  return types.find(item => item.type === type) || {}
}
// 测试
console.log(getValueType(String))           // { type: 'string', interface: [Function: String] }
console.log(getValueType('string'))         // { type: 'string', interface: [Function: String] }
console.log(getValueType(Number))           // { type: 'number', interface: [Function: Number] }
console.log(getValueType(2020))             // { type: 'number', interface: [Function: Number] }
console.log(getValueType(Boolean))          // { type: 'boolean', interface: [Function: Boolean] }
console.log(getValueType(true))             // { type: 'boolean', interface: [Function: Boolean] }
console.log(getValueType(Array))            // { type: 'array', interface: [Function: Array] }
console.log(getValueType([]))               // { type: 'array', interface: [Function: Array] }
console.log(getValueType(Object))           // { type: 'object', interface: [Function: Object] }
console.log(getValueType({}))               // { type: 'object', interface: [Function: Object] }
console.log(getValueType(null))             // { type: 'null', interface: null }