node package manager
Stop wasting time. Easily manage code sharing in your team. Create a free org »



Checks JSON data for required fields and data types. For REST and other data services testing. For example, model:

    a: "number",
    b: "string",
    c: "boolean"

will return two errors for object below (incorrect type a and missing field c):

    a: "I'm a string but should be a number!",
    b: "whee"


Better check tests/index.js for example.

var dataChecker = require( 'data-checker' ),
    data        = // get your data by HTTP request
    model       = require( './model' );
dataChecker.check( data, model, { callback: callback } );


check( data, model, options )

data: anything that your service returns. Example.

model {Object}: Object with required fields and types for data. Example.

options {Object}: Optional. Object with callback, assert and action methods.

options.callback( err, result ) {Function}

Called when data-checker finished the check.

err: null or {Array} with errors

result: {Object}. { total: {Number}, passed: {Number}, errors: {Array} }

options.assert( isSameType, errorMsg ) {Function}

Called in action if action is not overwritten, gets type check result and error message.


function assert( isSameType, errorMsg ) {
    var assert = require( 'assert' );
    assert.ok( isSameType, errorMsg );

options.action( fieldFromData, typeFromModel, options ) {Function}

Called when types check is required.