@mitcsutt/kiln-forms
Version:
Kiln forms: typed React forms on TanStack Form, in component or JSON schema mode, rendered through kiln-ui
404 lines (403 loc) • 13.9 kB
JavaScript
import { unsafePathSegment as e } from "./values.js";
import { SWATCH_HINT as t, isSchemaSwatch as n } from "./swatch.js";
import { unsafePropReason as r } from "./unsafeProps.js";
import { CONTENT_SCHEMA_KEYS as i, FIELD_SCHEMA_KEYS as a, LAYOUT_SCHEMA_KEYS as o } from "./nodes.js";
import { unsafePatternReason as s } from "./patterns.js";
//#region src/schema/core/parseFormSchema.ts
var c = "pattern rules aren't allowed in parsed schemas; register a named validator, or pass allowPatterns for trusted sources", l = [
"stack",
"inline",
"grid",
"gridItem",
"section",
"aside",
"rows",
"panels",
"panel",
"tabs",
"tab",
"accordion",
"accordionItem",
"steps",
"step",
"sentence",
"review",
"actions"
], u = {
section: ["title"],
aside: ["title"],
panel: ["title"],
tabs: ["label"],
tab: ["value", "label"],
accordionItem: ["value", "title"],
step: ["value", "title"],
sentence: ["label"]
}, d = [
"heading",
"text",
"alert",
"divider",
"submit",
"reset",
"errorSummary",
"status"
], f = [
"info",
"positive",
"caution",
"critical",
"neutral"
], p = ["fill", "min"], m = [
"eq",
"neq",
"in",
"notIn",
"truthy",
"falsy",
"empty",
"notEmpty",
"gt",
"gte",
"lt",
"lte"
], h = [
"eq",
"neq",
"in"
], g = [
"required",
"custom",
"minLength",
"maxLength",
"pattern",
"email",
"url",
"min",
"max",
"step",
"integer",
"minItems",
"maxItems",
"unique",
"minDate",
"maxDate"
], _ = [
"minLength",
"maxLength",
"minItems",
"maxItems"
], v = /^\d{4}-\d{2}-\d{2}/, y = [
"prune",
"keep",
"reset"
], b = [
"kind",
"layout",
"content",
"custom"
], x = {
maxDepth: 64,
maxNodes: 2e3,
maxJsonDepth: 64
};
function S(e) {
return typeof e == "object" && !!e && !Array.isArray(e);
}
function C(e) {
return e ? new Set(Array.isArray(e) ? e : Object.keys(e)) : /* @__PURE__ */ new Set();
}
var w = (e) => typeof e == "string" ? `"${e}"` : String(JSON.stringify(e));
function T(e, t, n) {
e.issues.push({
path: t,
message: n
});
}
function E(e, t = x.maxJsonDepth) {
if (e === null || typeof e == "string" || typeof e == "boolean") return !0;
if (typeof e == "number") return Number.isFinite(e);
if (typeof e != "object" || t <= 0) return !1;
if (Array.isArray(e)) return e.every((e) => E(e, t - 1));
let n = Object.getPrototypeOf(e);
return n !== Object.prototype && n !== null ? !1 : Object.values(e).every((e) => E(e, t - 1));
}
var D = `functions, objects with methods and values nested over ${String(x.maxJsonDepth)} levels are not allowed`;
function O(t, n, r) {
let i = e(n);
return i === void 0 || (T(t, r, `Path segment "${i}" is not allowed`), !1);
}
function k(e, t, n, r = !1) {
if (t === void 0) {
r && T(e, n, "Expected an array of field names");
return;
}
if (!Array.isArray(t) || !t.every((e) => typeof e == "string" && e !== "")) {
T(e, n, "Expected an array of field names");
return;
}
t.forEach((t, r) => O(e, t, `${n}[${String(r)}]`));
}
function A(e, t, n, r = 1) {
if (r > x.maxDepth) {
T(e, n, `Conditions are nested more than ${String(x.maxDepth)} levels deep`);
return;
}
if (!S(t)) {
T(e, n, "Expected a condition object");
return;
}
for (let i of ["all", "any"]) {
if (!(i in t)) continue;
let a = t[i];
if (!Array.isArray(a)) {
T(e, `${n}.${i}`, "Expected an array of conditions");
return;
}
a.forEach((t, a) => {
A(e, t, `${n}.${i}[${String(a)}]`, r + 1);
});
return;
}
if ("not" in t) {
A(e, t.not, `${n}.not`, r + 1);
return;
}
let i = "field" in t;
if (!i && !("context" in t)) {
T(e, n, "Condition needs one of field, context, all, any or not");
return;
}
let a = i ? t.field : t.context;
typeof a != "string" || a === "" ? T(e, `${n}.${i ? "field" : "context"}`, "Expected a non-empty string") : i && O(e, a, `${n}.field`);
let o = i ? m : h, s = t.op;
if (typeof s != "string" || !o.includes(s)) {
T(e, `${n}.op`, `Unknown condition operator ${w(s)}`);
return;
}
s === "in" || s === "notIn" ? (!Array.isArray(t.value) || !E(t.value)) && T(e, `${n}.value`, `Operator "${s}" needs an array value`) : s === "gt" || s === "gte" || s === "lt" || s === "lte" ? (typeof t.value != "number" || !Number.isFinite(t.value)) && T(e, `${n}.value`, `Operator "${s}" needs a number value`) : (s === "eq" || s === "neq") && (!("value" in t) || !E(t.value)) && T(e, `${n}.value`, `Operator "${s}" needs a JSON value`);
}
function j(e, t, n) {
if (t !== void 0) {
if (!Array.isArray(t)) {
T(e, n, "Expected an array of rules");
return;
}
t.forEach((t, r) => {
let i = `${n}[${String(r)}]`;
if (!S(t)) {
T(e, i, "Expected a rule object");
return;
}
let a = t.rule;
if (typeof a != "string" || !g.includes(a)) {
T(e, `${i}.rule`, `Unknown rule ${w(a)}`);
return;
}
t.message !== void 0 && typeof t.message != "string" && T(e, `${i}.message`, "Expected a string");
let o = t.value;
if (_.includes(a)) (typeof o != "number" || !Number.isInteger(o) || o < 0) && T(e, `${i}.value`, `Rule "${a}" needs a whole number of 0 or more`);
else if (a === "min" || a === "max") (typeof o != "number" || !Number.isFinite(o)) && T(e, `${i}.value`, `Rule "${a}" needs a number`);
else if (a === "step") (typeof o != "number" || !(o > 0)) && T(e, `${i}.value`, "Rule \"step\" needs a number above 0");
else if (a === "pattern") {
if (!e.allowPatterns) {
T(e, `${i}.rule`, c);
return;
}
if (typeof o != "string") {
T(e, `${i}.value`, "Rule \"pattern\" needs a string");
return;
}
if (t.flags !== void 0 && typeof t.flags != "string") {
T(e, `${i}.flags`, "Expected a string");
return;
}
let n = s(o);
if (n !== void 0) {
T(e, `${i}.value`, n);
return;
}
try {
new RegExp(o, t.flags);
} catch {
T(e, `${i}.value`, `Invalid pattern ${w(o)}`);
}
} else a === "minDate" || a === "maxDate" ? (typeof o != "string" || o !== "today" && !v.test(o)) && T(e, `${i}.value`, `Rule "${a}" needs an ISO date (YYYY-MM-DD) or "today"`) : a === "custom" ? ((typeof t.validator != "string" || !e.validators.has(t.validator)) && T(e, `${i}.validator`, `Unknown validator ${w(t.validator)}`), t.args !== void 0 && !E(t.args) && T(e, `${i}.args`, "Expected JSON")) : a === "unique" && (t.by !== void 0 && typeof t.by != "string" ? T(e, `${i}.by`, "Expected a string") : typeof t.by == "string" && O(e, t.by, `${i}.by`));
});
}
}
function M(e, t, n, i, a = !1) {
for (let [o, s] of Object.entries(t)) {
if (i.includes(o)) continue;
let t = r(o, s, a);
t === void 0 ? E(s) || T(e, `${n}.${o}`, `Expected a JSON value (${D})`) : T(e, `${n}.${o}`, t);
}
}
function N(e, r, i) {
if (r !== void 0) {
if (!Array.isArray(r)) {
T(e, i, "Expected an array of options");
return;
}
r.forEach((r, a) => {
S(r) && [
"string",
"number",
"boolean"
].includes(typeof r.value) && typeof r.label == "string" ? S(r) && r.swatch !== void 0 && !n(r.swatch) && T(e, `${i}[${String(a)}].swatch`, `Expected ${t}`) : T(e, `${i}[${String(a)}]`, "Option needs a value (string, number or boolean) and a label");
});
}
}
function P(e, t, n) {
t !== void 0 && !y.includes(t) && T(e, n, "Expected \"prune\", \"keep\" or \"reset\"");
}
function F(e, t, n, r) {
if (typeof n != "string" || n === "" || /\s/.test(n)) {
T(e, r, "Expected a field name (no spaces)");
return;
}
if (O(e, n, r)) {
if (t.has(n)) {
T(e, r, `Duplicate field name ${w(n)}`);
return;
}
t.add(n);
}
}
function I(e, t, n, r) {
(typeof t.kind != "string" || !e.kinds.has(t.kind)) && T(e, `${n}.kind`, `Unknown field kind ${w(t.kind)}`), F(e, r, t.name, `${n}.name`), j(e, t.rules, `${n}.rules`), j(e, t.warnRules, `${n}.warnRules`), t.defaultValue !== void 0 && !E(t.defaultValue) && T(e, `${n}.defaultValue`, "Expected a JSON value"), P(e, t.whenHidden, `${n}.whenHidden`);
for (let r of [
"disabledWhen",
"readOnlyWhen",
"excludeWhen",
"requiredWhen"
]) t[r] !== void 0 && A(e, t[r], `${n}.${r}`);
if (t.optionsFrom !== void 0) {
let r = t.optionsFrom;
S(r) ? ((typeof r.loader != "string" || !e.loaders.has(r.loader)) && T(e, `${n}.optionsFrom.loader`, `Unknown loader ${w(r.loader)}`), k(e, r.deps, `${n}.optionsFrom.deps`)) : T(e, `${n}.optionsFrom`, "Expected { loader, deps? }");
}
if (k(e, t.resets, `${n}.resets`), t.compute !== void 0) {
let r = t.compute;
S(r) ? ((typeof r.computer != "string" || !e.computers.has(r.computer)) && T(e, `${n}.compute.computer`, `Unknown computer ${w(r.computer)}`), k(e, r.from, `${n}.compute.from`, !0)) : T(e, `${n}.compute`, "Expected { computer, from }");
}
N(e, t.options, `${n}.options`), M(e, t, n, a, !0);
}
function L(e, t, n, r, i, a) {
if (!Array.isArray(t)) {
T(e, n, `Expected an array of ${i}`);
return;
}
t.forEach((t, i) => {
U(e, t, `${n}[${String(i)}]`, r, a + 1);
});
}
function R(e, t, n) {
if (t !== void 0) {
if (!Array.isArray(t)) {
T(e, n, "Expected an array of columns");
return;
}
t.forEach((t, r) => {
let i = `${n}[${String(r)}]`;
if (!S(t)) {
T(e, i, "Expected a column object");
return;
}
typeof t.header != "string" && T(e, `${i}.header`, "Column needs a header (text)"), t.width !== void 0 && !p.includes(t.width) && T(e, `${i}.width`, `Unknown column width ${w(t.width)} (expected "fill" or "min")`);
});
}
}
function z(e, t, n, r, i) {
F(e, r, t.name, `${n}.name`), typeof t.label != "string" && T(e, `${n}.label`, "Repeater needs a label"), "newItem" in t ? (!S(t.newItem) || !E(t.newItem)) && T(e, `${n}.newItem`, "Expected a JSON object") : T(e, n, "Repeater needs newItem (the value of an added item)"), R(e, t.columns, `${n}.columns`), "reorderable" in t && typeof t.reorderable != "boolean" && t.reorderable !== "drag" && T(e, `${n}.reorderable`, `Unknown reorderable ${w(t.reorderable)} (expected true, false or "drag")`), j(e, t.rules, `${n}.rules`), P(e, t.whenHidden, `${n}.whenHidden`), L(e, t.item, `${n}.item`, /* @__PURE__ */ new Set(), "item nodes", i), M(e, t, n, o);
}
function B(e, t, n, r, i) {
let a = t.layout;
if (typeof a != "string" || !e.layouts.has(a)) T(e, `${n}.layout`, `Unknown layout ${w(a)}`);
else for (let r of u[a] ?? []) typeof t[r] != "string" && T(e, `${n}.${r}`, `Layout "${a}" needs a ${r} (text)`);
a === "gridItem" && t.whenAnimate !== void 0 && T(e, `${n}.whenAnimate`, "A gridItem can't animate: the motion wrapper would become the grid cell and drop its span");
let s = a === "review" ? /* @__PURE__ */ new Set() : r;
L(e, t.children, `${n}.children`, s, "child nodes", i), M(e, t, n, o);
}
function V(e, t, n) {
let r = t.content;
if (typeof r != "string" || !d.includes(r)) {
T(e, `${n}.content`, `Unknown content ${w(r)}`);
return;
}
(r === "heading" || r === "text" || r === "alert") && typeof t.text != "string" && T(e, `${n}.text`, `Content "${r}" needs text`), r === "heading" && t.level !== void 0 && ![
2,
3,
4
].includes(t.level) && T(e, `${n}.level`, "Expected 2, 3 or 4"), r === "alert" && t.tone !== void 0 && !f.includes(t.tone) && T(e, `${n}.tone`, `Unknown tone ${w(t.tone)}`), M(e, t, n, i);
}
function H(e, t, n) {
(typeof t.custom != "string" || !e.nodes.has(t.custom)) && T(e, `${n}.custom`, `Unknown custom node ${w(t.custom)}`), t.props !== void 0 && (!S(t.props) || !E(t.props)) && T(e, `${n}.props`, "Expected a JSON object");
for (let r of Object.keys(t)) [
"custom",
"props",
"id",
"when",
"whenAnimate"
].includes(r) || T(e, `${n}.${r}`, `Unexpected key ${w(r)} (put custom node data in props)`);
}
function U(e, t, n, r, i) {
if (i > x.maxDepth) {
T(e, n, `Nodes are nested more than ${String(x.maxDepth)} levels deep`);
return;
}
if (e.count += 1, e.count > x.maxNodes) {
e.count === x.maxNodes + 1 && T(e, n, `Schema has more than ${String(x.maxNodes)} nodes`);
return;
}
if (!S(t)) {
T(e, n, "Expected a node object");
return;
}
let a = b.filter((e) => e in t && !(e === "layout" && "kind" in t));
if (a.length === 0) {
T(e, n, "Node needs one of kind, layout, content or custom");
return;
}
if (a.length > 1) {
T(e, n, `Node has more than one of ${a.join(", ")}`);
return;
}
t.id !== void 0 && (typeof t.id != "string" || t.id === "" ? T(e, `${n}.id`, "Expected a non-empty string") : e.ids.has(t.id) ? T(e, `${n}.id`, `Duplicate node id ${w(t.id)}`) : e.ids.add(t.id)), t.when !== void 0 && A(e, t.when, `${n}.when`), t.whenAnimate !== void 0 && typeof t.whenAnimate != "boolean" && T(e, `${n}.whenAnimate`, "Expected a boolean"), "kind" in t ? I(e, t, n, r) : t.layout === "repeater" ? z(e, t, n, r, i) : "layout" in t ? B(e, t, n, r, i) : "content" in t ? V(e, t, n) : H(e, t, n);
}
function W(e, t, n = {}) {
let r = {
issues: [],
kinds: C(t.kinds),
layouts: /* @__PURE__ */ new Set([...l, ...C(t.layouts)]),
loaders: C(t.loaders),
validators: C(t.validators),
computers: C(t.computers),
nodes: C(t.nodes),
ids: /* @__PURE__ */ new Set(),
count: 0,
allowPatterns: n.allowPatterns === !0
};
if (!S(e)) return {
ok: !1,
issues: [{
path: "",
message: "Expected a schema object"
}]
};
if (e.version !== 1 && T(r, "version", "Expected version 1"), e.title !== void 0 && typeof e.title != "string" && T(r, "title", "Expected a string"), e.description !== void 0 && typeof e.description != "string" && T(r, "description", "Expected a string"), !("root" in e)) T(r, "root", "Schema needs a root node");
else try {
U(r, e.root, "root", /* @__PURE__ */ new Set(), 1);
} catch (e) {
if (!(e instanceof RangeError)) throw e;
T(r, "root", "Schema is nested too deeply to check");
}
return r.issues.length > 0 ? {
ok: !1,
issues: r.issues
} : {
ok: !0,
schema: e
};
}
//#endregion
export { l as DEFAULT_LAYOUT_KEYS, c as PATTERN_NOT_ALLOWED, x as SCHEMA_LIMITS, W as parseFormSchema };
//# sourceMappingURL=parseFormSchema.js.map