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 (
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.