Assert
An assertion library with user-friendly error messages
Features
-
When an assertion succeeds, the value is returned. Useful for assertion + assignment.
-
Throws appropriate error types for each assertion (e.g. TypeError
, RangeError
, etc.)
-
Error messages include the invalid value, humanized and sanitized
-
You can customize the field name used in error messages
Installation
You can install this library via npm.
npm install @jsdevtools/assert
Usage
The exported assert
function supports fluent-like chaining with various assertion functions. Each of the assertion functions returns the value if valid, or throws an error if invalid.
assert.value(value, [fieldName], [defaultValue])
Asserts that a value that is not undefined
. Any other value will pass, even null
and NaN
.
- value - The value to check
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.value(0);
assert.value(false);
assert.value(null);
assert.value(NaN);
assert.value(undefined);
assert.value.oneOf(value, values, [fieldName], [defaultValue])
Assets that a value that is one of the specified values.
- value - The value to check
- values - The allowed values
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.value.oneOf("a", ["a", "b", "c"]);
assert.value.oneOf(4, [1, 2, 3, 4, 5]);
assert.value.oneOf(true, [1, true, "yes"]);
assert.value.oneOf("b", ["x", "y", "z"]);
assert.type(value, type, [fieldName], [defaultValue])
Asserts that a value is the specified type.
- value - The value to check
- type - The expected type. This can be a class, a primitive wrapper (e.g.
String
), null
, or undefined
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.type("John", String);
assert.type(42, Number);
assert.type(false, Boolean);
assert.type(null, null);
assert.type({ x: 1 }, Object);
assert.type(/^regex$/, RegExp);
assert.type(new Date(), Date);
assert.type("Fred", Object);
assert.type(100, BigInt);
assert.type(undefined, null);
assert.type(null, Object);
assert.type(new Date(), RangeError);
assert.type.oneOf(value, types, [fieldName], [defaultValue])
Asserts that a value is one of the specified types.
- value - The value to check
- types - An array of the allowed types.
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.type.oneOf("John", [Number, String, Boolean]);
assert.type.oneOf(42, [Number, BigInt, Date]);
assert.type.oneOf(null, [null, undefined]);
assert.type.oneOf(new RangeError(), [TypeError, RangeError]);
assert.type.oneOf("Fred", [Number, Boolean, Object]);
assert.type.oneOf(undefined, [Boolean, Number, null]);
assert.type.oneOf(new SyntaxError(), [TypeError, RangeError]);
assert.type.string(value, [fieldName], [defaultValue])
Asserts that a value is a primitive string (including empty strings).
- value - The value to check
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.type.string("John");
assert.type.string("");
assert.type.string(" ");
assert.type.string("\n");
assert.type.string("\t");
assert.type.string(123);
assert.type.string(null);
assert.type.string(new String());
assert.type.number(value, [fieldName], [defaultValue])
Asserts that a value is a primitive number. NaN
is not considered a number.
- value - The value to check
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.type.number(0)
assert.type.number(123)
assert.type.number(-42.1245)
assert.type.number(Math.PI)
assert.type.number(Infinity)
assert.type.number("123");
assert.type.number(NaN);
assert.type.number(new Number());
assert.type.boolean(value, [fieldName], [defaultValue])
Asserts that a value is a primitive boolean value.
- value - The value to check
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.type.boolean(false)
assert.type.boolean(true)
assert.type.boolean("true");
assert.type.boolean(0);
assert.type.boolean(1);
assert.type.boolean(new Boolean());
assert.type.object(value, [fieldName], [defaultValue])
Asserts that a value is an object. null
is not considered an object.
- value - The value to check
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.type.object({})
assert.type.object(/^regex$/)
assert.type.object(new Date())
assert.type.object(new Object())
assert.type.object(Object.prototype)
assert.type.object(null);
assert.type.object(undefined);
assert.type.object(Object);
assert.type.array(value, [fieldName], [defaultValue])
Asserts that a value is an array.
- value - The value to check
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.type.array([])
assert.type.array([1, 2, 3])
assert.type.array(new Array())
assert.type.array(Array.prototype)
assert.type.array("hello");
assert.type.array(Array);
assert.type.function(value, [fieldName], [defaultValue])
Asserts any type of function, including async, generators, arrow functions, classes, etc.
- value - The value to check
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.type.function(Object)
assert.type.function(Object.toString)
assert.type.function(function foo() {})
assert.type.function(() => null)
assert.type.function(class Foo {})
assert.type.function(null);
assert.type.function(new Object());
assert.type.function("function");
assert.string(value, [fieldName], [defaultValue])
This is an alias for assert.type.string()
assert.string.nonEmpty(value, [fieldName], [defaultValue])
Asserts that a value is a string with at least one character (including whitespace).
- value - The value to check
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.string.nonEmpty("John")
assert.string.nonEmpty(" ")
assert.string.nonEmpty("\n")
assert.string.nonEmpty("\t")
assert.string.nonEmpty("");
assert.string.nonEmpty(null);
assert.string.nonEmpty(new String());
assert.string.nonWhitespace(value, [fieldName], [defaultValue])
Asserts that a value is a string with at least one non-whitespace character.
- value - The value to check
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.string.nonWhitespace("John")
assert.string.nonWhitespace(" a ")
assert.string.nonWhitespace("");
assert.string.nonWhitespace(" ");
assert.string.nonWhitespace("\n")
assert.string.nonWhitespace("\t")
assert.string.nonWhitespace(new String());
assert.string.minLength(value, minLength, [fieldName], [defaultValue])
Asserts that a value is a string with at least the specified number of characters (including whitespace).
- value - The value to check
- minLength - The minimum allowed length
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.string.minLength("John", 1)
assert.string.minLength(" a ", 5)
assert.string.minLength("", 0)
assert.string.minLength("", 1);
assert.string.minLength("John", 10);
assert.string.maxLength(value, maxLength, [fieldName], [defaultValue])
Asserts that a value is a string with no more than the specified number of characters (including whitespace).
- value - The value to check
- maxLength - The maximum allowed length
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.string.maxLength("John", 10)
assert.string.maxLength(" a ", 5)
assert.string.maxLength("", 50)
assert.string.maxLength("John Doe", 5);
assert.string.length(value, minLength, [maxLength], [fieldName], [defaultValue])
Asserts that a value is a string with the specified number of characters (including whitespace)
- value - The value to check
- minLength - The minimum allowed length
- maxLength - (optional) The maximum allowed length. If not specified, it defaults to the same as
minLength
. - fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.string.length("John", 1, 10)
assert.string.length(" a ", 5, 25)
assert.string.length("", 0, 100)
assert.string.length("John Doe", 1, 5);
assert.string.length("John Doe", 20, 50);
assert.string.length("John Doe", 5);
assert.number(value, [fieldName], [defaultValue])
This is an alias for assert.type.number()
assert.number.integer(value, [fieldName], [defaultValue])
Asserts that a value is an integer value (positive or negative).
- value - The value to check
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.number.integer(0);
assert.number.integer(42);
assert.number.integer(-42);
assert.number.integer(12345.0);
assert.number.integer(Math.PI);
assert.number.integer(Infinity);
assert.number.integer(NaN);
assert.number.integer.positive(value, [fieldName], [defaultValue])
Asserts that a value is a positive integer value (one or more).
- value - The value to check
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.number.integer.positive(42);
assert.number.integer.positive(12345.0);
assert.number.integer.positive(0);
assert.number.integer.positive(-42);
assert.number.integer.positive(Infinity);
assert.number.integer.positive(NaN);
assert.number.integer.nonNegative(value, [fieldName], [defaultValue])
Asserts that a value is an integer value that is zero or greater.
- value - The value to check
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.number.integer.nonNegative(0);
assert.number.integer.nonNegative(42);
assert.number.integer.nonNegative(12345.0);
assert.number.integer.nonNegative(-42);
assert.number.integer.nonNegative(-Infinity);
assert.number.integer.nonNegative(NaN);
assert.array(value, [fieldName], [defaultValue])
This is an alias for assert.type.array()
assert.array.nonEmpty(value, [fieldName], [defaultValue])
Asserts that a value is an array with at least one item.
- value - The value to check
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.array.nonEmpty(["a", "b", "c"]);
assert.array.nonEmpty(new Array(1, 2, 3));
assert.array.nonEmpty([]);
assert.array.nonEmpty(new Array());
assert.array.minLength(value, minLength, [fieldName], [defaultValue])
Asserts that a value is an array with at least the specified number of items.
- value - The value to check
- minLength - The minimum allowed length
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.array.minLength([], 0);
assert.array.minLength(["a", "b", "c"], 1);
assert.array.minLength([], 1);
assert.array.minLength(["a", "b", "c"], 10);
assert.array.maxLength(value, maxLength, [fieldName], [defaultValue])
Asserts that a value is an array with no more than the specified number of items.
- value - The value to check
- maxLength - The maximum allowed length
- fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.array.maxLength([], 5);
assert.array.maxLength(["a", "b", "c"], 3);
assert.array.maxLength([1, 2, 3], 1);
assert.array.maxLength(["a", "b", "c"], 2);
assert.array.length(value, minLength, [maxLength], [fieldName], [defaultValue])
Asserts that a value is an array with the specified number of items.
- value - The value to check
- minLength - The minimum allowed length
- maxLength - (optional) The maximum allowed length. If not specified, it defaults to the same as
minLength
. - fieldName - (optional) The name of the field being assertd. This is used in the error message if the assertion fails.
- defaultValue - (optional) The default value to use if
value
is undefined
.
import assert from "@jsdevtools/assert";
assert.array.length([], 0);
assert.array.length([], 0, 5);
assert.array.length(["a", "b", "c"], 3);
assert.array.length(["a", "b", "c"], 1, 3);
assert.array.length([], 1);
assert.array.length([1, 2, 3], 5, 10);
assert.array.length(["a", "b", "c"], 2);
Contributing
Contributions, enhancements, and bug-fixes are welcome! File an issue on GitHub and submit a pull request.
Building
To build the project locally on your computer:
-
Clone this repo
git clone https://github.com/JS-DevTools/assert.git
-
Install dependencies
npm install
-
Build the code
npm run build
-
Run the tests
npm test
License
@jsdevtools/assert is 100% free and open-source, under the MIT license. Use it however you want.
This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
Big Thanks To
Thanks to these awesome companies for their support of Open Source developers ❤