fluent-assert
fluent-assert is meant to be a better way of specifying your contract signatures, allowing you to fluently make your assertions for not only types, but different properties of those types.
Getting Started (Required Node >=4)
First install through npm
npm install --save fluent-assert
And then require it
var assert = require('fluent-assert');
Examples
The Basics
All of fluent-asserts entry point functions (as described below) take two arguments: name and value. The only odd man out is assert.custom
fluent-assert has the most of the familiar contract assertions found in other libraries, including:
assert.string(...)
assert.number(...)
assert.bool(...)
assert.object(...)
...
fluent-assert has other type contracts such as:
assert.array(...) // Only checks to make sure value is an array
assert.ok(...) // Asserts value is not null or undefined
assert.custom(name, value, predicate) // Checks if value passes the predicate function
...
Assert
Base assertions not tied to a specific type.
#ok(name, value)
Validates your value is not null
or undefined
ensuring your value is safe to operate on.
assert; // Safeassert; // AssertionError: myVar should not be undefined or nullassert; // AssertionError: myVar should not be undefined or null
#defined(name, value)
Validates your value is defined (including null). Useful for allowing null values as defaults for third party libraries,
assert; // Safeassert; // Safeassert; // AssertionError: myVar should not be undefined
#custom(name, value, predicate)
Validates your value matches the given predicate. Useful when custom logic is needed specific to your own app not included in fluent-assert by default. The predicate function takes the value as an argument to minimize closures and leaves the function open for any extra checking that may be done by fluent-assert
///Arrow Functions used for brevityassert; // Safe, and contrivedassert; // AssertionError: predicate should be of type functionassert; // AssertionError: myVar should match predicate
#optional()
Allows all type assertions (not including those mentioned above) to allow undefined values. The optional flag will be applied throughout the entire assertion chain, so you can still make an assertion without the value being present.
assert; // Safe
Note Once the optional function has been called, the chain will no longer have an optional function, so something like assert.optional().optional()
will result in an error.
String
The string assertion utility has some useful functions for validating the contract on string parameters.
assert;
#matches(regexp: RegExp)
Validates your value against the supplied regular expression. Throws an assertion error if value does not match or if the argument for matches is not a regular expression.
assert; // Safeassert; // AssertionError: myVar should match pattern /banana/assert // AssertionError: parameter regexp should be of type RegExp
#notEmpty()
Validates your value is not an empty string. Throws an assertion error is string is empty. Only works for the empty string. Use #notWhiteSpace
for all other "empty" string values
assert; // Safe`assert; // AssertionError: myVar should be a non empty string
#notWhiteSpace()
Validates your value is not only white space characters as dictated by JavaScripts RegExp. Will also validate value is not an empty string.
assert; // Safeassert; // Safeassert; // AssertionError: myVar should be a non white space only string
#uuid() (also guid())
Validates your string is a UUID/GUID. Throws an assertion error otherwise.
assert // Safeassert // Safeassert // AssertionError: myVar should be a UUID
Number
The number assertion helps you validate certain properties against numbers.
assert;
#min(min)
Validates your value is equal to or above the min argument. Throws an assertion error if value is below min.
assert; // Safeassert; // Safeassert; // AssertionError: myVar should be greater than 11
#max(max)
Validates your value is equal to or below the max argument. Throws an assertion error if value is above max.
assert; // Safeassert; // Safeassert; // AssertionError: myVar should be less than 5
#range(lower, upper)
Validates your value is within the specified range of [lower] and [upper]. Throws an assertion error if value is outside the range.
assert; // Safeassert; // Safeassert; // Safeassert; // AssertionError: myVar should be between [lower] and [upper]assert; // AssertionError: myVar should be between 1 and 9
#even()
Validates your value is an even number. Throws an assertion error if value is odd.
assert; // Safeassert; // AssertionError: myVar should be even
#odd()
Validates your value is an odd number. Throws an assertion error if value is even.
assert; // Safeassert; // AssertionError: myVar should be odd
#equal(compare)
Validates your value is the same as [compare]. Throws an assertion error is value is not equal to [compare].
assert; //Safeassert; // AssertionError: myVar should be equal to 0
#in(values)
Validates your value is within an array of given values. Throws an assertion error is value is not within the array.
assert; // Safeassert; // AssertionError: myVar should be in [1, 3, 5, 7, 9]
#finite()
Validates your value is a number of a finite value. Throws an assertion error otherwise
assert; // Safeassert // AssertionError: myVar should be a finite numberassert // AssertionError: myVar should be a finite numberassert // AssertionError: myVar should be a finite number
#positive()
Validates your value is a positive number. Throws an assertion error if your value is <= 0
assert // Safeassert // AssertionError: myVar should be a positive numberassert // AssertionError: myVar should be a positive number
#integer()
Validate your value is an integer. Throws an assertion error if your value is a float
assert // Safeassert // Safeassert // AssertionError: myVar should be an float
#float()
Validate your value is a float. Throws an assertion error if your value is evaluated as a whole number
assert // Safeassert // AssertionError: myVar should be a floatassert // AssertionError: myVar should be a float
#negative()
Validates your value is a negative number. Throws an assertion error if your value is >= 0
assert // Safeassert // AssertionError: myVar should be a negative numberassert // AssertionError: myVar should be a negative number
Boolean
Since boolean values are so simple, there are no use cases for assertion chains on booleans.
assert; // Safeassert; // Safeassert; // AssertionError: myVar should be of type boolean
Buffer
I'm not that keen on how Buffers work, so for now, there isn't any chaining functionality for Buffers
assert;
Object
The object assertion helps you validate certain properties against objects.
assertobject'myVar' {};
#hasMember(member)
Validates your value has the specified [member]. Throws an assertion error if value does not have [member]
assertobject'myVar' greeting: 'Hello'; // Safeassertobject'myVar' {}; // AssertionError: Expected myVar to have member named greeting
#instanceOf(type)
Validates your value is an instance of [type] using the instanceof
operator. Throws an assertion error is value is not an instance of [type]
assertobject'myVar' Object'test'; // Safeassertobject'myVar' Object'test'; // AssertionError: myVar should be an instance of function Number() { ... }
Array
The array assertion help you validate certain properties against array and its elements.
assert;
#of(type)
Validates your array is a consistently typed array of [type]
assert; // Safeassert; // Safeassert; // AssertionError: myVar should be all of type numberassert; // AssertionError: myVar should be all of type Number
#contains(value)
Validates your array contains some value. Useful for checking required values passed as an array
assert; // Safeassert; // AssertionError: myVar should contain 4
Function
Functions are another of those simple types in js, so currently no useful assertions can be made of functions
assert;assert;
Date
The date assertion helps you validate certain properties against js dates.
assert;
#before(date)
Validates your date occurs before [date]
assert; // Safeassert; // AssertionError: myVar should occur before ... Dec 31 1969 ...
#after(date)
Validates your date occurs after [date]
assert; // Safeassert; // AssertionError myVar should occur after ... Jan 02 1970 ...
#within(lower, upper)
Validates your date occurs between [lower] and [upper]
assert; // Safeassert; // Safeassert; // Safeassert; // AssertionError: myVar should be within ... Jan 02 1970 ... and ... Jan 04 1970 ...assert; // AssertionError: myVar should be within ... Dec 29 1969 ... and ... Dec 31 1960 ...
#dayOf(day)
Validates your date occurs on the day of [day]
assert; // Safeassert; // AssertionError: myVar should occur on day 2
#monthOf(month)
Validates your date occurs on the month of [month]
assert; // Safeassert; // Safeassert; // Safeassert; // AssertionError: myVar should occur on month 1assert; // AssertionError: myVar should occur on month Febassert; // AssertionError: myVar should occur on month February
#yearOf(year)
Validates your date occurs on the year of [year]
assert; // Safeassert; // AssertionError: myVar should occur on year 1969