UNPKG

logitar-validation

Version:

JavaScript validation library distributed by Logitar.

26 lines (25 loc) 1.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * A validation rule that checks if a string contains a minimum number of unique characters. * @param value The value to validate. * @param args The minimum number of unique characters. * @returns The result of the validation rule execution. */ const uniqueCharacters = (value, args) => { const uniqueCharacters = Number(args); if (isNaN(uniqueCharacters) || uniqueCharacters <= 0) { return { severity: "warning", message: "The arguments should be a positive number." }; } if (typeof value !== "string") { return { severity: "error", message: "{{name}} must be a string." }; } else if (value.length > 0) { const count = [...new Set(value)].length; if (count < uniqueCharacters) { return { severity: "error", message: "{{name}} must contain at least {{uniqueCharacters}} unique character(s)." }; } } return { severity: "information" }; }; exports.default = uniqueCharacters;