@react-form-builder/core
Version:
React JSON Schema Form Builder to create complex, validated, reusable forms with no deep React knowledge required
53 lines (52 loc) • 1.7 kB
JavaScript
import { isPromise as v } from "../../../utils/isPromise.js";
import { needValidate as m } from "../../../utils/needValidate.js";
import { isUndefined as g } from "../../../utils/tools.js";
function k(r, n) {
return n.key === "code" ? -1 : n.key === "required" ? 1 : 0;
}
const u = () => !0;
function V(r, n) {
if (!n || !n.validations || !n.validations.length) return;
const e = [...n.validations].sort(k), d = (t) => {
if (!t.type || t.type === "internal") {
const i = r.internal[t.key];
if (!i)
return console.warn(`Cannot find 'internal' rule, key: '${t.key}'`), { settings: t, validator: u };
const a = i.validatorFactory(t.args ?? {});
return { settings: t, validator: a, params: i.params };
}
if (t.type === "custom") {
const i = r.custom?.[t.key];
if (i) return { settings: t, validator: i.validate, params: i.params };
}
return console.warn(`Cannot find rule, key: '${t.key}', type: '${t.type}'`), { settings: t, validator: u };
};
return e.map(d);
}
function w(r, n) {
const e = V(r, n);
return async (d, t, i) => {
if (!e) return;
const a = [];
for (const { settings: f, validator: p, params: y } of e) {
const o = {};
if (!m(f.validateWhen, i?.())) continue;
y?.filter((s) => !g(s.default)).map((s) => o[s.key] = s.default), Object.assign(o, f.args);
const c = p(d, t, o, i?.()), l = v(c) ? await c : c;
l !== !0 && a.push({
settings: f,
message: o.message ?? l
});
}
return a;
};
}
function O(r) {
return function(n) {
return w(r, n);
};
}
export {
O as typedValidatorsResolver
};
//# sourceMappingURL=validatorsResolver.js.map