Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    data-checkerpublic

    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.

    Keywords

    none

    install

    npm i data-checker

    Downloadsweekly downloads

    4

    version

    1.0.0

    license

    none

    repository

    githubgithub

    last publish

    collaborators

    • avatar