logitar-validation
Version:
JavaScript validation library distributed by Logitar.
22 lines (21 loc) • 870 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const logitar_js_1 = require("logitar-js");
const { isLetterOrDigit, isNullOrEmpty } = logitar_js_1.stringUtils;
/**
* A validation rule that checks if a string is a valid slug.
* @param value The value to validate.
* @returns The result of the validation rule execution.
*/
const slug = (value) => {
if (typeof value !== "string") {
return { severity: "error", message: "{{name}} must be a string." };
}
else if (value.length > 0) {
if (value.split("-").some((word) => isNullOrEmpty(word) || [...word].some((c) => !isLetterOrDigit(c)))) {
return { severity: "error", message: "{{name}} must be composed of non-empty alphanumeric words separated by hyphens (-)." };
}
}
return { severity: "information" };
};
exports.default = slug;