gib-validate
Version:
gib-validate is a lightweight validation helper for Vue 3 (Composition API). It provides a set of common validation rules and a `useValidation` hook to validate reactive form state.
135 lines (134 loc) • 5.12 kB
JavaScript
import { ref as v, shallowRef as T, computed as m, toValue as V, watch as q, unref as f, inject as x, watchEffect as w, provide as z } from "vue";
const E = Symbol("vuelidate"), y = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map();
function P(t, e) {
y.set(t, e);
const r = d.get(t);
r && (r.value = e);
}
function M(t) {
return y.get(t);
}
function _(t) {
if (!d.has(t)) {
const e = v(y.get(t));
d.set(t, e);
}
return d.get(t);
}
function F(t) {
const e = y.delete(t), r = d.get(t);
return r && (r.value = void 0), e;
}
function U(t, e, r) {
const i = v(!1), l = v({}), c = T(/* @__PURE__ */ new Set()), $ = v({}), N = () => {
i.value = !0, b(Object.keys(f(e))), Object.entries($.value).forEach(([n, s]) => {
s.$touch();
});
}, A = () => {
i.value = !1, c.value = /* @__PURE__ */ new Set([]), Object.entries($.value).forEach(([n, s]) => {
s.$reset();
});
}, b = (n) => {
i.value = !0;
const s = Array.isArray(n) ? n : [n];
c.value = /* @__PURE__ */ new Set([...f(c), ...s]);
}, D = (n) => {
const s = Array.isArray(n) ? n : [n], u = f(c), a = new Set(u);
s.forEach((o) => {
a.delete(o);
}), c.value = a;
}, R = m(() => {
var a;
const n = {}, s = V(l), u = Object.keys(s);
for (const o of u)
(a = l.value[o]) != null && a.length && c.value.has(o) && (n[o] = l.value[o]);
return n;
}), O = m(() => Object.entries(R.value).reduce(
(n, [s, u]) => (u != null && u.length && (n[s] = u[0]), n),
{}
));
q(
[() => ({ ...t }), () => e],
async ([n, s]) => {
const u = {}, a = f(s);
for (const o in a) {
const S = a[o];
if (S) {
const k = (await Promise.all(
S.map(async (g) => {
if (!g) return !0;
const j = await g(f(n[o]));
return typeof j == "string" ? j : null;
})
)).filter(
(g) => typeof g == "string"
);
k.length > 0 && (u[o] = k);
}
}
l.value = u;
},
{ immediate: !0 }
);
const h = m(() => ({
$touch: N,
$touched: c,
$reset: A,
$resetField: D,
$touchField: b,
$dirty: i.value,
$message: O.value,
$errors: R.value,
$silentErrors: l.value,
$children: $
// This should be a Ref, which it is
})), p = x(E, null);
return p && w(() => {
const n = r || Object.keys(p.value.$children).length.toString();
p.value.$children.value[n] = h.value;
}), r && (w(() => {
P(r, h.value);
}), w(() => () => {
F(r);
})), z(E, h), h;
}
const B = (t, e = "field is required") => (r) => t() && (!r || typeof r == "string" && r.trim() === "") ? e : !0, C = (t, e = "values do not match") => (r) => r !== t ? e : !0, G = (t, e = "invalid value") => (r) => t.includes(r) ? !0 : e, H = (t = "invalid url") => (e) => !e || !/^(https?:\/\/)?([a-z0-9-]+\.)+[a-z]{2,}(:[0-9]+)?(\/.*)?$/i.test(e) ? t : !0, J = (t = "invalid phone number") => (e) => !e || !/^\+?[0-9\s\-()]{7,}$/.test(e) ? t : !0, K = (t = "date must be in the past") => (e) => {
const r = /* @__PURE__ */ new Date();
return new Date(e) >= r ? t : !0;
}, Q = (t = 8, e = !0, r = `password must be at least ${t} chars and contain special characters`) => (i) => i.length < t || e && !/[!@#$%^&*(),.?":{}|<>]/.test(i) ? r : !0, W = (t = "value is not a number") => (e) => isNaN(Number(e)) ? t : !0, X = (t = "value is not an integer") => (e) => Number.isInteger(Number(e)) ? !0 : t, Y = (t = "date must be in the future") => (e) => {
const r = /* @__PURE__ */ new Date();
return new Date(e) <= r ? t : !0;
}, Z = (t = "invalid date") => (e) => {
const r = new Date(e);
return isNaN(r.getTime()) ? t : !0;
}, ee = (t, e = "invalid file type") => (r) => t.includes(r.type) ? !0 : e, te = (t, e = `file size must be less than ${t} MB`) => (r) => r.size > t * 1024 * 1024 ? e : !0, re = (t, e = "value must be equal to target") => (r) => r !== t ? e : !0, ne = (t, e = "value must contain specified value") => (r) => r.includes(String(t)) ? !0 : e, se = (t, e, r = `value must be between ${t} and ${e}`) => (i) => i < t || i > e ? r : !0, ie = (t = "invalid email") => (e) => {
const r = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return !e || e.length > 254 || !r.test(e) ? t : !0;
}, ue = (t, e = "value too long") => (r) => r.length > t ? e : !0, oe = (t, e = "value too short") => (r) => r.length < t ? e : !0, ae = (t = "value is required") => (e) => e == null || typeof e == "string" && e.trim() === "" || Array.isArray(e) && e.length === 0 || typeof e == "object" && !Object.keys(e).length ? t : !0;
export {
se as between,
ne as contains,
re as equalTo,
te as fileSizeLessThan,
ee as fileType,
M as getValidation,
Z as isDate,
ie as isEmail,
Y as isFutureDate,
X as isInteger,
W as isNumeric,
Q as isPassword,
K as isPastDate,
J as isPhone,
H as isUrl,
ue as maxLength,
oe as minLength,
G as oneOf,
P as registerValidation,
ae as required,
B as requiredIf,
C as sameAs,
F as unregisterValidation,
_ as useNamedValidation,
U as useValidation
};