n4s v5.0.28
Enforce
Enforce is a validation assertions library. It allows you to run your data against rules and conditions and test whether it passes your validations. It is intended for validation logic that gets repeated over and over again and should not be written manually. It comes with a wide-variety of pre-built rules, but it can also be extended to support your own repeated custom logic.
The way Enforce operates is similar to most common assertion libraries - if the validation fails, it throws an Error, and if the validation succeeds, it returns the current instance of Enforce to allow chaining more rules.
import enforce from 'n4s'
enforce(4).isNumber();
// passes
enforce(4).lessThan(2);
// throws an errorContent
- Chaining enforce rules
- Custom Enforce Rules
- Enforce rules
- equal
- notEqual
- isEmpty
- isNotEmpty
- isNumeric
- isNotNumeric
- greaterThan
- greaterThanOrEquals
- lengthEquals
- lengthNotEquals
- lessThan
- lessThanOrEquals
- longerThan
- longerThanOrEquals
- numberEquals
- numberNotEquals
- shorterThan
- shorterThanOrEquals
- matches
- notMatches
- inside
- notInside
- isTruthy
- isFalsy
- isArray
- isNotArray
- isNumber
- isNotNumber
- isString
- isNotString
- isOdd
- isEven
Chaining enforce rules
All of enforce's rules are chainable and can be used more than once in the same chain.
When chained, all blocks share an AND relationship, meaning that if one block fails, the whole test fails as well.
The following are valid uses of enforce.
enforce([1,2,3,4,5,6]).longerThan(5).isArray();
enforce('North Dakota, por favor').shorterThan(10).longerThan(6);
enforce('Toto, I\'ve a feeling we\'re not in Kansas anymore').notMatches(/0-9/);Enforce exposes all predefined and custom rules. You may use chaining to make multiple enfocements for the same value.
Custom enforce rules
To make it easier to reuse logic across your application, sometimes you would want to encapsulate bits of logic in rules that you can use later on, for example, "what's considered a valid email".
Your custom rules are essentially a single javascript object containing your rules.
const myCustomRules = {
isValidEmail: (value) => value.indexOf('@') > -1,
hasKey: (value, {key}) => value.hasOwnProperty(key),
passwordsMatch: (passConfirm, options) => passConfirm === options.passConfirm && options.passIsValid
}Just like the predefined rules, your custom rules can accepts two parameters:
valueThe actual value you are testing against.args(optional) the arguments which you pass on when running your tests.
You can extend enforce with your custom rules by creating a new instance of Enforce and adding the rules object as the argument.
import Enforce from 'n4s';
const myCustomRules = {
isValidEmail: (value) => value.indexOf('@') > -1,
hasKey: (value, key) => value.hasOwnProperty(key),
passwordsMatch: (passConfirm, options) => passConfirm === options.passConfirm && options.passIsValid
}
const enforce = new Enforce(myCustomRules);
enforce(user.email).isValidEmail();enforce rules
Enforce rules are functions that allow you to test your data against different criteria. The following rules are supported out-of-the-box
equals
Description
Checks if your enforced value strictly equals (===) another.
It is not recommended to use this rule to compare arrays or objects, as it does not perform any sort of deep comparison on the value.
For numeric value comparison, you should use numberEquals, which coerces numeric strings into numbers before comparing.
Arguments
value: Any value you wish to check your enforced value against
Usage examples:
Passing
enforce(1).equals(1);
enforce('hello').equals('hello');
const a = [1, 2, 3];
enforce(a).equals(a);failing
enforce('1').equals(1);
enforce([1, 2, 3]).equals([1, 2, 3]);notEquals
Description
Checks if your enforced value does not strictly equal (===) another.
Reverse implementation of equals.
Usage examples:
Passing
enforce('1').notEquals(1);
enforce([1, 2, 3]).notEquals([1, 2, 3]);failing
enforce(1).notEquals(1);
enforce('hello').notEquals('hello');
const a = [1, 2, 3];
enforce(a).notEquals(a);isEmpty
Description
Checks if your enforced value is empty, false, zero, null or undefined.
Expected results are:
- object: checks against count of keys (
0is empty) - array/string: checks against length. (
0is empty) - number: checks the value of the number. (
0andNaNare empty) - boolean:
falseis empty. - undefined/null: are both empty.
Arguments
| Name | Type | Required? | Description |
|---|---|---|---|
| expect | Boolean | No | when passed false, the negative result will be tested |
Usage examples:
enforce([]).isEmpty();
enforce('').isEmpty();
enforce({}).isEmpty();
enforce(0).isEmpty();
enforce(NaN).isEmpty();
enforce(undefined).isEmpty();
enforce(null).isEmpty();
enforce(false).isEmpty();
// passesenforce([1]).isEmpty();
enforce('1').isEmpty();
enforce({1:1}).isEmpty();
enforce(1).isEmpty();
enforce(true).isEmpty();
// throwsisNotEmpty
Description
Checks that your enforced value is not empty, false, or zero.
Reverse implementation of isEmpty.
Usage examples:
enforce([1]).isNotEmpty();
// passesenforce('1').isNotEmpty();
// passesenforce({1:1}).isNotEmpty();
// passesenforce([]).isNotEmpty();
// throwsenforce('').isNotEmpty();
// throwsenforce({}).isNotEmpty();
// throwsenforce(0).isNotEmpty();
// throwsisNumeric
Description
Checks if a value is a representation of a real number
Arguments
| Name | Type | Required? | Description |
|---|---|---|---|
| expect | Boolean | No | when passed false, the negative result will be tested |
Usage examples:
enforce(143).isNumeric();
enforce('143').isNumeric();
// passesenforce(NaN).isNumeric();
enforce('1hello').isNumeric();
enforce('hi').isNumeric();
// throwsisNotNumeric
Description
Checks if a value is not a representation of a real number.
Reverse implementation of isNumeric.
Usage examples:
enforce(NaN).isNotNumeric();
enforce('Hello World!').isNotNumeric();
// passesenforce(731).isNotNumeric();
enforce('42').isNotNumeric();
// throwsgreaterThan
- alias:
gt
Description
Checks that your numeric enforced value is larger than a given numeric value.
Arguments
value:number | string| A numeric value against which you want to check your enforced value.
Strings are parsed using Number(), values which are non fully numeric always return false;
Usage
Passing examples:
enforce(1).greaterThan(0);
enforce('10').greaterThan(0);
enforce(900).gt('100');Failing examples:
enforce(100).greaterThan(100);
enforce('100').greaterThan(110);
enforce([100]).gt(1);greaterThanOrEquals
- alias:
gte()
Description
Checks that your numeric enforced value is larger than or equals to a given numeric value.
Arguments
value:number | string| A numeric value against which you want to check your enforced value.
Strings are parsed using Number(), values which are non fully numeric always return false;
Usage
Passing examples:
enforce(1).greaterThanOrEquals(0);
enforce('10').greaterThanOrEquals(0);
enforce(900).greaterThanOrEquals('100');
enforce(100).greaterThanOrEquals('100');
enforce(900).gte('900');
enforce('1337').gte(1337);Failing examples:
enforce(100).greaterThanOrEquals('120');
enforce('100').greaterThanOrEquals(110);
enforce([100]).gte(1);lengthEquals
Description
Checks that your enforced value is equal to the given number.
Arguments
size:number| the number which you would like your initial value to be tested against.
The value argument can be of the following types:
- array: checks against length.
- string: checks against length.
Usage examples:
Passing examples:
enforce([1]).lengthEquals(1);
// passesenforce('a').lengthEquals(1);
// passesFailing examples:
enforce([1, 2]).lengthEquals(1);
// throwsenforce('').lengthEquals(1);
// throwslengthNotEquals
Description
Checks that your enforced value is not equal to the given number.
Reverse implementation of lengthEquals.
Arguments
size:number| the number which you would like your initial value to be tested against.
The value argument can be of the following types:
- array: checks against length.
- string: checks against length.
Usage examples:
Passing examples:
enforce([1]).lengthNotEquals(0);
// passesenforce('a').lengthNotEquals(3);
// passesFailing examples:
enforce([1]).lengthNotEquals(1);
// throwsenforce('').lengthNotEquals(0);
// throwslessThan
- alias:
lt()
Description
Checks that your numeric enforced value is smaller than a given numeric value.
Arguments
value:number | string| A numeric value against which you want to check your enforced value.
Strings are parsed using Number(), values which are non fully numeric always return false;
Usage
Passing examples:
enforce(0).lessThan(1);
enforce(2).lessThan('10');
enforce('90').lt(100);Failing examples:
enforce(100).lessThan(100);
enforce('110').lessThan(100);
enforce([0]).lt(1);lessThanOrEquals
- alias:
lte()
Description
Checks that your numeric enforced value is smaller than or equals to a given numeric value.
Arguments
value:number | string| A numeric value against which you want to check your enforced value.
Strings are parsed using Number(), values which are non fully numeric always return false;
Usage
Passing examples:
enforce(0).lessThanOrEquals(1);
enforce(2).lessThanOrEquals('10');
enforce('90').lte(100);
enforce(100).lte('100');Failing examples:
enforce(100).lessThanOrEquals(90);
enforce('110').lessThanOrEquals(100);
enforce([0]).lte(1);longerThan
Description
Checks that your enforced value is longer than a given number.
Arguments
size:number| the number which you would like your initial value to be tested against.
The value argument can be of the following types:
- array: checks against length.
- string: checks against length.
Usage examples:
Passing examples:
enforce([1]).longerThan(0);
// passesenforce('ab').longerThan(1);
// passesFailing examples:
enforce([1]).longerThan(2);
// throwsenforce('').longerThan(0);
// throwslongerThanOrEquals
Description
Checks that your enforced value is longer than or equals to a given number.
Arguments
size:number| the number which you would like your initial value to be tested against.
The value argument can be of the following types:
- array: checks against length.
- string: checks against length.
Usage examples:
Passing examples:
enforce([1]).longerThanOrEquals(0);
// passesenforce('ab').longerThanOrEquals(1);
// passesenforce([1]).longerThanOrEquals(1);
// passesenforce('a').longerThanOrEquals(1);
// passesFailing examples:
enforce([1]).longerThanOrEquals(2);
// throwsenforce('').longerThanOrEquals(1);
// throwsnumberEquals
Description
Checks that your numeric enforced value is equals another value.
Arguments
value:number | string| A numeric value against which you want to check your enforced value.
Strings are parsed using Number(), values which are non fully numeric always return false;
Usage
Passing examples:
enforce(0).numberEquals(0);
enforce(2).numberEquals('2');Failing examples:
enforce(100).numberEquals(10);
enforce('110').numberEquals(100);
enforce([0]).numberEquals(1);numberNotEquals
Description
Checks that your numeric enforced value does not equal another value.
Reverse implementation of numberEquals.
Arguments
value:number | string| A numeric value against which you want to check your enforced value.
Strings are parsed using Number(), values which are non fully numeric always return false;
Usage
Passing examples:
enforce(2).numberNotEquals(0);
enforce('11').numberNotEquals('10');Failing examples:
enforce(100).numberNotEquals(100);
enforce('110').numberNotEquals(100);shorterThan
Description
Checks that your enforced value is shorter than a given number.
Arguments
size:number| the number which you would like your initial value to be tested against.
The value argument can be of the following types:
- array: checks against length.
- string: checks against length.
Usage examples:
Passing examples:
enforce([]).shorterThan(1);
// passesenforce('a').shorterThan(2);
// passesFailing examples:
enforce([1]).shorterThan(0);
// throwsenforce('').shorterThan(0);
// throwsshorterThanOrEquals
Description
Checks that your enforced value is shorter than or equals to a given number.
Arguments
size:number| the number which you would like your initial value to be tested against.
The value argument can be of the following types:
- array: checks against length.
- string: checks against length.
Usage examples:
Passing examples:
enforce([]).shorterThanOrEquals(1);
// passesenforce('a').shorterThanOrEquals(2);
// passesenforce([]).shorterThanOrEquals(0);
// passesenforce('a').shorterThanOrEquals(1);
// passesFailing examples:
enforce([1]).shorterThanOrEquals(0);
// throwsenforce('ab').shorterThanOrEquals(1);
// throwsmatches
Description
Checks if a value contains a regex match.
Arguments
regexp: either aRegExpobject, or a RegExp valid string
Usage examples:
enforce(1984).matches(/[0-9]/);
// passesenforce(1984).matches('[0-9]');
// passesenforce('1984').matches(/[0-9]/);
// passesenforce('1984').matches('[0-9]');
// passesenforce('198four').matches(/[0-9]/);
// passesenforce('198four').matches('[0-9]');
// passesenforce('ninety eighty four').matches(/[0-9]/);
// throwsenforce('ninety eighty four').matches('[0-9]');
// throwsnotMatches
Description
Checks if a value does not contain a regex match.
Reverse implementation of matches.
Usage examples:
enforce(1984).notMatches(/[0-9]/);
// throwsenforce('ninety eighty four').notMatches('[0-9]');
// passesinside
Description
Checks if your enforced value is contained in another array or string. Your enforced value can be of the following types:
stringnumberboolean
Arguments
container: astringor anarraywhich may contain the value specified.
Usage examples:
inside: array
Checks for membership in an array.
- string: checks if a string is an element in an array
enforce('hello').inside(['hello', 'world']);
// passesenforce('hello!').inside(['hello', 'world']);
// throws- number: checks if a number is an element in an array
enforce(1).inside([1, 2]);
// passesenforce(3).inside([1, 2]);
// throws- boolean: checks if a number is an element in an array
enforce(false).inside([true, false]);
// passesenforce(true).inside([1,2,3]);
// throwsinside: string
- string: checks if a string is inside another string
enforce('da').inside('tru dat.');
// passesenforce('ad').inside('tru dat.');
// throwsnotInside
Description
Checks if a given value is not contained in another array or string.
Reverse implementation of inside.
Usage examples:
enforce('ad').notInside('tru dat.');
enforce('hello!').notInside(['hello', 'world']);
// passesenforce('hello').notInside(['hello', 'world']);
enforce('da').notInside('tru dat.');
// throwsisTruthy
Description
Checks if a value is truthy; Meaning: if it can be coerced into boolean true.
Anything not in the following list is considered to be truthy.
undefinednullfalse0NaN- empty string (
"")
Arguments
| Name | Type | Required? | Description |
|---|---|---|---|
| expect | Boolean | No | when passed false, the negative result will be tested |
Usage examples:
enforce("hello").isTruthy();
enforce(true).isTruthy();
enforce(1).isTruthy();
// passesenforce(false).isTruthy();
enforce(null).isTruthy();
enforce(undefined).isTruthy();
enforce(0).isTruthy();
enforce(NaN).isTruthy();
enforce("").isTruthy();
// throwsisFalsy
Description
Checks if a value is falsy; Meaning: if it can be coerced into boolean false.
Reverse implementation of isTruthy.
Anything not in the following list is considered to be truthy:
undefinednullfalse0NaN- empty string (
"")
Usage examples:
enforce(1).isFalsy();
// throwsenforce(true).isFalsy();
// throwsenforce('hi').isFalsy();
// throwsenforce(false).isFalsy();
// passesenforce(0).isFalsy();
// passesenforce(undefined).isFalsy();
// passesisArray
Description
Checks if a value is of type Array.
Arguments
| Name | Type | Required? | Description |
|---|---|---|---|
| expect | Boolean | No | when passed false, the negative result will be tested |
Usage examples:
enforce(['hello']).isArray();
// passesenforce('hello').isArray();
// throwsisNotArray
Description
Checks if a value is of any type other than Array.
Reverse implementation of isArray.
Usage examples:
enforce(['hello']).isNotArray();
// throwsenforce('hello').isNotArray();
// passesisNumber
Description
Checks if a value is of type number.
Arguments
| Name | Type | Required? | Description |
|---|---|---|---|
| expect | Boolean | No | when passed false, the negative result will be tested |
Usage examples:
enforce(143).isNumber();
enforce(NaN).isNumber(); // (NaN is of type 'number!')
// passesenforce([]).isNumber();
enforce("143").isNumber();
// throwsisNotNumber
Description
Checks if a value is of any type other than number.
Reverse implementation of isNumber.
Usage examples:
enforce(143).isNotNumber();
// throwsenforce(NaN).isNotNumber();
// throws (NaN is of type 'number!')enforce('143').isNotNumber();
// passesenforce(143).isNotNumber();
// passesisString
Description
Checks if a value is of type String.
Arguments
| Name | Type | Required? | Description |
|---|---|---|---|
| expect | Boolean | No | when passed false, the negative result will be tested |
Usage examples:
enforce('hello').isString();
// passesenforce(['hello']).isString();
enforce(1984).isString();
// throwsisNotString
Description
Checks if a value is of any type other than String.
Reverse implementation of isString.
Usage examples:
enforce('hello').isNotString();
// throwsenforce(['hello']).isNotString();
// passesisOdd
Description
Checks if a value is an odd numeric value.
Usage examples:
enforce('1').isOdd();
enforce(9).isOdd();
// passesenforce(2).isOdd();
enforce('4').isOdd();
enforce('1withNumber').isOdd();
enforce([1]).isOdd();
// throwsisEven
Description
Checks if a value is an even numeric value.
Usage examples:
enforce(0).isEven();
enforce('2').isEven();
// passesenforce(1).isEven();
enforce('3').isEven();
enforce('2withNumber').isEven();
enforce([0]).isEven();
// throws11 months ago
9 months ago
12 months ago
12 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago