validate.io
v2.0.7
Published
Validation utilities.
Downloads
66
Maintainers
Keywords
Readme
Validate
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).
- Installation
- Usage
- Methods
- Strings
- Booleans
- Numbers
- isNumber()
- isNumberPrimitive()
- isNaN()
- isNaNPrimitive()
- isFloat()
- isPositive()
- isPositivePrimitive()
- isPositiveZero()
- isNegative()
- isNegativeZero()
- isNonnegative()
- isNonpositive()
- isInteger()
- isIntegerPrimitive()
- isSafeInteger()
- isNonnegativeInteger()
- isNonpositiveInteger()
- isNegativeInteger()
- isPositiveInteger()
- isFinite()
- isLessThan()
- isGreaterThan()
- onInterval()
- Null
- Undefined
- Objects
- Arrays
- isArray()
- isStringArray()
- isStringPrimitiveArray()
- isAlphanumericArray()
- isBooleanArray()
- isBooleanPrimitiveArray()
- isNumberArray()
- isNumberPrimitiveArray()
- isIntegerArray()
- isSafeIntegerArray()
- isNonnegativeIntegerArray()
- isPositiveIntegerArray()
- isNegativeIntegerArray()
- isNonpositiveIntegerArray()
- isArrayArray()
- isObjectArray()
- isPrimitiveArray()
- isLogicalArray()
- isFunctionArray()
- isTypedArray()
- isInt8Array()
- isUint8Array()
- isUint8ClampedArray()
- isInt16Array()
- isUint16Array()
- isInt32Array()
- isUint32Array()
- isFloat32Array()
- isFloat64Array()
- isUnique()
- isPermutation()
- isSquareMatrix()
- isStrictDateArray()
- hasMax()
- hasMin()
- hasNumMax()
- hasNumMin()
- hasSize()
- contains()
- arrayLike()
- typedArrayLike()
- matrixLike()
- ndarrayLike()
- isMatrix()
- Functions
- Regular Expressions
- Time
- Other
- Examples
- Tests
- 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
Copyright
Copyright © 2014-2015. The Validate.io Authors.