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
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 (
0
is empty) - array/string: checks against length. (
0
is empty) - number: checks the value of the number. (
0
andNaN
are empty) - boolean:
false
is 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