Skip to content

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

FunctionDescription
identicalReturns a predicate that checks if the value is equal to the expected value.
differentFromReturns a predicate that checks if the value is not equal to the forbidden value.
isInReturns a predicate that checks if the value is in the allowed set.
isNotInReturns 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
FunctionDescription
andCombines multiple conditions using logical AND
orCombines multiple conditions using logical OR
notNegates a condition

Lists

FunctionDescription
hasExactlyReturns a predicate that holds when the list has exactly the given number of elements
isEmptyReturns a predicate that holds when the list has no elements
isNotEmptyReturns a predicate that holds when the list has at least one element
hasMoreThanReturns a predicate that holds when the list has more than the given number of elements
hasAtMostReturns a predicate that holds when the list has at most the given number of elements
hasLessThanReturns a predicate that holds when the list has less than the given number of elements
hasAtLeastReturns a predicate that holds when the list has at least the given number of elements
includesReturns a predicate that holds when the list contains the given element
doesNotIncludeOpposite of includes
allSatisfyReturns a predicate that holds when all elements of the list satisfy the given condition
anySatisfyReturns a predicate that holds when any element of the list satisfies the given condition
noneSatisfyReturns a predicate that holds when no element of the list satisfies the given condition. Opposite of anySatisfy

Numbers

FunctionDescription
greaterThanReturns a predicate that is true if the value is greater than the given number
greaterThanOrEqualReturns a predicate that is true if the value is greater than or equal to the given number
lessThanReturns a predicate that is true if the value is less than the given number
lessThanOrEqualReturns a predicate that is true if the value is less than or equal to the given number
betweenReturns a predicate that is true if the value is between two numbers (inclusive)
isIntegerReturns a predicate that is true if the value is an integer
isFloatReturns a predicate that is true if the value is a float
isPositiveReturns a predicate that is true if the value is positive
isNegativeReturns a predicate that is true if the value is negative
isPositiveIntegerReturns a predicate that is true if the value is a positive integer
isNegativeIntegerReturns a predicate that is true if the value is a negative integer
isIntegerBetweenReturns a predicate that is true if the value is an integer between two numbers (inclusive)

Strings

FunctionDescription
isBlankA predicate that evaluates to true if the string is empty or contains only whitespace characters.
isNotBlankOpposite of isBlank.

Others

FunctionDescription
holdA predicate that always evaluates to true.
failA predicate that always evaluates to false.