UNPKG

@frank-auth/react

Version:

Flexible and customizable React UI components for Frank Authentication

278 lines (277 loc) 8.36 kB
const n = (r = "This field is required") => (e) => { const i = e == null || typeof e == "string" && e.trim() === "" || Array.isArray(e) && e.length === 0; return { isValid: !i, error: i ? r : void 0 }; }, f = (r, e) => (i) => { const s = e || `Must be at least ${r} characters`, t = typeof i == "string" && i.length >= r; return { isValid: t, error: t ? void 0 : s }; }, l = (r, e) => (i) => { const s = e || `Must be no more than ${r} characters`, t = typeof i == "string" && i.length <= r; return { isValid: t, error: t ? void 0 : s }; }, v = (r, e = "Invalid format") => (i) => { const s = typeof i == "string" && r.test(i); return { isValid: s, error: s ? void 0 : e }; }, g = (r = "Invalid email address") => { const e = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; return (i) => { if (!i) return { isValid: !0 }; const s = e.test(i); return { isValid: s, error: s ? void 0 : r }; }; }, p = (r = "Invalid phone number") => { const e = /^\+?[1-9]\d{1,14}$/; return (i) => { if (!i) return { isValid: !0 }; const s = i.replace(/[^\d+]/g, ""), t = e.test(s); return { isValid: t, error: t ? void 0 : r }; }; }, h = (r = "Username must be 3-30 characters and contain only letters, numbers, and underscores") => { const e = /^[a-zA-Z0-9_]{3,30}$/; return (i) => { if (!i) return { isValid: !0 }; const s = e.test(i); return { isValid: s, error: s ? void 0 : r }; }; }, w = (r = {}) => { const { minLength: e = 8, requireUppercase: i = !0, requireLowercase: s = !0, requireNumbers: t = !0, requireSymbols: a = !1, message: d } = r; return (c) => { if (!c) return { isValid: !0 }; const o = []; c.length < e && o.push(`Must be at least ${e} characters long`), i && !/[A-Z]/.test(c) && o.push("Must contain at least one uppercase letter"), s && !/[a-z]/.test(c) && o.push("Must contain at least one lowercase letter"), t && !/\d/.test(c) && o.push("Must contain at least one number"), a && !/[^A-Za-z0-9]/.test(c) && o.push("Must contain at least one special character"); const u = o.length === 0; return { isValid: u, error: d || (u ? void 0 : o[0]), errors: o.length > 0 ? o : void 0 }; }; }, V = (r, e = "Passwords do not match") => (i) => { const s = i === r; return { isValid: s, error: s ? void 0 : e }; }, b = (r = "Invalid URL") => (e) => { if (!e) return { isValid: !0 }; try { return new URL(e), { isValid: !0 }; } catch { return { isValid: !1, error: r }; } }, y = (r = {}) => { const { min: e, max: i, integer: s = !1, message: t } = r; return (a) => { if (a == null || a === "") return { isValid: !0 }; const d = Number(a); return isNaN(d) ? { isValid: !1, error: t || "Must be a valid number" } : s && !Number.isInteger(d) ? { isValid: !1, error: t || "Must be a whole number" } : e !== void 0 && d < e ? { isValid: !1, error: t || `Must be at least ${e}` } : i !== void 0 && d > i ? { isValid: !1, error: t || `Must be no more than ${i}` } : { isValid: !0 }; }; }, A = (r, e) => (i) => { if (!i) return { isValid: !0 }; const s = r.includes(i), t = e || `Must be one of: ${r.join(", ")}`; return { isValid: s, error: s ? void 0 : t }; }, z = (r, e = "Invalid value") => (i) => { const s = r(i); return typeof s == "boolean" ? { isValid: s, error: s ? void 0 : e } : { isValid: !1, error: s }; }, q = (r, e = "Invalid value") => async (i) => { try { const s = await r(i); return typeof s == "boolean" ? { isValid: s, error: s ? void 0 : e } : { isValid: !1, error: s }; } catch (s) { return { isValid: !1, error: s instanceof Error ? s.message : "Validation failed" }; } }, N = (...r) => async (e) => { for (const i of r) { const s = await i(e); if (!s.isValid) return s; } return { isValid: !0 }; }, m = async (r, e) => { const i = {}, s = {}; for (const [t, a] of Object.entries(e)) { const d = r[t], c = Array.isArray(a) ? a : [a]; for (const o of c) { const u = await o(d); if (!u.isValid) { i[t] = u.error || "Invalid value", u.errors ? s[t] = u.errors : u.error && (s[t] = [u.error]); break; } } } return { isValid: Object.keys(i).length === 0, errors: i, fieldErrors: s }; }, Z = async (r, e) => { const i = Array.isArray(e) ? e : [e]; for (const s of i) { const t = await s(r); if (!t.isValid) return t; } return { isValid: !0 }; }, M = async (r) => { const e = { identifier: [ n("Email or username is required") // Could be email or username, so we don't validate format here ] }; return r.password !== void 0 && (e.password = n("Password is required")), m(r, e); }, P = async (r) => { const e = {}; return r.emailAddress !== void 0 && (e.emailAddress = [n("Email is required"), g()]), r.username !== void 0 && (e.username = [n("Username is required"), h()]), r.password !== void 0 && (e.password = [n("Password is required"), w()]), r.confirmPassword !== void 0 && (e.confirmPassword = [ n("Please confirm your password"), V(r.password || "") ]), r.firstName !== void 0 && (e.firstName = [ n("First name is required"), f(1), l(50) ]), r.lastName !== void 0 && (e.lastName = [ n("Last name is required"), f(1), l(50) ]), r.phoneNumber !== void 0 && (e.phoneNumber = p()), m(r, e); }, $ = async (r) => { const e = { emailAddress: [n("Email is required"), g()] }; return m(r, e); }, R = async (r) => { const e = { currentPassword: n("Current password is required"), newPassword: [n("New password is required"), w()], confirmPassword: [ n("Please confirm your new password"), V(r.newPassword) ] }; return m(r, e); }, I = async (r) => { const e = {}; return r.firstName !== void 0 && (e.firstName = [f(1), l(50)]), r.lastName !== void 0 && (e.lastName = [f(1), l(50)]), r.username !== void 0 && (e.username = h()), r.emailAddress !== void 0 && (e.emailAddress = g()), r.phoneNumber !== void 0 && (e.phoneNumber = p()), r.bio !== void 0 && (e.bio = l(500)), r.website !== void 0 && (e.website = b()), m(r, e); }, E = async (r) => { const e = { name: [ n("Organization name is required"), f(1), l(100) ] }; return r.slug !== void 0 && (e.slug = [ v(/^[a-z0-9-]+$/, "Slug can only contain lowercase letters, numbers, and hyphens"), f(3), l(50) ]), r.description !== void 0 && (e.description = l(500)), r.website !== void 0 && (e.website = b()), r.billingEmail !== void 0 && (e.billingEmail = g()), m(r, e); }, x = async (r) => { const e = { emailAddress: [n("Email is required"), g()], roleId: n("Role is required") }; return r.customMessage !== void 0 && (e.customMessage = l(500)), m(r, e); }, F = (r) => /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(r), L = (r) => { const e = /^\+?[1-9]\d{1,14}$/, i = r.replace(/[^\d+]/g, ""); return e.test(i); }, U = (r) => /^[a-zA-Z0-9_]{3,30}$/.test(r), O = (r) => { try { return new URL(r), !0; } catch { return !1; } }, S = (r) => r.trim().replace(/[<>]/g, ""), _ = (r) => r.toLowerCase().trim(), j = (r) => r.replace(/[^\d+]/g, ""); export { q as asyncValidation, N as combine, V as confirmPassword, z as custom, g as email, F as isValidEmail, L as isValidPhone, O as isValidUrl, U as isValidUsername, l as maxLength, f as minLength, _ as normalizeEmail, j as normalizePhone, y as number, A as oneOf, w as password, v as pattern, p as phone, n as required, S as sanitizeInput, b as url, h as username, Z as validateField, m as validateForm, x as validateInvitationForm, E as validateOrganizationForm, R as validatePasswordChangeForm, $ as validatePasswordResetForm, I as validateProfileForm, M as validateSignInForm, P as validateSignUpForm }; //# sourceMappingURL=validation.js.map