assert-utils
Assertion tool that deeply compares value to its definition.
Install
$ yarn add assert-utils
Usage
const assertUtils = assert = assertUtils; { asserttypemyText 'string';} ; // valid, outputs nothing; // invalid, throws an error with message 'Assertion error: expected "string" but got "number"'
API
assert.value(value, expectation, strict)
Checks whether the value
equals to the expectation
. Throws error if not.
value | any
expectation | any
strict | boolean
optional
Default is false
.
strict | boolean
optional
Default is false
.
assertvalue'1' + 0 '10'; // validassertvalue'1' + 0 10; // validassertvalue'1' + 0 10 true; // invalidassertvalue'1' + 0 1; // invalid
assert.type(value, definition)
Deeply checks whether the whole value
equals to its definition
. If the definition
is an object, it recursively checks all its properties.
value | any
definition | any
definition
basically accepts the same value as returned by built in typeof
operator. In addition, there are some shortcuts:
'[]'
forarray
'{}'
or{}
forobject
Example
let value = number: 200 string: 'this is string' {} object: stringProperty: 'also string' ; asserttypevalue number: 'number' string: 'string' function: 'function' object: stringProperty: 'string' ;
Optional properties
If you want to mark any definition optional, just add a ?
before the first letter.
asserttype'this is string' '?string'; // validasserttypeundefined '?string'; // also validasserttypeundefined 'string'; // invalid
In case of optional object definition you can use special property _required: true
. Default is true
.
// validasserttype string: 'this is string' string: 'string' // also validasserttypeundefined _required: false string: 'string' // invalidasserttypeundefined string: 'string'
Multiple possible types
In case you expect multiple type, just divide them by |
.
// validasserttype stringOrNumber: 2 stringOrNumber: 'string|number' // also validasserttype stringOrNumber: 'this is string' stringOrNumber: '?string|number' // invalid - "?" has to be firstasserttype stringOrNumber: 'this is string' stringOrNumber: 'string|?number'
Equal properties
By default only the properties included in the definition
object are being checked. If the tested object has some additional properties, it just passes them as valid. If you want to check there are no additional properties beside those in the definition
, use a special property _strict: true
. Default is false
:
// validasserttype string: 'this is string' string: 'string' // also validasserttype string: 'this is string' number: 2 string: 'string' // invalidasserttype string: 'this is string' number: 2 _strict: true string: 'string'
Specific value instead of type
In case you want to mix up type and value checks, use a special property _value
. Of cource you can use it along with _strict
property:
// validasserttype1 _value: 1 // validasserttype1 _value: '1' // invalidasserttype1 _value: 'number' // invalidasserttype1 _value: '1' _strict: true
Testing
Tests are using AVA library
$ yarn test // run all tests$ yarn test -- -m 'Test title RegExp' // run test with matching title