UNPKG

@agile-ts/multieditor

Version:

Simple Form Manager for UI-Frameworks

87 lines (85 loc) 2.94 kB
function isString(errorMessage) { return { key: "isString", method: (toValidateItemKey, value, editor) => { const isValid = typeof value === "string"; if (!isValid) { editor.setStatus(toValidateItemKey, "error", errorMessage || `${toValidateItemKey} must be a valid string`); } return isValid; } }; } function maxLength(maxLength2, errorMessage) { return { key: "maxLength", method: (toValidateItemKey, value, editor) => { if (value == null || typeof value !== "string") return false; const isValid = value.length <= maxLength2; if (!isValid) { editor.setStatus(toValidateItemKey, "error", errorMessage || `${toValidateItemKey} must be at most ${maxLength2} characters`); } return isValid; } }; } function minLength(minLength2, errorMessage) { return { key: "minLength", method: (toValidateItemKey, value, editor) => { if (value == null || typeof value !== "string") return false; const isValid = value.length >= minLength2; if (!isValid) { editor.setStatus(toValidateItemKey, "error", errorMessage || `${toValidateItemKey} must be at least ${minLength2} characters`); } return isValid; } }; } function isEmail(errorMessage) { return { key: "isEmail", method: (toValidateItemKey, value, editor) => { if (value == null || typeof value !== "string") return false; const emailRegex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; const isValid = emailRegex.test(value.toLowerCase()); if (!isValid) { editor.setStatus(toValidateItemKey, "error", errorMessage || `${toValidateItemKey} must be a valid email`); } return isValid; } }; } function isUrl(errorMessage) { return { key: "isUrl", method: (toValidateItemKey, value, editor) => { if (value == null || typeof value !== "string") return false; const urlRegex = /[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)?/gi; const isValid = urlRegex.test(value.toLowerCase()); if (!isValid) { editor.setStatus(toValidateItemKey, "error", errorMessage || `${toValidateItemKey} must be a valid url`); } return isValid; } }; } function matchesRegex(regex, errorMessage) { return { key: "matchesRegex", method: (toValidateItemKey, value, editor) => { if (value == null || typeof value !== "string") return false; const isValid = regex.test(value.toLowerCase()); if (!isValid) { editor.setStatus(toValidateItemKey, "error", errorMessage || `${toValidateItemKey} must follow the defined regex`); } return isValid; } }; } export { isEmail, isString, isUrl, matchesRegex, maxLength, minLength };