UNPKG

logitar-validation

Version:

JavaScript validation library distributed by Logitar.

26 lines (25 loc) 1.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * A validation rule that checks if a string only contains allowed characters. * @param value The value to validate. * @param args The allowed characters. * @returns The result of the validation rule execution. */ const allowedCharacters = (value, args) => { if (typeof value !== "string") { return { severity: "error", message: "{{name}} must be a string." }; } else if (typeof args !== "string") { return { severity: "warning", message: "The arguments must be a string containing the allowed characters." }; } const prohibitedCharacters = new Set([...value].filter((c) => !args.includes(c))); if (prohibitedCharacters.size > 0) { return { severity: "error", message: `{{name}} contains the following prohibited characters: ${[...prohibitedCharacters].join("")}. Only the following characters are allowed: {{allowedCharacters}}`, }; } return { severity: "information" }; }; exports.default = allowedCharacters;