Git Product home page Git Product logo

snapsjot's Introduction

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

snapsjot's People

Contributors

robert-van-engelen avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

sgml muescha

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.