@cowwoc/requirements
Version:
A fluent API for enforcing design contracts with automatic message generation.
75 lines (74 loc) • 3.4 kB
text/typescript
import { StringMappers } from "./internal.mjs";
/**
* Determines the behavior of a validator.
*/
declare class Configuration {
/**
* The default configuration.
*/
static readonly DEFAULT: Configuration;
private readonly _stringMappers;
private readonly _allowDiff;
private readonly _recordStacktrace;
private readonly _throwOnFailure;
private readonly _errorTransformer;
/**
* Creates a new configuration that:
* <ul>
* <li>Has an empty context.</li>
* <li>Throws an error on failure.</li>
* <li>Records the error stack trace when a validation failure occurs.</li>
* <li>May include a diff that compares the actual and expected values.</li>
* </ul>
*
* @param allowDiff - `true` if error messages may include a diff that compares actual and expected values
* @param stringMappers - the configuration used to map contextual values to a string
* @param recordStacktrace - `true` if the error stack trace must be recorded when a validation failure
* occurs. If `false`, the error type remains the same, but the stack trace points to the invocation
* of `elseGetError()`. Users who only plan to
* {@link ValidationFailures.getMessages|list of failure messages} instead of retrieving an error
* may see a performance improvement if this value is set to `false`.
* @param throwOnFailure - `true` if an error is thrown on validation failure
* @param errorTransformer - a function that transforms the validation error before it is thrown or
* returned
* @throws TypeError if any of the arguments are `undefined` or `null`
*/
constructor(allowDiff?: boolean, stringMappers?: StringMappers, recordStacktrace?: boolean, throwOnFailure?: boolean, errorTransformer?: (error: Error) => Error);
/**
* Returns `true` if error messages may include a diff that compares actual and expected values.
*
* @returns `true` by default
*/
allowDiff(): boolean;
/**
* Returns the configuration used to map contextual values to a String. Supports common types such as
* arrays, numbers, collections, maps, paths and errors.
*
* @returns a function that takes an object and returns the `string` representation of the object
*/
stringMappers(): StringMappers;
/**
* Returns `true` if the error stack trace must be recorded when a validation failure occurs. If `false`,
* the error type remains the same, but the stack trace points to the invocation of
* `elseGetError()`. Users who only plan to
* {@link ValidationFailures.getMessages|list of failure messages} instead of retrieving an error
* may see a performance improvement if this value is set to `false`.
*
* @returns `true` if error stack traces must be recorded when a validation failure occurs
*/
recordStacktrace(): boolean;
/**
* Returns `true` if an error is thrown on validation failure.
*
* @returns `true` if an error is thrown on validation failure
*/
throwOnFailure(): boolean;
/**
* Returns a function that transforms validation errors before they are thrown or recorded.
*
* @returns a function that transforms the validation error
*/
errorTransformer(): (error: Error) => Error;
toString(): string;
}
export { Configuration };