Socket
Socket
Sign inDemoInstall

validate.io

Package Overview
Dependencies
115
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    validate.io

Validation utilities.


Version published
Weekly downloads
4
decreased by-63.64%
Maintainers
1
Install size
931 kB
Created
Weekly downloads
 

Readme

Source

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 © 2014-2015. The Validate.io Authors.

Keywords

FAQs

Last updated on 05 Dec 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc