A simple framework for data validation.
npm install saguaro
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]) => true
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') => false
Negation of any assertion is supported by the !
char.
var is = require('saguaro');
is({}, '!number') => true
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)') => false
Asserts that the string|number target contains alphanumerical characters ([0-9a-zA-Z]
).
var is = require('saguaro');
is('123Az', 'alphanum') => true
is('123Az@', 'alphanum') => false
Asserts that the target is an Arguments
object.
var is = require('saguaro');
(function () {
is(arguments, 'arguments') => true
is({}, 'arguments') => false
})();
Asserts that the target is an Array
.
var is = require('saguaro');
is([], 'array') => true
is({}, 'array') => false
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)') => false
Asserts that the target is a Boolean
.
var is = require('saguaro');
is(true, 'boolean') => true
is(1, 'boolean') => false
Asserts that the target is a Buffer
.
var is = require('saguaro');
is(new Buffer(1234), 'buffer') => true
is({}, 'buffer') => false
Asserts that the string target contains another string.
var is = require('saguaro');
is('- foobar -', 'contain(foobar)') => true
is('- foobar -', 'contain(foobar1)') => false
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') => false
Asserts that the target is a Date
.
var is = require('saguaro');
is(new Date(), 'date') => true
is(1, 'date') => false
Asserts that the string target is an email.
var is = require('saguaro');
is('johndoe@example.com', 'email') => true
is('johndoe-example.com', 'email') => false
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') => false
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)') => false
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') => false
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') => false
Asserts that the number target is a Float
.
var is = require('saguaro');
is(1.01, 'float') => true
is(1, 'float') => false
Asserts that the target is a Function
.
var is = require('saguaro');
is(function () {}, 'function') => true
is(1, 'function') => false
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') => false
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)') => false
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)') => false
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)') => false
Asserts that the target is an Integer
.
var is = require('saguaro');
is(1, 'integer') => true
is(1.01, 'integer') => false
Asserts that the target is an ip.
var is = require('saguaro');
is('255.255.255.255', 'ip') => true
is('256.255.255.255', 'ip') => false
Asserts that the target is a JSON notation.
var is = require('saguaro');
is('{ "foo": "bar" }', 'json') => true
is('foobar', 'json') => false
Asserts that the object target has the key value
.
var is = require('saguaro');
is({ 'foo': 'bar' }, 'key(foo)') => true
is({}, 'key(toString)') => false
Asserts that the number|string target is a latitude value.
var is = require('saguaro');
is(37.4418834, 'lat') => true
is(90.01, 'lat') => false
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)') => false
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)') => false
Asserts that the number|string target is a longitude value.
var is = require('saguaro');
is(-122.1430195, 'lng') => true
is(180.01, 'lng') => false
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)') => false
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)') => false
Asserts that the target is an illegal number (NaN
).
var is = require('saguaro');
is(0 / 0, 'NaN') => true
is(1, 'NaN') => false
Asserts that the number target is a negative Number
.
var is = require('saguaro');
is(-1, 'negative') => true
is(0, 'negative') => false
Asserts that the target is a Number
.
var is = require('saguaro');
is(123, 'number') => true
is('foobar', 'number') => false
Asserts that the target is an Object
.
var is = require('saguaro');
is({}, 'object') => true
is([], 'object') => false
Asserts that the number target is a positive Number
.
var is = require('saguaro');
is(1, 'positive') => true
is(0, 'positive') => false
Asserts that the target is a regular expression.
var is = require('saguaro');
is(/foobar/, 'regexp') => true
is(1, 'regexp') => false
Asserts that the target is a String
.
var is = require('saguaro');
is('foobar', 'string') => true
is(1, 'string') => false
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') => false
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)') => false
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)') => false
To display logs for debug purpose, run your script with NODE_DEBUG=saguaro
in the environment.
NODE_DEBUG=saguaro node script.js
Tests are given complete coverage of all features.
$ npm test
MIT