UNPKG

logitar-validation

Version:

JavaScript validation library distributed by Logitar.

25 lines (24 loc) 972 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const logitar_js_1 = require("logitar-js"); const { isDigit, isLetterOrDigit, isNullOrEmpty } = logitar_js_1.stringUtils; /** * A validation rule that checks if a string is a valid identifier. * @param value The value to validate. * @returns The result of the validation rule execution. */ const identifier = (value) => { if (typeof value !== "string") { return { severity: "error", message: "{{name}} must be a string." }; } else if (value.length > 0) { if (isDigit(value[0])) { return { severity: "error", message: "{{name}} cannot start with a digit." }; } else if ([...value].some((c) => !isLetterOrDigit(c) && c !== "_")) { return { severity: "error", message: "{{name}} may only contain letters, digits and underscores (_)." }; } } return { severity: "information" }; }; exports.default = identifier;