@cowwoc/requirements
Version:
A fluent API for enforcing design contracts with automatic message generation.
172 lines • 6.17 kB
JavaScript
import { Configuration, IllegalStateError, MutableConfiguration, MutableStringMappers, internalValueToString, requireThatValueIsNotNull, AssertionError } from "../internal.mjs";
/**
* Updates the configuration that will be used by new validators.
*
* @typeParam S - the type of the validator factory
*/
class ConfigurationUpdaterImpl {
outer;
_allowDiff;
mutableStringMappers;
_recordStacktrace;
_errorTransformer;
changed = false;
closed = false;
/**
* Creates a new configuration updater.
*
* @param outer - a reference to the outer class
*/
constructor(outer) {
this.outer = outer;
const configuration = outer.getRequireThatConfiguration();
this._allowDiff = configuration.allowDiff();
this.mutableStringMappers = MutableStringMappers.from(configuration.stringMappers());
this._recordStacktrace = configuration.recordStacktrace();
this._errorTransformer = configuration.errorTransformer();
}
allowDiff(allowDiff) {
this.ensureOpen();
if (allowDiff === undefined)
return this._allowDiff;
if (allowDiff !== this._allowDiff) {
this._allowDiff = allowDiff;
this.changed = true;
}
return this;
}
stringMappers() {
this.ensureOpen();
return this.mutableStringMappers;
}
recordStacktrace(recordStacktrace) {
this.ensureOpen();
if (recordStacktrace === undefined)
return this._recordStacktrace;
if (recordStacktrace !== this._recordStacktrace) {
this._recordStacktrace = recordStacktrace;
this.changed = true;
}
return this;
}
errorTransformer(errorTransformer) {
this.ensureOpen();
if (errorTransformer === undefined)
return this._errorTransformer;
if (errorTransformer !== this._errorTransformer) {
this._errorTransformer = errorTransformer;
this.changed = true;
}
return this;
}
/**
* @throws IllegalStateError if the updater is closed
*/
ensureOpen() {
if (this.closed)
throw new IllegalStateError("The changes have already been applied");
}
close() {
if (this.closed)
return;
this.closed = true;
const oldConfiguration = this.outer.getRequireThatConfiguration();
const immutableStringMappers = this.mutableStringMappers.toImmutable();
this.changed ||= immutableStringMappers !== oldConfiguration.stringMappers();
if (!this.changed)
return;
this.outer.setConfiguration(new Configuration(this._allowDiff, immutableStringMappers, this._recordStacktrace, oldConfiguration.throwOnFailure(), this._errorTransformer));
}
toString() {
return `allowDiff: ${this._allowDiff}, stringMappers: ${internalValueToString(this.mutableStringMappers)}, recordStacktrace: ${this._recordStacktrace}`;
}
}
/**
* @typeParam S - the type of the validator factory
*/
class AbstractValidators {
/**
* A function that converts the thrown error to `AssertionError`.
*/
static CONVERT_TO_ASSERTION_ERROR = e => {
const assertionError = new AssertionError(e.message);
assertionError.stack = e.stack?.replace(e.name, assertionError.name);
throw assertionError;
};
scope;
requireThatConfiguration;
assertThatConfiguration;
checkIfConfiguration;
context = new Map();
/**
* Creates a new instance.
*
* @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, configuration) {
this.scope = scope;
requireThatValueIsNotNull(configuration, "configuration");
this.requireThatConfiguration = configuration;
this.assertThatConfiguration = MutableConfiguration.from(configuration).
throwOnFailure(false).errorTransformer(AbstractValidators.CONVERT_TO_ASSERTION_ERROR).toImmutable();
this.checkIfConfiguration = MutableConfiguration.from(configuration).
throwOnFailure(false).toImmutable();
}
/**
* @returns the application configuration
*/
getScope() {
return this.scope;
}
getRequireThatConfiguration() {
return this.requireThatConfiguration;
}
/**
* Returns the configuration for `assertThat` factory methods.
*
* @returns the configuration for `assertThat` factory methods
*/
getAssertThatConfiguration() {
return this.assertThatConfiguration;
}
/**
* Returns the configuration for `checkIf` factory methods.
*
* @returns the configuration for `checkIf` factory methods
*/
getCheckIfConfiguration() {
return this.checkIfConfiguration;
}
updateConfiguration(updater) {
if (updater === undefined)
return new ConfigurationUpdaterImpl(this);
const updatableConfiguration = this.updateConfiguration();
updater(updatableConfiguration);
updatableConfiguration.close();
return this;
}
/**
* Set the configuration used by new validators.
*
* @param configuration - the updated configuration
* @throws TypeError if `configuration` is null
*/
setConfiguration(configuration) {
requireThatValueIsNotNull(configuration, "configuration");
this.requireThatConfiguration = configuration;
this.assertThatConfiguration = MutableConfiguration.from(configuration).
errorTransformer(AbstractValidators.CONVERT_TO_ASSERTION_ERROR).toImmutable();
this.checkIfConfiguration = MutableConfiguration.from(configuration).
throwOnFailure(false).toImmutable();
}
getContext() {
return new Map(this.context);
}
getGlobalConfiguration() {
return this.scope.getGlobalConfiguration();
}
}
export { AbstractValidators };
//# sourceMappingURL=AbstractValidators.mjs.map