Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including validate.io with all npm packages installed. Try it out:

var validate = require( "validate.io" ); function is( bool, value, msg ) { if ( validate.isRegexp( value ) ) { value = value.toString(); } else if ( validate.isFunction( value ) ) { value = value.constructor.name; } else { value = JSON.stringify( value ); } console.log( "%s is %s%s", value, ( bool ) ? "" : "not ", msg ); } var methods, bool, vals, fcn, msg, v, N, M, i, j; vals = [ "beep", 5, Math.PI, -0, true, null, undefined, [], {}, function foo(){}, /.*/, new Date(), -1, JSON.parse ]; methods = [ [ validate.isStringPrimitive, "a string" ], [ validate.isNegativeZero, "negative zero" ], [ validate.isPositiveInteger, "a positive integer" ], [ validate.isPositive, "a positive number" ], [ validate.isNegativeInteger, "a negative integer" ], [ validate.isNull, "null" ], [ validate.isUndefined, "undefined" ], [ validate.isBoolean, "a boolean" ], [ validate.isNativeFunction, "a native function" ], [ validate.isFunction, "a function" ], [ validate.isArray, "an array" ], [ validate.isStrictDate, "a date object" ], [ validate.isRegexp, "a regular expression" ], [ validate.isObject, "an object" ] ]; N = vals.length; M = methods.length; for ( i = 0; i < N; i++ ) { v = vals[ i ]; for ( j = 0; j < M; j++ ) { fcn = methods[ j ][ 0 ]; msg = methods[ j ][ 1 ]; bool = fcn( v ); if ( bool ) { is( bool, v, msg ); break; } else { is( bool, v, msg ); } } }

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

validate.io v2.0.7

Validation utilities.

Validate

NPM version Build Status Coverage Dependencies

Validation utilities.

The primary motivation for this module is to validate input arguments provided to publicly exposed methods. Other utilities exist but are either limited, more verbose (use method chaining), used as if asynchronous (callbacks), or part of some larger utility library (e.g., underscore).

  1. Installation
  2. Usage
  3. Methods
  4. Examples
  5. Tests
  6. License

Installation

$ npm install validate.io

Usage

var validate = require( 'validate.io' );

Methods

The validate module is comprised of several smaller modules. If you want to roll your own validate, follow the links and import the individual modules. For method documentation, see each respective module.


Examples

var validate = require( 'validate.io' );

function is( bool, value, msg ) {
    if ( validate.isRegexp( value ) ) {
        value = value.toString();
    }
    else if ( validate.isFunction( value ) ) {
        value = value.constructor.name;
    }
    else {
        value = JSON.stringify( value );
    }
    console.log( '%s is %s%s', value, ( bool ) ? '' : 'not ', msg );
}

var methods,
    bool,
    vals,
    fcn,
    msg,
    v,
    N, M,
    i, j;

vals = [
    'beep',
    5,
    Math.PI,
    -0,
    true,
    null,
    undefined,
    [],
    {},
    function foo(){},
    /.*/,
    new Date(),
    -1,
    JSON.parse
];

methods = [
    [ validate.isStringPrimitive, 'a string' ],
    [ validate.isNegativeZero, 'negative zero' ],
    [ validate.isPositiveInteger, 'a positive integer' ],
    [ validate.isPositive, 'a positive number' ],
    [ validate.isNegativeInteger, 'a negative integer' ],
    [ validate.isNull, 'null' ],
    [ validate.isUndefined, 'undefined' ],
    [ validate.isBoolean, 'a boolean' ],
    [ validate.isNativeFunction, 'a native function' ],
    [ validate.isFunction, 'a function' ],
    [ validate.isArray, 'an array' ],
    [ validate.isStrictDate, 'a date object' ],
    [ validate.isRegexp, 'a regular expression' ],
    [ validate.isObject, 'an object' ]
];

N = vals.length;
M = methods.length;
for ( i = 0; i < N; i++ ) {
    v = vals[ i ];
    for ( j = 0; j < M; j++ ) {
        fcn = methods[ j ][ 0 ];
        msg = methods[ j ][ 1 ];
        bool = fcn( v );
        if ( bool ) {
            is( bool, v, msg );
            break;
        } else {
            is( bool, v, msg );
        }
    }
}

To run the example code from the top-level application directory,

$ node ./examples/index.js

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

License

MIT license.

Copyright

Copyright © 2014-2015. The Validate.io Authors.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free