UNPKG

@cowwoc/requirements

Version:

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

93 lines 3.94 kB
/* * Copyright (c) 2019 Gili Tzabari * Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0 */ import { internalValueToString, MutableStringMappers, Configuration } from "../internal.mjs"; /** * Determines the behavior of a validator. */ class MutableConfiguration { _stringMappers; _allowDiff; _recordStacktrace; _throwOnFailure; _errorTransformer; /** * Creates a new configuration. * * @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 into a suitable runtime * error or error * @throws TypeError if any of the arguments are `undefined` or `null` */ constructor(allowDiff, stringMappers, recordStacktrace, throwOnFailure, errorTransformer) { this._allowDiff = allowDiff; this._stringMappers = stringMappers; this._recordStacktrace = recordStacktrace; this._throwOnFailure = throwOnFailure; this._errorTransformer = errorTransformer; } /** * @param configuration - the immutable configuration * @returns a mutable copy of the configuration */ static from(configuration) { return new MutableConfiguration(configuration.allowDiff(), MutableStringMappers.from(configuration.stringMappers()), configuration.recordStacktrace(), configuration.throwOnFailure(), configuration.errorTransformer()); } /** * Returns an immutable copy of this configuration. * * @returns an immutable copy of this configuration */ toImmutable() { return new Configuration(this._allowDiff, this._stringMappers.toImmutable(), this._recordStacktrace, this._throwOnFailure, this._errorTransformer); } allowDiff(mayDiff) { if (mayDiff === undefined) return this._allowDiff; this._allowDiff = mayDiff; return this; } /** * 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() { return this._stringMappers; } recordStacktrace(recordStacktrace) { if (recordStacktrace === undefined) return this._recordStacktrace; this._recordStacktrace = recordStacktrace; return this; } throwOnFailure(throwOnFailure) { if (throwOnFailure === undefined) return this._throwOnFailure; this._throwOnFailure = throwOnFailure; return this; } errorTransformer(errorTransformer) { if (errorTransformer === undefined) return this._errorTransformer; this._errorTransformer = errorTransformer; return this; } toString() { return `Configuration[allowDiff=${this._allowDiff}, stringMappers=\ ${internalValueToString(this._stringMappers)}, recordStacktrace: ${this._recordStacktrace}, \ throwOnFailure: ${this._throwOnFailure}]`; } } export { MutableConfiguration }; //# sourceMappingURL=MutableConfiguration.mjs.map