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

snapsjot

v1.4.1

Published

Companion module for SJOT schemas (npm package sjot), creates a SJOT schema from JSON data to efficiently validate and type-check JSON data streams.

Downloads

9

Readme

logo

npm version build status license

snapSJOT

Creates SJOT schemas from JSON data, a companion module for SJOT schemas sjot.org npm package sjot.js GitHub repository Genivia/SJOT.

Live demo SJOT validator, schema converters and snapSJOT creator.

Installation

npm install snapsjot

How to snapSJOT JSON

snapSJOT.convert(data) returns a SJOT schema for the given JSON or JS value. Use snapSJOT with node.js as follows:

var snapSJOT = require("snapsjot");
var data =
[
  {
    "name":    "SJOT",
    "v":       "1.3.7",
    "tags":    [ "JSON", "SJOT", "validator" ],
    "package": { "id": 1, "name": "sjot" }
  },
  {
    "name":    "SNAPSJOT",
    "v":       1.3,
    "tags":    [ "JSON", "SJOT", "converter" ],
    "package": { "id": 2, "name": "snapsjot", "opt": true }
  }
];
var schema = snapSJOT.convert(data);
console.log(JSON.stringify(schema, null, 2));

This creates and displays the following SJOT schema:

{
  "@note": "SJOT schema created from JSON data by snapSJOT",
  "@root": [
    {
      "@final": true,
      "name": "string",
      "v": [[ "string", "number" ]],
      "tags": [ "string" ],
      "package": {
        "@final": true,
        "id": "number",
        "name": "string",
        "opt?": "boolean"
      }
    }
  ]
}

where @root specifies that the root type of the JSON data is an array of objects, @final means that the object is not extensible (remove if extensibility is desired), the "string" type matches any string, the "number" type matches any number and the "boolean" type matches true or false. These types can be further restricted as specified by SJOT schema syntax. The [[ "string", "number" ]] with "string" and "number" types is a type choice that matches any string or number. An object property name ending in a ? is optional.

To validate JSON or type check JS values with SJOT schemas, use the SJOT validator by installing npm package sjot:

npm install sjot

and use SJOT.valid(data, "@root", schema) to validate data against a SJOT schema:

var SJOT = require("sjot");

var schema = ...; // SJOT schema (e.g. generated with snapSJOT)

var data = ...;   // some data to validate (or type check)

if (SJOT.valid(data, "@root", schema))
  ... // OK: data validated against schema

Because snapSJOT generates only one root type, you can simply pass the root type to SJOT.valid() to type-check the data as follows:

var type = schema["@root"]; // just use the SJOT root type

if (SJOT.valid(data, type))
  ... // OK: data type-checked

How to contribute?

We love feedback and contributions to this project. Please read CONTRIBUTING for details.

Changelog

  • Nov 14, 2017: snapsjot 1.3.17 added snapSJOT snapsjot.js schema creator to convert JSON data to SJOT schemas
  • Nov 15, 2017: snapsjot 1.3.18 fixed index.js
  • Nov 16, 2017: snapsjot 1.4.0 updates
  • Nov 18, 2017: snapsjot 1.4.1 minor updates