@cowwoc/requirements
Version:
A fluent API for enforcing design contracts with automatic message generation.
111 lines • 5.15 kB
JavaScript
import { AbstractValidator, MessageBuilder, StringMappers } from "../internal.mjs";
/**
* @param validator - the validator
* @param expectedName - the name of the expected value
* @param expected - the expected value
* @returns a message for the validation failure
*/
function comparableIsEqualTo(validator, expectedName, expected) {
return comparableCompareValues(validator, "must be equal to", expectedName, expected);
}
/**
* @param validator - the validator
* @param limitName - the name of the value's bound
* @param maximumExclusive - the exclusive upper bound
* @returns a message for the validation failure
*/
function comparableIsLessThan(validator, limitName, maximumExclusive) {
return comparableCompareValues(validator, "must be less than", limitName, maximumExclusive);
}
/**
* @param validator - the validator
* @param limitName - the name of the value's bound
* @param maximumInclusive - the inclusive upper bound
* @returns a message for the validation failure
*/
function comparableIsLessThanOrEqualTo(validator, limitName, maximumInclusive) {
return comparableCompareValues(validator, "must be less than or equal to", limitName, maximumInclusive);
}
/**
* @param validator - the validator
* @param limitName - the name of the value's bound
* @param minimumInclusive - the inclusive lower bound
* @returns a message for the validation failure
*/
function comparableIsGreaterThanOrEqualTo(validator, limitName, minimumInclusive) {
return comparableCompareValues(validator, "must be greater than or equal to", limitName, minimumInclusive);
}
/**
* @param validator - the validator
* @param limitName - the name of the value's bound
* @param minimumExclusive - the exclusive lower bound
* @returns a message for the validation failure
*/
function comparableIsGreaterThan(validator, limitName, minimumExclusive) {
return comparableCompareValues(validator, "must be greater than", limitName, minimumExclusive);
}
/**
* @param validator - the validator
* @param relationship - a description of the relationship between the actual and expected value (e.g. "must
* be equal to")
* @param expectedName - the name of the expected value
* @param expected - the expected value
* @returns a message for the validation failure
*/
function comparableCompareValues(validator, relationship, expectedName, expected) {
const actualName = validator.getName();
// "actual" must be equal to "expected".
// actual : 123
// expected: 456
const expectedNameOrValue = validator.getNameOrValue("", expectedName, "", expected);
const messageBuilder = new MessageBuilder(validator, `${MessageBuilder.quoteName(actualName)} ${relationship} ${expectedNameOrValue}.`);
const invalidToNull = validator.getValueOrDefault(null);
if (invalidToNull !== null)
messageBuilder.withContext(invalidToNull, actualName);
if (expectedName !== null)
messageBuilder.withContext(expected, expectedName);
return messageBuilder;
}
/**
* @param validator - the validator
* @param minimum - the object representation of the lower limit
* @param minimumInclusive - `true` if the lower bound of the range is inclusive
* @param maximum - the object representation of the upper limit
* @param maximumInclusive - `true` if the upper bound of the range is inclusive
* @returns a message for the validation failure
*/
function isBetweenFailed(validator, minimum, minimumInclusive, maximum, maximumInclusive) {
const name = validator.getName();
const builder = new MessageBuilder(validator, `${MessageBuilder.quoteName(name)} is out of bounds.`);
const value = validator.getValueOrDefault(null);
if (value !== null)
builder.withContext(value, name);
const bounds = comparableGetBounds(minimum, minimumInclusive, maximum, maximumInclusive, validator.configuration().stringMappers());
builder.withContext(bounds, "bounds");
return builder;
}
/**
* @param minimum - the Object representation of the lower limit
* @param minimumInclusive - `true` if the lower bound of the range is inclusive
* @param maximum - the Object representation of the upper limit
* @param maximumInclusive - `true` if the upper bound of the range is inclusive
* @param stringMappers - the configuration used to map contextual values to a String
* @returns a message for the validation failure
*/
function comparableGetBounds(minimum, minimumInclusive, maximum, maximumInclusive, stringMappers) {
let bounds = "";
if (minimumInclusive)
bounds += "[";
else
bounds += "(";
const minimumAsString = stringMappers.toString(minimum);
const maximumAsString = stringMappers.toString(maximum);
bounds += `${minimumAsString}, ${maximumAsString}`;
if (maximumInclusive)
bounds += "]";
else
bounds += ")";
return bounds;
}
export { comparableIsEqualTo, comparableIsLessThan, comparableIsLessThanOrEqualTo, comparableIsGreaterThanOrEqualTo, comparableIsGreaterThan, comparableCompareValues, isBetweenFailed, comparableGetBounds };
//# sourceMappingURL=ComparableMessages.mjs.map