List of 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
- notEquals
- isEmpty
- isNotEmpty
- isNumeric
- isNotNumeric
- greaterThan
- greaterThanOrEquals
- lengthEquals
- lengthNotEquals
- lessThan
- lessThanOrEquals
- longerThan
- longerThanOrEquals
- numberEquals
- numberNotEquals
- shorterThan
- shorterThanOrEquals
- matches
- notMatches
- inside
- notInside
- isTruthy
- isFalsy
- isArray
- isNotArray
- isBoolean
- isNotBoolean
- isBlank
- isNotBlank
- isNumber
- isNotNumber
- isNaN
- isNotNaN
- isNull
- isNotNull
- isNullish
- isNotNullish
- isString
- isNotString
- isUndefined
- isOdd
- isEven
- isBetween
- isNotBetween
- endsWith
- doesNotEndWith
- startsWith
- doesNotStartWith
- isNegative
- isPositive
- isValueOf
- isNotValueOf
- isKeyOf
- isNotKeyOf
- isEmail
- isDate
- isAfter
- isBefore
- isISO8601
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:​
enforce(1).equals(1);
enforce('hello').equals('hello');
const a = [1, 2, 3];
enforce(a).equals(a);
// passes
enforce('1').equals(1);
enforce([1, 2, 3]).equals([1, 2, 3]);
// throws
notEquals​
Description​
Checks if your enforced value does not strictly equal (===) another.
Reverse implementation of equals.
Usage examples:​
enforce('1').notEquals(1);
enforce([1, 2, 3]).notEquals([1, 2, 3]);
// passes
enforce(1).notEquals(1);
enforce('hello').notEquals('hello');
const a = [1, 2, 3];
enforce(a).notEquals(a);
// throws
isEmpty​
Description​
Checks if your enforced value is empty, false, zero, null or undefined.
Expected results are:
- object: checks against the 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.
Usage examples:​
enforce([]).isEmpty();
enforce('').isEmpty();
enforce({}).isEmpty();
enforce(0).isEmpty();
enforce(NaN).isEmpty();
enforce(undefined).isEmpty();
enforce(null).isEmpty();
enforce(false).isEmpty();
// passes
enforce([1]).isEmpty();
enforce('1').isEmpty();
enforce({ 1: 1 }).isEmpty();
enforce(1).isEmpty();
enforce(true).isEmpty();
// throws
isNotEmpty​
Description​
Checks that your enforced value is not empty, false, or zero.
Reverse implementation of isEmpty.
Usage examples:​
enforce([1]).isNotEmpty();
enforce('1').isNotEmpty();
enforce({ 1: 1 }).isNotEmpty();
// passes
enforce([]).isNotEmpty();
enforce('').isNotEmpty();
enforce({}).isNotEmpty();
enforce(0).isNotEmpty();
// throws
isNumeric​
Description​
Checks if a value is a representation of a real number
Usage examples:​
enforce(143).isNumeric();
enforce('143').isNumeric();
// passes
enforce(NaN).isNumeric();
enforce('1hello').isNumeric();
enforce('hi').isNumeric();
// throws
isNotNumeric​
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();
// passes
enforce(731).isNotNumeric();
enforce('42').isNotNumeric();
// throws
greaterThan​
- 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 not fully numeric always return false;
Usage​
enforce(1).greaterThan(0);
enforce('10').greaterThan(0);
enforce(900).gt('100');
// passes
enforce(100).greaterThan(100);
enforce('100').greaterThan(110);
enforce([100]).gt(1);
// throws
greaterThanOrEquals​
- alias:
gte()
Description​
Checks that your numeric enforced value is larger than or equals 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 not fully numeric always return false;
Usage​
enforce(1).greaterThanOrEquals(0);
enforce('10').greaterThanOrEquals(0);
enforce(900).greaterThanOrEquals('100');
enforce(100).greaterThanOrEquals('100');
enforce(900).gte('900');
enforce('1337').gte(1337);
// passes
enforce(100).greaterThanOrEquals('120');
enforce('100').greaterThanOrEquals(110);
enforce([100]).gte(1);
// throws
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:​
enforce([1]).lengthEquals(1);
enforce('a').lengthEquals(1);
// passes
enforce([1, 2]).lengthEquals(1);
enforce('').lengthEquals(1);
// throws
lengthNotEquals​
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:​
enforce([1]).lengthNotEquals(0);
enforce('a').lengthNotEquals(3);
// passes
enforce([1]).lengthNotEquals(1);
enforce('').lengthNotEquals(0);
// throws
lessThan​
- 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 not fully numeric always return false;
Usage​
enforce(0).lessThan(1);
enforce(2).lessThan('10');
enforce('90').lt(100);
// passes
enforce(100).lessThan(100);
enforce('110').lessThan(100);
enforce([0]).lt(1);
// throws
lessThanOrEquals​
- alias:
lte()
Description​
Checks that your numeric enforced value is smaller than or equals 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 not fully numeric always return false;
Usage​
enforce(0).lessThanOrEquals(1);
enforce(2).lessThanOrEquals('10');
enforce('90').lte(100);
enforce(100).lte('100');
// passes
enforce(100).lessThanOrEquals(90);
enforce('110').lessThanOrEquals(100);
enforce([0]).lte(1);
// throws
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:​
enforce([1]).longerThan(0);
enforce('ab').longerThan(1);
// passes
enforce([1]).longerThan(2);
enforce('').longerThan(0);
// throws
longerThanOrEquals​
Description​
Checks that your enforced value is longer than or equals 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:​
enforce([1]).longerThanOrEquals(0);
enforce('ab').longerThanOrEquals(1);
enforce([1]).longerThanOrEquals(1);
enforce('a').longerThanOrEquals(1);
// passes
enforce([1]).longerThanOrEquals(2);
enforce('').longerThanOrEquals(1);
// throws
numberEquals​
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 not fully numeric always return false;
Usage​
enforce(0).numberEquals(0);
enforce(2).numberEquals('2');
// passes
enforce(100).numberEquals(10);
enforce('110').numberEquals(100);
enforce([0]).numberEquals(1);
// throws
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 not fully numeric always return false;
Usage​
enforce(2).numberNotEquals(0);
enforce('11').numberNotEquals('10');
// passes
enforce(100).numberNotEquals(100);
enforce('110').numberNotEquals(100);
// throws
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:​
enforce([]).shorterThan(1);
enforce('a').shorterThan(2);
// passes
enforce([1]).shorterThan(0);
enforce('').shorterThan(0);
// throws
shorterThanOrEquals​
Description​
Checks that your enforced value is shorter than or equals 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:​
enforce([]).shorterThanOrEquals(1);
enforce('a').shorterThanOrEquals(2);
enforce([]).shorterThanOrEquals(0);
enforce('a').shorterThanOrEquals(1);
// passes
enforce([1]).shorterThanOrEquals(0);
enforce('ab').shorterThanOrEquals(1);
// throws
matches​
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]/);
enforce(1984).matches('[0-9]');
enforce('1984').matches(/[0-9]/);
enforce('1984').matches('[0-9]');
enforce('198four').matches(/[0-9]/);
enforce('198four').matches('[0-9]');
// passes
enforce('ninety eighty four').matches(/[0-9]/);
enforce('ninety eighty four').matches('[0-9]');
// throws
notMatches​
Description​
Checks if a value does not contain a regex match.
Reverse implementation of matches.
Usage examples:​
enforce(1984).notMatches(/[0-9]/);
// throws
enforce('ninety eighty four').notMatches('[0-9]');
// passes
inside​
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']);
// passes
enforce('hello!').inside(['hello', 'world']);
// throws
- number: checks if a number is an element in an array
enforce(1).inside([1, 2]);
// passes
enforce(3).inside([1, 2]);
// throws
- boolean: checks if a number is an element in an array
enforce(false).inside([true, false]);
// passes
enforce(true).inside([1, 2, 3]);
// throws
inside: string​
- string: checks if a string is inside another string
enforce('da').inside('tru dat.');
// passes
enforce('ad').inside('tru dat.');
// throws
notInside​
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']);
// passes
enforce('hello').notInside(['hello', 'world']);
enforce('da').notInside('tru dat.');
// throws
isTruthy​
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 (
"")
Usage examples:​
enforce('hello').isTruthy();
enforce(true).isTruthy();
enforce(1).isTruthy();
// passes
enforce(false).isTruthy();
enforce(null).isTruthy();
enforce(undefined).isTruthy();
enforce(0).isTruthy();
enforce(NaN).isTruthy();
enforce('').isTruthy();
// throws
isFalsy​
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(false).isFalsy();
enforce(0).isFalsy();
enforce(undefined).isFalsy();
// passes
enforce(1).isFalsy();
enforce(true).isFalsy();
enforce('hi').isFalsy();
// throws
isArray​
Description​
Checks if a value is of type Array.
Usage examples:​
enforce(['hello']).isArray();
// passes
enforce('hello').isArray();
// throws
isNotArray​
Description​
Checks if a value is of any type other than Array.
Reverse implementation of isArray.
Usage examples:​
enforce(['hello']).isNotArray();
// throws
enforce('hello').isNotArray();
// passes