saguaro
v2.1.0
Published
A simple framework for data validation.
Readme
Saguaro

![]()
A simple framework for data validation.
Installation
npm install saguaroAssertion call
All assertions can be called in two way: constructor call or method call. With method calls, assertion parameters need to be passed within an array.
var is = require('saguaro');
is('foobar', 'alphanum') => true
is.alphanum('foobar') => true
is('foobar', 'contain(foobar)') => true
is.contain('foobar', ['foobar']) => true
is(2, 'within(1, 3)') => true
is.within(2, [1, 3]) => trueMultiple assertions
All assertions can be combined in one call. Assert fail if one of them fail.
var is = require('saguaro');
is('529419940a585fb2a83765b2ca5cc091', 'string,length(32),hexa') => true
is('529419940a585fb2a83765b2ca5cc091', 'string,length(31),hexa') => falseAssert and negation
Negation of any assertion is supported by the ! char.
var is = require('saguaro');
is({}, '!number') => trueAssertion list
above(num), greaterThan(num)
Asserts that the string|number target is above the given value (> num).
var is = require('saguaro');
is(3, 'above(1)') => true
is(1, 'above(1)') => falsealphanum
Asserts that the string|number target contains alphanumerical characters ([0-9a-zA-Z]).
var is = require('saguaro');
is('123Az', 'alphanum') => true
is('123Az@', 'alphanum') => falsearguments
Asserts that the target is an Arguments object.
var is = require('saguaro');
(function () {
is(arguments, 'arguments') => true
is({}, 'arguments') => false
})();array
Asserts that the target is an Array.
var is = require('saguaro');
is([], 'array') => true
is({}, 'array') => falsebelow(num), lessThan(num)
Asserts that the string|number target is below than the given value (< num).
var is = require('saguaro');
is(3, 'below(4)') => true
is(4, 'below(4)') => falseboolean
Asserts that the target is a Boolean.
var is = require('saguaro');
is(true, 'boolean') => true
is(1, 'boolean') => falsebuffer
Asserts that the target is a Buffer.
var is = require('saguaro');
is(new Buffer(1234), 'buffer') => true
is({}, 'buffer') => falsecontain(str)
Asserts that the string target contains another string.
var is = require('saguaro');
is('- foobar -', 'contain(foobar)') => true
is('- foobar -', 'contain(foobar1)') => falsecreditcard
Asserts that the string target is a credit card number (Visa, MasterCard, American Express, Diner Club, Discover, JCB).
var is = require('saguaro');
is(/* A valid credit card number */, 'creditcard') => true
is('foobar', 'creditcard') => falsedate
Asserts that the target is a Date.
var is = require('saguaro');
is(new Date(), 'date') => true
is(1, 'date') => falseAsserts that the string target is an email.
var is = require('saguaro');
is('[email protected]', 'email') => true
is('johndoe-example.com', 'email') => falseempty
Asserts that the target is empty.
var is = require('saguaro');
is('', 'empty') => true
is([], 'empty') => true
is({}, 'empty') => true
is(null, 'empty') => true
is(0, 'empty') => falseequal(value), exactly(value)
Asserts that the target is equal to the given value.
var is = require('saguaro');
is(1.01, 'equal(1.01)') => true
is('foobar', 'equal(foobar)') => true
is('-foobar', 'equal(foobar)') => falseerror
Asserts that the target is an Error.
var is = require('saguaro');
is(new Error(), 'error') => true
is(new TypeError(), 'error') => true
is({ name: 'Error', message: 'an error occurred' }, 'error') => falseexist
Asserts that the target is not undefined or null.
var is = require('saguaro');
is(0, 'exist') => true
is('', 'exist') => true
is(null, 'exist') => false
is(undefined, 'exist') => falsefloat
Asserts that the number target is a Float.
var is = require('saguaro');
is(1.01, 'float') => true
is(1, 'float') => falsefunction
Asserts that the target is a Function.
var is = require('saguaro');
is(function () {}, 'function') => true
is(1, 'function') => falsehexa
Asserts that the string|number target is an hexadecimal ([0-9a-fA-F]).
var is = require('saguaro');
is('0123456789aBcdef', 'hexa') => true
is(123456789, 'hexa') => true
is('foobar', 'hexa') => falseimatch(value)
Asserts that the target matches a case insensitive regular expression.
var is = require('saguaro');
is('- FooBar -', 'imatch(\\bfoo[a-z]{3})') => true
is('foo', 'imatch(foobar)') => falsein(value1, value2 ..., valueN)
Asserts that the strin|number|array target matches at least one element in value list.
var is = require('saguaro');
is(1, 'in(1, 2)') => true
is('foo', 'in(foo, bar)') => true
is(3, 'in(1, 2)') => false
is([1, 2, 3], 'in(3, 4, 5)') => true
is([1, 2, 3], 'in(4, 5)') => falseinclude(value1, value2 ..., valueN)
Asserts that the target include all elements in value list.
var is = require('saguaro');
is([1, 2, 3], 'include(2)') => true
is([1, 2, 3], 'include(2, 3)') => true
is([1, 3], 'include(2)') => falseinteger, int
Asserts that the target is an Integer.
var is = require('saguaro');
is(1, 'integer') => true
is(1.01, 'integer') => falseip
Asserts that the target is an ip.
var is = require('saguaro');
is('255.255.255.255', 'ip') => true
is('256.255.255.255', 'ip') => falsejson
Asserts that the target is a JSON notation.
var is = require('saguaro');
is('{ "foo": "bar" }', 'json') => true
is('foobar', 'json') => falsekey(value)
Asserts that the object target has the key value.
var is = require('saguaro');
is({ 'foo': 'bar' }, 'key(foo)') => true
is({}, 'key(toString)') => falselat
Asserts that the number|string target is a latitude value.
var is = require('saguaro');
is(37.4418834, 'lat') => true
is(90.01, 'lat') => falseleast(value)
Asserts that the number|string target is greater than or equal to the given value (>= num).
var is = require('saguaro');
is(1, 'least(1)') => true
is(0.9, 'least(1)') => falselength(min, max), size(min, max)
Asserts that the number|string target is within a range or equal to the given value min.
var is = require('saguaro');
is('foobar', 'length(6)') => true
is('foobar', 'length(4, 6)') => true
is(['foo', 'bar'], 'length(1, 2)') => true
is({ 'foo': 'bar' }, 'length(1, 2)') => true
is('foobar', 'length(1)') => falselng
Asserts that the number|string target is a longitude value.
var is = require('saguaro');
is(-122.1430195, 'lng') => true
is(180.01, 'lng') => falsematch(str)
Asserts that the number|string target matches a regular expression.
var is = require('saguaro');
is('- fooBar -', 'match(\\bfoo[a-z]{3})') => true
is('Foo', 'match(foo)') => falsemost(num)
Asserts that the number target is less than or equal to the given value (<= num).
var is = require('saguaro');
is(1, 'most(1)') => true
is(1.1, 'most(1)') => falseNaN
Asserts that the target is an illegal number (NaN).
var is = require('saguaro');
is(0 / 0, 'NaN') => true
is(1, 'NaN') => falsenegative
Asserts that the number target is a negative Number.
var is = require('saguaro');
is(-1, 'negative') => true
is(0, 'negative') => falsenumber
Asserts that the target is a Number.
var is = require('saguaro');
is(123, 'number') => true
is('foobar', 'number') => falseobject
Asserts that the target is an Object.
var is = require('saguaro');
is({}, 'object') => true
is([], 'object') => falsepositive
Asserts that the number target is a positive Number.
var is = require('saguaro');
is(1, 'positive') => true
is(0, 'positive') => falseregexp
Asserts that the target is a regular expression.
var is = require('saguaro');
is(/foobar/, 'regexp') => true
is(1, 'regexp') => falsestring
Asserts that the target is a String.
var is = require('saguaro');
is('foobar', 'string') => true
is(1, 'string') => falseurl
Asserts that the target is an url (valid protocol, hostname and pathname).
var is = require('saguaro');
is('http://foo.bar:8080/some/uri', 'url') => true
is('foobar', 'url') => falsewithin(from, to), between(from, to), range(from, to)
Asserts that the string|number target is within a range (inclusive).
var is = require('saguaro');
is(2, 'within(2, 3)') => true
is(1, 'within(2, 3)') => falseyear(value), year(min, max)
Asserts that the year of the date target is greater than or equal to value or within a range (inclusive).
var is = require('saguaro');
var data = new Date();
data.setFullYear(data.getFullYear() - 19);
is(data, 'year(19)') => true
is(data, 'year(18)') => true
is(data, 'year(18,21)') => true
is(data, 'year(21)') => falseLog
To display logs for debug purpose, run your script with NODE_DEBUG=saguaro in the environment.
NODE_DEBUG=saguaro node script.jsRun Tests
Tests are given complete coverage of all features.
$ npm testLicense
MIT
