UNPKG

@cowwoc/requirements

Version:

A fluent API for enforcing design contracts with automatic message generation.

356 lines (355 loc) 15.5 kB
import { type BooleanValidator, type StringValidator, type NumberValidator, type ArrayValidator, type SetValidator, type MapValidator, type UnknownValidator, JavascriptValidatorsImpl, type ConfigurationUpdater } from "./internal/internal.mjs"; /** * Validates the state of a number. * <p> * The returned validator throws an error immediately if a validation fails. * * @typeParam T - the type the value * @param value - the value * @param name - the name of the value * @returns a validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function requireThatNumber<T extends number | undefined | null>(value: T, name: string): NumberValidator<T>; /** * Validates the state of a boolean. * <p> * The returned validator throws an error immediately if a validation fails. * * @typeParam T - the type the value * @param value - the value * @param name - the name of the value * @returns a validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function requireThatBoolean<T extends boolean | undefined | null>(value: T, name: string): BooleanValidator<T>; /** * Validates the state of an array. * <p> * The returned validator throws an exception immediately if a validation fails. This exception is then * converted into an {@link AssertionError}. Exceptions unrelated to validation failures are not converted. * * @typeParam T - the type the value * @typeParam E - the type elements in the array * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function requireThatArray<T extends E[] | undefined | null, E>(value: T, name: string): ArrayValidator<T, E>; /** * Validates the state of a set. * <p> * The returned validator throws an exception immediately if a validation fails. This exception is then * converted into an {@link AssertionError}. Exceptions unrelated to validation failures are not converted. * * @typeParam T - the type the value * @typeParam E - the type elements in the set * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function requireThatSet<T extends Set<E> | undefined | null, E>(value: T, name: string): SetValidator<T, E>; /** * Validates the state of a map. * <p> * The returned validator throws an exception immediately if a validation fails. This exception is then * converted into an {@link AssertionError}. Exceptions unrelated to validation failures are not converted. * * @typeParam T - the type the value * @typeParam K - the type of keys in the map * @typeParam V - the type of values in the map * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function requireThatMap<T extends Map<K, V> | undefined | null, K, V>(value: T, name: string): MapValidator<T, K, V>; /** * Validates the state of a string. * <p> * The returned validator throws an exception immediately if a validation fails. This exception is then * converted into an {@link AssertionError}. Exceptions unrelated to validation failures are not converted. * * @typeParam T - the type the value * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function requireThatString<T extends string | undefined | null>(value: T, name: string): StringValidator<T>; /** * Validates the state of an unknown value or a value that does not have a specialized validator. * <p> * The returned validator throws an exception immediately if a validation fails. This exception is then * converted into an {@link AssertionError}. Exceptions unrelated to validation failures are not converted. * * @typeParam T - the type the value * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function requireThat<T>(value: T, name: string): UnknownValidator<T>; /** * Validates the state of a number. * <p> * The returned validator throws an exception immediately if a validation fails. This exception is then * converted into an {@link AssertionError}. Exceptions unrelated to validation failures are not converted. * * @typeParam T - the type the value * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function assertThatNumber<T extends number | undefined | null>(value: T, name?: string): NumberValidator<T>; /** * Validates the state of a boolean. * <p> * The returned validator throws an exception immediately if a validation fails. This exception is then * converted into an {@link AssertionError}. Exceptions unrelated to validation failures are not converted. * * @typeParam T - the type the value * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function assertThatBoolean<T extends boolean | undefined | null>(value: T, name?: string): BooleanValidator<T>; /** * Validates the state of an array. * <p> * The returned validator throws an exception immediately if a validation fails. This exception is then * converted into an {@link AssertionError}. Exceptions unrelated to validation failures are not converted. * * @typeParam T - the type the value * @typeParam E - the type elements in the array * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function assertThatArray<T extends E[] | undefined | null, E>(value: T, name?: string): ArrayValidator<T, E>; /** * Validates the state of a set. * <p> * The returned validator throws an exception immediately if a validation fails. This exception is then * converted into an {@link AssertionError}. Exceptions unrelated to validation failures are not converted. * * @typeParam T - the type the value * @typeParam E - the type elements in the set * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function assertThatSet<T extends Set<E> | undefined | null, E>(value: T, name?: string): SetValidator<T, E>; /** * Validates the state of a map. * <p> * The returned validator throws an exception immediately if a validation fails. This exception is then * converted into an {@link AssertionError}. Exceptions unrelated to validation failures are not converted. * * @typeParam T - the type the value * @typeParam K - the type of keys in the map * @typeParam V - the type of values in the map * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function assertThatMap<T extends Map<K, V> | undefined | null, K, V>(value: T, name?: string): MapValidator<T, K, V>; /** * Validates the state of a string. * <p> * The returned validator throws an exception immediately if a validation fails. This exception is then * converted into an {@link AssertionError}. Exceptions unrelated to validation failures are not converted. * * @typeParam T - the type the value * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function assertThatString<T extends string | undefined | null>(value: T, name?: string): StringValidator<T>; /** * Validates the state of an unknown value or a value that does not have a specialized validator. * <p> * The returned validator throws an exception immediately if a validation fails. This exception is then * converted into an {@link AssertionError}. Exceptions unrelated to validation failures are not converted. * * @typeParam T - the type the value * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function assertThat<T>(value: T, name?: string): UnknownValidator<T>; /** * Validates the state of a number. * <p> * The returned validator throws an error immediately if a validation fails. * * @typeParam T - the type the value * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function checkIfNumber<T extends number | undefined | null>(value: T, name: string): NumberValidator<T>; /** * Validates the state of a boolean. * <p> * The returned validator throws an error immediately if a validation fails. * * @typeParam T - the type the value * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function checkIfBoolean<T extends boolean | undefined | null>(value: T, name: string): BooleanValidator<T>; /** * Validates the state of an array. * <p> * The returned validator throws an error immediately if a validation fails. * * @typeParam T - the type the value * @typeParam E - the type elements in the array * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function checkIfArray<T extends E[] | undefined | null, E>(value: T, name: string): ArrayValidator<T, E>; /** * Validates the state of a set. * <p> * The returned validator throws an error immediately if a validation fails. * * @typeParam T - the type the value * @typeParam E - the type elements in the array or set * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function checkIfSet<T extends Set<E> | undefined | null, E>(value: T, name: string): SetValidator<T, E>; /** * Validates the state of a map. * <p> * The returned validator throws an error immediately if a validation fails. * * @typeParam T - the type the value * @typeParam K - the type of keys in the map * @typeParam V - the type of values in the map * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function checkIfMap<T extends Map<K, V>, K, V>(value: T, name: string): MapValidator<T, K, V>; /** * Validates the state of a string. * <p> * The returned validator throws an error immediately if a validation fails. * * @typeParam T - the type the value * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function checkIfString<T extends string | undefined | null>(value: T, name: string): StringValidator<T>; /** * Validates the state of an unknown value or a value that does not have a specialized validator. * <p> * The returned validator throws an error immediately if a validation fails. * * @typeParam T - the type the value * @typeParam E - the type elements in the array or set * @typeParam K - the type of keys in the map * @typeParam V - the type of values in the map * @param value - the value * @param name - the name of the value * @returns validator for the value * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` is empty */ declare function checkIf<T>(value: T, name: string): UnknownValidator<T>; /** * Updates the configuration that will be used by new validators. * * @param updater - a function that updates the configuration * @returns this * @see {@link JavascriptValidators.newInstance|Creating an independent configuration} */ declare function updateConfiguration(updater: (configuration: ConfigurationUpdater) => void): JavascriptValidatorsImpl; /** * Returns the contextual information for validators created out by this factory. The contextual information * is a map of key-value pairs that can provide more details about validation failures. For example, if the * message is "Password may not be empty" and the map contains the key-value pair * `{"username": "john.smith"}`, the error message would be: * <p> * ```console * Password may not be empty * username: john.smith * ``` * * @returns an unmodifiable map from each entry's name to its value */ declare function getContext(): Map<string, unknown>; /** * Sets the contextual information for validators created by this factory. * <p> * This method adds contextual information to error messages. The contextual information is stored as * key-value pairs in a map. Values set by this method may be overridden by * {@link ValidatorComponent.withContext}. * * @param value - the value of the entry * @param name - the name of an entry * @returns the underlying validator factory * @throws NullPointerError if `name` is not a string */ declare function withContext(value: unknown, name: string): JavascriptValidatorsImpl; /** * Removes the contextual information of validators created by this factory. * * @param name - the parameter name * @returns the underlying validator factory * @throws NullPointerError if `name` is not a string * @throws IllegalArgumentError if `name` contains leading or trailing whitespace, or is * empty */ declare function removeContext(name: string): JavascriptValidatorsImpl; /** * Returns the global configuration shared by all validators. * <p> * <b>NOTE</b>: Updating this configuration affects existing and new validators. * * @returns the global configuration updater */ declare function globalConfiguration(): import("./GlobalConfiguration.mjs").GlobalConfiguration; export { requireThatNumber, requireThatBoolean, requireThatArray, requireThatSet, requireThatMap, requireThatString, requireThat, assertThatNumber, assertThatBoolean, assertThatArray, assertThatSet, assertThatMap, assertThatString, assertThat, checkIfNumber, checkIfBoolean, checkIfArray, checkIfSet, checkIfMap, checkIfString, checkIf, updateConfiguration, getContext, withContext, removeContext, globalConfiguration };