UNPKG

@cowwoc/requirements

Version:

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

67 lines (66 loc) 2.8 kB
import { AbstractValidator } from "../../internal.mjs"; /** * Builds an error message. */ declare class MessageBuilder { static readonly DIFF_LEGEND = "\nLegend\n------\n+ : Add this character to the value\n- : Remove this character from the value\n[index] : Refers to the index of a collection element\n@line-number: Refers to the line number of a multiline string\n"; private readonly validator; private readonly message; private readonly failureContext; /** * A string that describes the difference between the expected and actual values. */ private readonly diff; /** * @param validator - the validator * @param message - (optional) the error message (empty string when absent) * @throws AssertionError if: * <ul> * <li>any of the arguments are null</li> * <li>`message` is blank or does not end with a dot</li> * </ul> */ constructor(validator: AbstractValidator<unknown>, message: string); /** * Appends context to the error message. If the context previously contained a mapping for the name, the * old value is replaced. * * @param value - the value of the context * @param name - (optional) the name of the context (empty string if absent) * @returns this * @throws AssertionError if `name`: * <ul> * <li>is `undefined` or `null`</li> * <li>is empty</li> * <li>contains whitespace or a colon</li> * </ul> */ withContext(value: unknown, name: string): this; /** * Adds a DIFF to the context that compares the value to an expected value * * @param actualName - the name of the value * @param actualValue - the object representation of the value * @param expectedName - the name of the expected value * @param expectedValue - the object representation of the expected value * @returns this */ addDiff(actualName: string, actualValue: unknown, expectedName: string, expectedValue: unknown): this; /** * @returns the contextual information associated with a validation failure */ private getValidatorContext; /** * Quotes the name of a parameter, unless it references a method call. * * @param name - the name of a parameter * @returns the updated name */ static quoteName(name: string): string; toString(): string; private addDiffToContext; private addValidatorContextToContext; private contextToString; private addErrorMessageToContext; } export { MessageBuilder };