data-checker
v1.0.0
Published
Checks JSON data for required fields and data types. For REST and other data services testing.
Downloads
1
Readme
data-checker
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 ) {Function}
Called when types check is required.