Skip to content

Companion module for SJOT schemas, creates a SJOT schema from JSON data to efficiently validate and type-check JSON data streams

License

Notifications You must be signed in to change notification settings

Genivia/snapSJOT

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

About

Companion module for SJOT schemas, creates a SJOT schema from JSON data to efficiently validate and type-check JSON data streams

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks