Skip to content
Checks JSON data for required fields and data types
JavaScript
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
libs
tests
.gitignore
README.md
index.js
package.json

README.md

data-checker

Please use JSON Schema instead

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"
}

Example

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 } );

Methods

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.

Example:

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

    assert.ok( isSameType, errorMsg );
}

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

Called when types check is required. callback gets err {String}, that will be pushed to errors array.

Something went wrong with that request. Please try again.