@cowwoc/requirements
Version:
A fluent API for enforcing design contracts with automatic message generation.
426 lines • 15.7 kB
JavaScript
import { Configuration, JavascriptValidatorsImpl, MainApplicationScope, JavascriptValidators, AssertionError } from "./internal/internal.mjs";
const typedocWorkaround = null;
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
// noinspection PointlessBooleanExpressionJS
if (typedocWorkaround !== null)
console.log("WORKAROUND: https://github.com/microsoft/tsdoc/issues/348");
/* eslint-enable @typescript-eslint/no-unnecessary-condition */
/**
* Creates validators for the Javascript API.
* <p>
* There are three kinds of validators:
* <ul>
* <li>`requireThat()` for method preconditions.</li>
* <li>`assertThat()` for class invariants, and method postconditions.</li>
* <li>`checkIf()` for returning multiple validation failures.</li>
* </ul>
* <p>
*/
const DELEGATE = new JavascriptValidatorsImpl(MainApplicationScope.INSTANCE, Configuration.DEFAULT);
/**
* 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
*/
function requireThatNumber(value, name) {
return DELEGATE.requireThatNumber(value, name);
}
/**
* 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
*/
function requireThatBoolean(value, name) {
return DELEGATE.requireThatBoolean(value, name);
}
/**
* 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
*/
function requireThatArray(value, name) {
return DELEGATE.requireThatArray(value, name);
}
/**
* 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
*/
function requireThatSet(value, name) {
return DELEGATE.requireThatSet(value, name);
}
/**
* 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
*/
function requireThatMap(value, name) {
return DELEGATE.requireThatMap(value, name);
}
/**
* 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
*/
function requireThatString(value, name) {
return DELEGATE.requireThatString(value, name);
}
/**
* 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
*/
function requireThat(value, name) {
return DELEGATE.requireThat(value, name);
}
/**
* 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
*/
function assertThatNumber(value, name) {
return DELEGATE.assertThatNumber(value, name);
}
/**
* 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
*/
function assertThatBoolean(value, name) {
return DELEGATE.assertThatBoolean(value, name);
}
/**
* 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
*/
function assertThatArray(value, name) {
return DELEGATE.assertThatArray(value, name);
}
/**
* 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
*/
function assertThatSet(value, name) {
return DELEGATE.assertThatSet(value, name);
}
/**
* 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
*/
function assertThatMap(value, name) {
return DELEGATE.assertThatMap(value, name);
}
/**
* 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
*/
function assertThatString(value, name) {
return DELEGATE.assertThatString(value, name);
}
/**
* 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
*/
function assertThat(value, name) {
return DELEGATE.assertThat(value, name);
}
/**
* 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
*/
function checkIfNumber(value, name) {
return DELEGATE.checkIfNumber(value, name);
}
/**
* 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
*/
function checkIfBoolean(value, name) {
return DELEGATE.checkIfBoolean(value, name);
}
/**
* 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
*/
function checkIfArray(value, name) {
return DELEGATE.checkIfArray(value, name);
}
/**
* 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
*/
function checkIfSet(value, name) {
return DELEGATE.checkIfSet(value, name);
}
/**
* 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
*/
function checkIfMap(value, name) {
return DELEGATE.checkIfMap(value, name);
}
/**
* 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
*/
function checkIfString(value, name) {
return DELEGATE.checkIfString(value, name);
}
/**
* 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
*/
function checkIf(value, name) {
return DELEGATE.checkIf(value, name);
}
/**
* 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}
*/
function updateConfiguration(updater) {
return DELEGATE.updateConfiguration(updater);
}
/**
* 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
*/
function getContext() {
return DELEGATE.getContext();
}
/**
* 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
*/
function withContext(value, name) {
return DELEGATE.withContext(value, name);
}
/**
* 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
*/
function removeContext(name) {
return DELEGATE.removeContext(name);
}
/**
* 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
*/
function globalConfiguration() {
return DELEGATE.getGlobalConfiguration();
}
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 };
//# sourceMappingURL=DefaultJavascriptValidators.mjs.map