@mendix/pluggable-widgets-tools
Version:
Mendix Pluggable Widgets Tools
45 lines (44 loc) • 2.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WidgetValidationError = void 0;
exports.throwOnIllegalChars = throwOnIllegalChars;
exports.throwOnNoMatch = throwOnNoMatch;
class WidgetValidationError extends Error {
constructor(message) {
super(message);
this.name = "Widget Validation Error";
}
}
exports.WidgetValidationError = WidgetValidationError;
/**
* Asserts that the given input string only contains the characters specified by `legalCharacters`.
* @param input The string under test
* @param legalCharacters The characters that the input string may contain. Follows character class notation from regex (inside the [])
* @param message The message that is included in the error. Specify where the input is used and can be corrected.
* @throws WidgetValidationError If the input contains characters not allowed by legalCharacters
*/
function throwOnIllegalChars(input, legalCharacters, message) {
const pattern = new RegExp(`([^${legalCharacters}])`, "g");
const illegalChars = input.match(pattern);
if (illegalChars === null) {
return;
}
const formatted = illegalChars
.reduce((unique, c) => (unique.includes(c) ? unique : [...unique, c]), [])
.map(c => `"${c}"`)
.join(", ");
throw new WidgetValidationError(`${message} contains illegal characters ${formatted}. Allowed characters are [${legalCharacters}].`);
}
/**
* Asserts that the given input string matches the given pattern.
* @param input The string under test
* @param pattern The pattern the input must match
* @param message The message that is included in the error. Specify where the input is used and can be corrected.
* @throws WidgetValidationError If the input contains characters not allowed by legalCharacters
*/
function throwOnNoMatch(input, pattern, message) {
if (pattern.test(input)) {
return;
}
throw new WidgetValidationError(`${message} does not match the pattern ${pattern}.`);
}