self-assert / Requirements
Requirements
A collection of common rule requirements.
It also provides a way to compose requirements using the and
, or
and not
functions.
Comparison
Function | Description |
---|---|
identical | Returns a predicate that checks if the value is equal to the expected value. |
differentFrom | Returns a predicate that checks if the value is not equal to the forbidden value. |
isIn | Returns a predicate that checks if the value is in the allowed set. |
isNotIn | Returns a predicate that checks if the value is not in the forbidden set. |
Composition
Methods for composing requirements. Example:
ts
const myRequirement = Requirements.and(
Requirements.greaterThan(0),
(value) => value % 42 === 0
);
console.log(myRequirement(0)); // false
console.log(myRequirement(42)); // true
Function | Description |
---|---|
and | Combines multiple conditions using logical AND |
or | Combines multiple conditions using logical OR |
not | Negates a condition |
Lists
Function | Description |
---|---|
hasExactly | Returns a predicate that holds when the list has exactly the given number of elements |
isEmpty | Returns a predicate that holds when the list has no elements |
isNotEmpty | Returns a predicate that holds when the list has at least one element |
hasMoreThan | Returns a predicate that holds when the list has more than the given number of elements |
hasAtMost | Returns a predicate that holds when the list has at most the given number of elements |
hasLessThan | Returns a predicate that holds when the list has less than the given number of elements |
hasAtLeast | Returns a predicate that holds when the list has at least the given number of elements |
includes | Returns a predicate that holds when the list contains the given element |
doesNotInclude | Opposite of includes |
allSatisfy | Returns a predicate that holds when all elements of the list satisfy the given condition |
anySatisfy | Returns a predicate that holds when any element of the list satisfies the given condition |
noneSatisfy | Returns a predicate that holds when no element of the list satisfies the given condition. Opposite of anySatisfy |
Numbers
Function | Description |
---|---|
greaterThan | Returns a predicate that is true if the value is greater than the given number |
greaterThanOrEqual | Returns a predicate that is true if the value is greater than or equal to the given number |
lessThan | Returns a predicate that is true if the value is less than the given number |
lessThanOrEqual | Returns a predicate that is true if the value is less than or equal to the given number |
between | Returns a predicate that is true if the value is between two numbers (inclusive) |
isInteger | Returns a predicate that is true if the value is an integer |
isFloat | Returns a predicate that is true if the value is a float |
isPositive | Returns a predicate that is true if the value is positive |
isNegative | Returns a predicate that is true if the value is negative |
isPositiveInteger | Returns a predicate that is true if the value is a positive integer |
isNegativeInteger | Returns a predicate that is true if the value is a negative integer |
isIntegerBetween | Returns a predicate that is true if the value is an integer between two numbers (inclusive) |
Strings
Function | Description |
---|---|
isBlank | A predicate that evaluates to true if the string is empty or contains only whitespace characters. |
isNotBlank | Opposite of isBlank. |
Others
Function | Description |
---|---|
hold | A predicate that always evaluates to true . |
fail | A predicate that always evaluates to false . |