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

json-semantic

v1.6.4

Published

A specification and implementation for verifying and generating(mock) JSON data.

Downloads

75

Readme

JSON Semantic

A specification and implementation for verifying and generating(mock) JSON data .

Example

import { generate, htmlFormat, ObjectSchema, parse, stringify, verify } from "json-semantic";

const testSchema: ObjectSchema = {
  number: 0,
  string: 'string',
  boolean: true,
  array: [1, 2],
  object: {
    a: 1,
    b: {
      __type: 'string',
      __format: /(a|c)/
    },
  },
  numberWithSchema: {
    __type: 'integer',
    __min: 1,
    __max: 10,
  },
  stringWithSchema: {
    __type: 'string',
    __format: /135\d{8}/,
  },
  stringWithSchema1: {
    __type: 'string',
    __format: 'email',
  },
  arrayWithSchema: {
    __type: 'array',
    __min: 2,
    __max: 5,
    __item: {
      a: {
        __type: 'string',
        __format: /\d+/,
      },
    },
  },
};

const schemaStr = stringify(testSchema);
console.info('generate: ', generate(testSchema));

// 输出
/*
{
  number: 0,
  string: 'string',
  boolean: true,
  array: [1, 2],
  object: { a: 1, b: 'a' },
  numberWithSchema: 7,
  stringWithSchema: '13594732735',
  stringWithSchema1: '[email protected]',
  arrayWithSchema: [{ a: '8481' }]
}
*/

console.info('parse: ', parse(schemaStr));

const jsonData = {
  "number": 1,
  "string": "string1",
  "boolean": false,
  "array": [2, 2],
  "object": { "a": 2, "b": "b" },
  "numberWithSchema": 4,
  "stringWithSchema": "13529277784",
  "stringWithSchema1": "[email protected]",
  "arrayWithSchema": [{ "a": "6832" }, { "a": "56" }, { "b": "7248" }, { "a": "48" }]
};
const delta = verify(jsonData, testSchema);
console.info('verify: ', delta);

if (delta) {
  console.info('visual: ', htmlFormat(delta, jsonData))
}

schema参数

参数 | 含义 | 取值 ---|---|--- __type | 类型 | string|integer|float|boolean|array|ref __format | 格式,当__type等于string时设置 | cname | name | phone | paragraph | cparagraph | sentence | csentence | word | cword| title | ctitle | hex | rgb | rgba | hsl| image | dataImage | date | time | datetime | now | url | protocol | domain | email | ip | id | RegExp __min | 最小值,当__type等于integerfloat时表示数字最小值,当__type等于array时表示数组长度最小值 | 数字 __max | 最小值,当__type等于integerfloat时表示数字最大值,当__type等于array时表示数组长度最大值 | 数字 __item | 数组元素 | json结构 __ratio | 几率 | 当__type等于boolean时表示true出现的几率 __jsonPath | json path | 当__type等于ref时, 取值路径, $.self已被内部占用