0.1.4 • Published 8 years ago
validator-es5 v0.1.4
validator-es5
A data validation utility, can be used to validate e.g. function arguments. Do argument validation improves code quality.
Browser support
Desktop: Chrome, Safari, Firefox, Opera, IE6+
Installation
npm install validator-es5
or
jspm install npm:validator-es5
Usage
Some examples :
var Validator = require('validator-es5');
Validator.isType('string', 'Singapore', 'city'); // true
Validator.isType('string', 15, 'city', false); // false
Validator.isType('string', 15, 'city'); // throw error.
Validator.isType('object', {a:1}, 'data'); // true
Validator.isType('object', null, 'data', false); // false
Validator.isType('number', 15, 'width'); // true
Validator.isType('number', NaN, 'width', false); // false
Validator.isType('number', Infinity, 'width', false); // false
Validator.isType('function', function () {}, 'onclick'); // true
Validator.isType('boolean', false, 'isTriggered'); // true
Validator.isInt(-15, 'width'); // true
Validator.isInt(-15.0, 'width'); // true
Validator.isPosInt(15, 'width'); // true
Validator.isNonEmptyStr('James', 'name'); // true
Validator.isNonEmptyStr('', 'name', false); // false
Validator.isNonEmptyStr(15, 'name'); // throw error.
/**
* Must provide a 3rd argument.
*/
Validator.isType('string', 'Singapore'); // throw error.
Validator.isInObj('a', {a:1, b:2}, 'state'); // true
Validator.isInObj('x', {a:1, b:2}, 'state', false); // false
Validator.isInObj('x', {a:1, b:2}, 'state'); // throw error.
Public properties and methods :
/**
* Check whether a given value is a specified type.
* 1. Null is treated as not `object` .
* 2. NaN, infinite numbers are treated as not `number` .
*
* @param type {String}
* @param val - Any type.
* @param valTxt {String} - Value text is used in thrown error message (if throws).
* @param throwErr {Boolean} - optional - By default, it throws an error when validation fails,
* - Set this argument to 'false' it will always return a Boolean.
* @return {Boolean} or throw error.
*/
isType: function (type, val, valTxt, throwErr)
/**
* Check whether a given property is in the object.
*
* @param val {String} - object property.
* @param obj {Object}
* @param objTxt {String} - Object text is used in thrown error message (if throws).
* @param throwErr {Boolean} - optional - By default, it throws an error when validation fails,
* - Set this argument to 'false' it will always return a Boolean.
* @return {Boolean} or throw error.
*/
isInObj: function (val, obj, objTxt, throwErr)
/**
* Check integer.
*
* @param val - Any type.
* @param valTxt {String} - Value text is used in thrown error message (if throws).
* @param throwErr {Boolean} - optional - By default, it throws an error when validation fails,
* - Set this argument to 'false' it will always return a Boolean.
* @return {Boolean} or throw error.
*/
isInt: function (val, valTxt, throwErr)
/**
* Check positive integer.
*
* @param val - Any type.
* @param valTxt {String} - Value text is used in thrown error message (if throws).
* @param throwErr {Boolean} - optional - By default, it throws an error when validation fails,
* - Set this argument to 'false' it will always return a Boolean.
* @return {Boolean} or throw error.
*/
isPosInt: function (val, valTxt, throwErr)
/**
* Check non-empty string.
*
* @param val - Any type.
* @param valTxt {String} - Value text is used in thrown error message (if throws).
* @param throwErr {Boolean} - optional - By default, it throws an error when validation fails,
* - Set this argument to 'false' it will always return a Boolean.
* @return {Boolean} or throw error.
*/
isNonEmptyStr: function (val, valTxt, throwErr)
Tests
npm test