Coder Social home page Coder Social logo

rdkamali / tincanvalidator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rusticisoftware/tincanvalidator

0.0 1.0 0.0 147 KB

JavaScript Utilities to do validation of Tin Can structures based off of TinCanSchema

License: Apache License 2.0

Makefile 0.51% JavaScript 93.09% Shell 6.40%

tincanvalidator's Introduction

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

tincanvalidator's People

Contributors

chaimleib avatar

Watchers

Kamali avatar

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.