@cowwoc/requirements
Version:
A fluent API for enforcing design contracts with automatic message generation.
354 lines (353 loc) • 15.7 kB
text/typescript
import { Configuration, AbstractValidators, type BooleanValidator, type StringValidator, type NumberValidator, type SetValidator, type UnknownValidator, type MapValidator, type ArrayValidator, type ApplicationScope, JavascriptValidators } from "../internal.mjs";
/**
* The default implementation of JavascriptValidators.
*/
declare class JavascriptValidatorsImpl extends AbstractValidators<JavascriptValidators> implements JavascriptValidators {
private static readonly DEFAULT_NAME;
/**
* A validator factory that creates validators to check the arguments of validation methods.
*/
static readonly INTERNAL: JavascriptValidatorsImpl;
/**
* Creates a new instance of this validator with an independent configuration.
*
* @param scope - the application configuration
* @param configuration - the configuration to use for new validators
* @throws TypeError if any of the arguments are `undefined` or `null`
*/
constructor(scope: ApplicationScope, configuration: Configuration);
/**
* Creates a new instance of this validator with an independent configuration.
*
* @param scope - the application configuration
* @param other - the factory to copy
* @throws TypeError if any of the arguments are `undefined` or `null`
*/
constructor(scope: ApplicationScope, other: JavascriptValidatorsImpl);
/**
* @param configurationOrOther - the configuration to use for new validators or the factory to copy
* @returns the configuration to use for new validators
*/
private static getRequireThatConfiguration;
/**
* 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
*/
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
*/
requireThatBoolean<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 a validator for the value
* @throws TypeError if `name` is `undefined` or `null`
* @throws RangeError if `name` is empty
*/
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 error immediately if a validation fails.
*
* @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 a validator for the value
* @throws TypeError if `name` is `undefined` or `null`
* @throws RangeError if `name` is empty
*/
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 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 a validator for the value
* @throws TypeError if `name` is `undefined` or `null`
* @throws RangeError if `name` is empty
*/
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 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
*/
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 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
*/
requireThat<T>(value: T, name: string): UnknownValidator<T>;
/**
* Validates the state of a number.
* <p>
* The returned validator captures exceptions on validation failure rather than throwing them immediately.
* The exceptions are converted into an {@link AssertionError} and can be retrieved or thrown once the
* validation completes. Exceptions unrelated to validation failures are thrown immediately.
*
* @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
*/
assertThatNumber<T extends number | undefined | null>(value: T, name?: string): NumberValidator<T>;
/**
* Validates the state of a boolean.
* <p>
* The returned validator captures exceptions on validation failure rather than throwing them immediately.
* The exceptions are converted into an {@link AssertionError} and can be retrieved or thrown once the
* validation completes. Exceptions unrelated to validation failures are thrown immediately.
*
* @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
*/
assertThatBoolean<T extends boolean | undefined | null>(value: T, name?: string): BooleanValidator<T>;
/**
* Validates the state of an array.
* <p>
* The returned validator captures exceptions on validation failure rather than throwing them immediately.
* The exceptions are converted into an {@link AssertionError} and can be retrieved or thrown once the
* validation completes. Exceptions unrelated to validation failures are thrown immediately.
*
* @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
*/
assertThatArray<T extends E[] | undefined | null, E>(value: T, name?: string): ArrayValidator<T, E>;
/**
* Validates the state of a set.
* <p>
* The returned validator captures exceptions on validation failure rather than throwing them immediately.
* The exceptions are converted into an {@link AssertionError} and can be retrieved or thrown once the
* validation completes. Exceptions unrelated to validation failures are thrown immediately.
*
* @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
*/
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 captures exceptions on validation failure rather than throwing them immediately.
* The exceptions are converted into an {@link AssertionError} and can be retrieved or thrown once the
* validation completes. Exceptions unrelated to validation failures are thrown immediately.
*
* @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
*/
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 captures exceptions on validation failure rather than throwing them immediately.
* The exceptions are converted into an {@link AssertionError} and can be retrieved or thrown once the
* validation completes. Exceptions unrelated to validation failures are thrown immediately.
*
* @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
*/
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 captures exceptions on validation failure rather than throwing them immediately.
* The exceptions are converted into an {@link AssertionError} and can be retrieved or thrown once the
* validation completes. Exceptions unrelated to validation failures are thrown immediately.
*
* @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
*/
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
*/
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
*/
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
*/
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 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
*/
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
*/
checkIfMap<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 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
*/
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
*/
checkIf<T>(value: T, name?: string): UnknownValidator<T>;
private validateNumber;
/**
* Ensures that the value's runtime and compile-time types match.
*
* @param validator - a validator
* @param value - the value
* @param expectedType - the value's expected type
*/
private validateType;
private validateBoolean;
private validateArray;
private validateSet;
private validateMap;
private validateString;
private validateUnknown;
copy(): JavascriptValidators;
withContext(value: unknown, name: string): this;
removeContext(name: string): this;
}
export { JavascriptValidatorsImpl };