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

tin-can-json-schema-validator

v1.0.0

Published

Tin Can JSON Schema Validator

Downloads

6

Readme

Install

This software requires Node.js to be installed. This can be done using the standalone installer, or using your OS's package manager.

To install:

git clone https://github.com/RusticiSoftware/TinCanValidator.git
cd TinCanValidator
make
make test

What is this?

Tin Can Validator takes JSON objects in, and tells you if they are well-formed and valid according to the Tin Can API. It will also (optionally) attempt to give informative error messages, telling you what went wrong, if anything did.

validate.js is a command-line tool useful for validating individual files. lib contains the JavaScript library on which validate.js is based.

For quick projects, the command-line tool is probably easiest. But if you need to validate many objects without reloading the schema every time, the library will be more efficient.

Tin Can Validator command-line tools

validate.js

Check the structure of TinCan JSON data

Usage: validate.js [file.json] [OPTIONS]

OPTIONS:
  -t, --type=ARG    check against this type id
  -s, --schema=ARG  use this schema directory
  -h, --help        display this help
  -v, --verbose     more informative messages

If file.json is not specified, validates input from stdin.

If no type is specified, tries to validate against all possible types.

If no schema directory is specified, tries to read `dirname /path/to/validate.js`/lib/schema/1.0.1

A schema directory contains schema in JSON schema draft v4 format. Files in the schema directory with extensions other than .json are ignored.

test.js

Located in the test directory. Runs validation checks against the v1.0.1 schema (lib/schema/1.0.1) using all the test JSONs in test/data. Good for making sure that the Tin Can Validator library is set up correctly.

Tin Can Validator library

This Node.js library checks the structure of JavaScript and JSON structures, ensuring that they are valid according to a schema defined by the Tin Can API.

It is in the lib folder. Here is some sample code:

var tcv = require('path/to/TinCanValidator/lib/tincanValidator.js');

tcv.readJsonFile('path/to/test.json', function(err, data) {
    if (err) throw err;
    tcv.validateWithId(data, 'statement', function(err) {
        if (err) {
            console.log("Invalid");
            throw err;
        }
        console.log("Valid");
    });
});

Or, if you want to do it synchronously:

var tcv = require('path/to/TinCanValidator/lib/tincanValidator.js');

var data = tcv.readJsonFileSync('path/to/test.json');
try {
    tcv.validateWithIdSync(data, 'statement');
    console.log("Valid");
} catch (err) {
    console.log("Invalid");
    throw err;
}

Debugging

If an error is thrown, it will usually have a subErrors item, which is an array of any errors that caused the error. Since each sub-error may, in turn, have a subError field of its own, errorUtils.js was developed to easily print the chain of errors. For example:

var tcv = require('path/to/TinCanValidator/lib/tincanValidator.js');
var eu = require('path/to/TinCanValidator/lib/errorUtils.js');

tcv.validateJsonFile('path/to/test.json', function(err) {
    if (!err) return;
    eu.pprintError(err);
    var errJson = eu.pprintErrorToString(err);
});

If you want to also use this system, you can use the addError function to continue the chain of sub-errors:

var tcv = require('path/to/TinCanValidator/lib/tincanValidator.js');
var eu = require('path/to/TinCanValidator/lib/errorUtils.js');

tcv.validateJsonFile('path/to/test.json', function(err) {
    if (!err) return;

    // note that the first argument is an array. Good for when
    // multiple failures have to be traced.
    err = eu.addError([err], "My new error");
    //err.mydata = stuff;
    eu.pprintError(err);
});

Other command-line tools

These tools are mostly useful for debugging the schema itself.

joinSchema.js

Read a directory of JSON schema, and save them in a single file

Usage: joinSchema.js src_dir dst.json [OPTIONS]

OPTIONS:
  -h, --help   display this help
  -q, --quiet  silence most messages

splitSchema.js

Take a JSON schema, and save its parts as separate files

Usage: splitSchema.js src.json dst_dir [OPTIONS]

OPTIONS:
  -h, --help   display this help
  -q, --quiet  silence most messages