@logixjs/form 0.0.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Form.cjs CHANGED
@@ -42,7 +42,7 @@ module.exports = __toCommonJS(Form_exports);
42
42
 
43
43
  // src/internal/form/impl.ts
44
44
  var Logix4 = __toESM(require("@logixjs/core"), 1);
45
- var import_effect4 = require("effect");
45
+ var import_effect3 = require("effect");
46
46
 
47
47
  // src/internal/form/traits.ts
48
48
  var isPlainObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
@@ -1006,8 +1006,14 @@ var makeFormController = (params) => {
1006
1006
  });
1007
1007
  const state = yield* runtime.getState;
1008
1008
  const { errors: _errors, ui: _ui, $form: _meta, ...values } = state;
1009
- const decoded = import_effect.Schema.decodeUnknownEither(params.valuesSchema)(values);
1010
- const schemaTree = decoded._tag === "Right" ? {} : toSchemaErrorTree(decoded.left);
1009
+ const schemaTree = (() => {
1010
+ try {
1011
+ import_effect.Schema.decodeUnknownSync(params.valuesSchema)(values);
1012
+ return {};
1013
+ } catch (error) {
1014
+ return toSchemaErrorTree(error);
1015
+ }
1016
+ })();
1011
1017
  yield* runtime.dispatch({
1012
1018
  _tag: "setValue",
1013
1019
  payload: { path: "errors.$schema", value: schemaTree }
@@ -1049,8 +1055,14 @@ var makeFormController = (params) => {
1049
1055
  $form: _metaAfterRules,
1050
1056
  ...valuesAfterRules
1051
1057
  } = stateAfterRules;
1052
- const decoded = import_effect.Schema.decodeUnknownEither(params.valuesSchema)(valuesAfterRules);
1053
- const schemaTree = decoded._tag === "Right" ? {} : toSchemaErrorTree(decoded.left);
1058
+ const schemaTree = (() => {
1059
+ try {
1060
+ import_effect.Schema.decodeUnknownSync(params.valuesSchema)(valuesAfterRules);
1061
+ return {};
1062
+ } catch (error) {
1063
+ return toSchemaErrorTree(error);
1064
+ }
1065
+ })();
1054
1066
  yield* runtime.dispatch({
1055
1067
  _tag: "setValue",
1056
1068
  payload: { path: "errors.$schema", value: schemaTree }
@@ -1166,7 +1178,7 @@ var install = (module2, config) => module2.logic(($) => {
1166
1178
  const prev = pending.get(path);
1167
1179
  if (!prev) return;
1168
1180
  pending.delete(path);
1169
- yield* import_effect2.Fiber.interruptFork(prev);
1181
+ yield* import_effect2.Fiber.interrupt(prev);
1170
1182
  });
1171
1183
  const scheduleDebouncedValidate = (path) => import_effect2.Effect.gen(function* () {
1172
1184
  const ms = debounceMs ?? 0;
@@ -1177,9 +1189,9 @@ var install = (module2, config) => module2.logic(($) => {
1177
1189
  yield* cancelPending(path);
1178
1190
  const fiber = yield* import_effect2.Effect.forkScoped(
1179
1191
  import_effect2.Effect.sleep(import_effect2.Duration.millis(ms)).pipe(
1180
- import_effect2.Effect.zipRight(validate("valueChange", path)),
1192
+ import_effect2.Effect.flatMap(() => validate("valueChange", path)),
1181
1193
  import_effect2.Effect.ensuring(import_effect2.Effect.sync(() => pending.delete(path))),
1182
- import_effect2.Effect.catchAllCause(() => import_effect2.Effect.void)
1194
+ import_effect2.Effect.catchCause(() => import_effect2.Effect.void)
1183
1195
  )
1184
1196
  );
1185
1197
  pending.set(path, fiber);
@@ -1316,7 +1328,6 @@ var makeFormRulesManifestArtifactExporter = (params) => ({
1316
1328
  });
1317
1329
 
1318
1330
  // src/internal/validators/index.ts
1319
- var import_effect3 = require("effect");
1320
1331
  var ERROR_VALUE_MAX_BYTES = 256;
1321
1332
  var textEncoder = new TextEncoder();
1322
1333
  var jsonByteSize = (value) => {
@@ -1411,7 +1422,7 @@ var pattern = (decl) => {
1411
1422
  var schemaErrorMessage = (schemaError) => {
1412
1423
  let message;
1413
1424
  try {
1414
- message = import_effect3.ParseResult.TreeFormatter.formatErrorSync(schemaError);
1425
+ message = schemaError instanceof Error ? schemaError.message || "schema invalid" : String(schemaError);
1415
1426
  } catch {
1416
1427
  message = "schema invalid";
1417
1428
  }
@@ -1549,28 +1560,28 @@ var list = (listPath, spec) => ({
1549
1560
 
1550
1561
  // src/internal/form/impl.ts
1551
1562
  var FormActions = {
1552
- setValue: import_effect4.Schema.Struct({ path: import_effect4.Schema.String, value: import_effect4.Schema.Unknown }),
1553
- blur: import_effect4.Schema.Struct({ path: import_effect4.Schema.String }),
1554
- submit: import_effect4.Schema.Void,
1555
- validate: import_effect4.Schema.Void,
1556
- validatePaths: import_effect4.Schema.Array(import_effect4.Schema.String),
1557
- submitAttempt: import_effect4.Schema.Void,
1558
- setSubmitting: import_effect4.Schema.Boolean,
1559
- reset: import_effect4.Schema.UndefinedOr(import_effect4.Schema.Unknown),
1560
- setError: import_effect4.Schema.Struct({ path: import_effect4.Schema.String, error: import_effect4.Schema.Unknown }),
1561
- clearErrors: import_effect4.Schema.UndefinedOr(import_effect4.Schema.Array(import_effect4.Schema.String)),
1562
- arrayAppend: import_effect4.Schema.Struct({ path: import_effect4.Schema.String, value: import_effect4.Schema.Unknown }),
1563
- arrayPrepend: import_effect4.Schema.Struct({ path: import_effect4.Schema.String, value: import_effect4.Schema.Unknown }),
1564
- arrayRemove: import_effect4.Schema.Struct({ path: import_effect4.Schema.String, index: import_effect4.Schema.Number }),
1565
- arraySwap: import_effect4.Schema.Struct({
1566
- path: import_effect4.Schema.String,
1567
- indexA: import_effect4.Schema.Number,
1568
- indexB: import_effect4.Schema.Number
1563
+ setValue: import_effect3.Schema.Struct({ path: import_effect3.Schema.String, value: import_effect3.Schema.Unknown }),
1564
+ blur: import_effect3.Schema.Struct({ path: import_effect3.Schema.String }),
1565
+ submit: import_effect3.Schema.Void,
1566
+ validate: import_effect3.Schema.Void,
1567
+ validatePaths: import_effect3.Schema.Array(import_effect3.Schema.String),
1568
+ submitAttempt: import_effect3.Schema.Void,
1569
+ setSubmitting: import_effect3.Schema.Boolean,
1570
+ reset: import_effect3.Schema.UndefinedOr(import_effect3.Schema.Unknown),
1571
+ setError: import_effect3.Schema.Struct({ path: import_effect3.Schema.String, error: import_effect3.Schema.Unknown }),
1572
+ clearErrors: import_effect3.Schema.UndefinedOr(import_effect3.Schema.Array(import_effect3.Schema.String)),
1573
+ arrayAppend: import_effect3.Schema.Struct({ path: import_effect3.Schema.String, value: import_effect3.Schema.Unknown }),
1574
+ arrayPrepend: import_effect3.Schema.Struct({ path: import_effect3.Schema.String, value: import_effect3.Schema.Unknown }),
1575
+ arrayRemove: import_effect3.Schema.Struct({ path: import_effect3.Schema.String, index: import_effect3.Schema.Number }),
1576
+ arraySwap: import_effect3.Schema.Struct({
1577
+ path: import_effect3.Schema.String,
1578
+ indexA: import_effect3.Schema.Number,
1579
+ indexB: import_effect3.Schema.Number
1569
1580
  }),
1570
- arrayMove: import_effect4.Schema.Struct({
1571
- path: import_effect4.Schema.String,
1572
- from: import_effect4.Schema.Number,
1573
- to: import_effect4.Schema.Number
1581
+ arrayMove: import_effect3.Schema.Struct({
1582
+ path: import_effect3.Schema.String,
1583
+ from: import_effect3.Schema.Number,
1584
+ to: import_effect3.Schema.Number
1574
1585
  })
1575
1586
  };
1576
1587
  var initialMeta2 = () => ({
@@ -1580,22 +1591,20 @@ var initialMeta2 = () => ({
1580
1591
  errorCount: 0
1581
1592
  });
1582
1593
  var make2 = (id, config, extend) => {
1583
- const ErrorsSchema = import_effect4.Schema.Unknown;
1584
- const UiSchema = import_effect4.Schema.Unknown;
1585
- const MetaSchema = import_effect4.Schema.Struct({
1586
- submitCount: import_effect4.Schema.Number,
1587
- isSubmitting: import_effect4.Schema.Boolean,
1588
- isDirty: import_effect4.Schema.Boolean,
1589
- errorCount: import_effect4.Schema.Number
1594
+ const ErrorsSchema = import_effect3.Schema.Unknown;
1595
+ const UiSchema = import_effect3.Schema.Unknown;
1596
+ const MetaSchema = import_effect3.Schema.Struct({
1597
+ submitCount: import_effect3.Schema.Number,
1598
+ isSubmitting: import_effect3.Schema.Boolean,
1599
+ isDirty: import_effect3.Schema.Boolean,
1600
+ errorCount: import_effect3.Schema.Number
1601
+ });
1602
+ const StateSchema = import_effect3.Schema.Struct({
1603
+ ...config.values.fields ?? {},
1604
+ errors: ErrorsSchema,
1605
+ ui: UiSchema,
1606
+ $form: MetaSchema
1590
1607
  });
1591
- const StateSchema = import_effect4.Schema.extend(
1592
- config.values,
1593
- import_effect4.Schema.Struct({
1594
- errors: ErrorsSchema,
1595
- ui: UiSchema,
1596
- $form: MetaSchema
1597
- })
1598
- );
1599
1608
  const Actions = FormActions;
1600
1609
  const reducers = makeFormReducers({
1601
1610
  initialValues: config.initialValues
@@ -1637,67 +1646,60 @@ var make2 = (id, config, extend) => {
1637
1646
  if (mode === "store" || mode === "index") return;
1638
1647
  throw new Error(`[Form.make] Invalid identity.mode for "${listPath}" (got "${String(mode)}")`);
1639
1648
  };
1649
+ const isRelativeRuleDep = (dep, options) => {
1650
+ const allowNumericRelativeDep = options?.allowNumericRelativeDep ?? true;
1651
+ if (dep === "$root") return false;
1652
+ if (dep.includes("[") || dep.includes("]") || dep.includes(".")) return false;
1653
+ if (!allowNumericRelativeDep && /^[0-9]+$/.test(dep)) return false;
1654
+ return true;
1655
+ };
1656
+ const prefixRuleDeps = (deps, prefix, options) => {
1657
+ if (!Array.isArray(deps)) return void 0;
1658
+ const out = [];
1659
+ for (const raw of deps) {
1660
+ if (typeof raw !== "string") continue;
1661
+ const dep = raw.trim();
1662
+ if (!dep) continue;
1663
+ out.push(isRelativeRuleDep(dep, options) && prefix ? `${prefix}.${dep}` : dep);
1664
+ }
1665
+ return out;
1666
+ };
1667
+ const prefixRuleInputDeps = (input, prefix, options) => {
1668
+ if (!input || typeof input !== "object") return input;
1669
+ if (Array.isArray(input)) return input;
1670
+ const anyInput = input;
1671
+ const deps = prefixRuleDeps(anyInput.deps, prefix, options);
1672
+ const validate = anyInput.validate;
1673
+ if (typeof validate === "function") {
1674
+ return deps !== void 0 ? { ...anyInput, deps } : input;
1675
+ }
1676
+ if (validate && typeof validate === "object" && !Array.isArray(validate)) {
1677
+ const nextValidate = { ...validate };
1678
+ for (const [name, raw] of Object.entries(validate)) {
1679
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) continue;
1680
+ const entryDeps = prefixRuleDeps(raw.deps, prefix, options);
1681
+ if (entryDeps !== void 0) {
1682
+ nextValidate[name] = { ...raw, deps: entryDeps };
1683
+ }
1684
+ }
1685
+ return {
1686
+ ...anyInput,
1687
+ ...deps !== void 0 ? { deps } : {},
1688
+ validate: nextValidate
1689
+ };
1690
+ }
1691
+ return deps !== void 0 ? { ...anyInput, deps } : input;
1692
+ };
1640
1693
  const compileRulesToTraitSpec = (rulesSpec2) => {
1641
1694
  if (!rulesSpec2 || rulesSpec2._tag !== "FormRulesSpec") {
1642
1695
  throw new Error(`[Form.make] "rules" must be a FormRulesSpec (from Form.rules(...)/rules.schema(...))`);
1643
1696
  }
1644
- const joinPath2 = (prefix, suffix) => {
1645
- if (!prefix) return suffix;
1646
- if (!suffix) return prefix;
1647
- return `${prefix}.${suffix}`;
1648
- };
1649
1697
  const dirnamePath = (path) => {
1650
1698
  const p = String(path ?? "").trim();
1651
1699
  if (!p) return "";
1652
1700
  const idx = p.lastIndexOf(".");
1653
1701
  return idx >= 0 ? p.slice(0, idx) : "";
1654
1702
  };
1655
- const isRelativeDep = (dep) => {
1656
- if (typeof dep !== "string") return false;
1657
- const d = dep.trim();
1658
- if (!d) return false;
1659
- if (d === "$root") return false;
1660
- if (d.includes("[") || d.includes("]")) return false;
1661
- if (d.includes(".")) return false;
1662
- return true;
1663
- };
1664
- const prefixDeps = (deps, prefix) => {
1665
- if (!Array.isArray(deps)) return void 0;
1666
- const out = [];
1667
- for (const raw of deps) {
1668
- if (typeof raw !== "string") continue;
1669
- const d = raw.trim();
1670
- if (!d) continue;
1671
- out.push(isRelativeDep(d) ? joinPath2(prefix, d) : d);
1672
- }
1673
- return out;
1674
- };
1675
- const prefixRuleInputDeps = (input, prefix) => {
1676
- if (!input || typeof input !== "object") return input;
1677
- if (Array.isArray(input)) return input;
1678
- const anyInput = input;
1679
- const deps = prefixDeps(anyInput.deps, prefix);
1680
- const validate = anyInput.validate;
1681
- if (typeof validate === "function") {
1682
- return deps !== void 0 ? { ...anyInput, deps } : input;
1683
- }
1684
- if (validate && typeof validate === "object" && !Array.isArray(validate)) {
1685
- const nextValidate = { ...validate };
1686
- for (const [name, raw] of Object.entries(validate)) {
1687
- if (!raw || typeof raw !== "object" || Array.isArray(raw)) continue;
1688
- const entryDeps = prefixDeps(raw.deps, prefix);
1689
- if (entryDeps !== void 0) {
1690
- nextValidate[name] = { ...raw, deps: entryDeps };
1691
- }
1692
- }
1693
- return {
1694
- ...anyInput,
1695
- ...deps !== void 0 ? { deps } : {},
1696
- validate: nextValidate
1697
- };
1698
- }
1699
- return deps !== void 0 ? { ...anyInput, deps } : input;
1700
- };
1701
1703
  const decls = Array.isArray(rulesSpec2.decls) ? rulesSpec2.decls : [];
1702
1704
  const spec = {};
1703
1705
  const declared = /* @__PURE__ */ new Set();
@@ -1872,45 +1874,9 @@ var make2 = (id, config, extend) => {
1872
1874
  const idx = valuePath.lastIndexOf(".");
1873
1875
  return idx >= 0 ? valuePath.slice(0, idx) : "";
1874
1876
  })();
1875
- const prefixDeps = (deps, prefix) => {
1876
- if (!Array.isArray(deps)) return void 0;
1877
- const out = [];
1878
- for (const raw of deps) {
1879
- if (typeof raw !== "string") continue;
1880
- const d = raw.trim();
1881
- if (!d) continue;
1882
- const isRelative = d !== "$root" && !d.includes("[") && !d.includes("]") && !d.includes(".") && !/^[0-9]+$/.test(d);
1883
- out.push(isRelative && prefix ? `${prefix}.${d}` : d);
1884
- }
1885
- return out;
1886
- };
1887
- const prefixRuleInputDeps = (input, prefix) => {
1888
- if (!input || typeof input !== "object") return input;
1889
- if (Array.isArray(input)) return input;
1890
- const anyInput = input;
1891
- const deps = prefixDeps(anyInput.deps, prefix);
1892
- const validate = anyInput.validate;
1893
- if (typeof validate === "function") {
1894
- return deps !== void 0 ? { ...anyInput, deps } : input;
1895
- }
1896
- if (validate && typeof validate === "object" && !Array.isArray(validate)) {
1897
- const nextValidate = { ...validate };
1898
- for (const [ruleName, raw] of Object.entries(validate)) {
1899
- if (!raw || typeof raw !== "object" || Array.isArray(raw)) continue;
1900
- const entryDeps = prefixDeps(raw.deps, prefix);
1901
- if (entryDeps !== void 0) {
1902
- nextValidate[ruleName] = { ...raw, deps: entryDeps };
1903
- }
1904
- }
1905
- return {
1906
- ...anyInput,
1907
- ...deps !== void 0 ? { deps } : {},
1908
- validate: nextValidate
1909
- };
1910
- }
1911
- return deps !== void 0 ? { ...anyInput, deps } : input;
1912
- };
1913
- const ruleInput = prefixRuleInputDeps(decl.rule, depsPrefix);
1877
+ const ruleInput = prefixRuleInputDeps(decl.rule, depsPrefix, {
1878
+ allowNumericRelativeDep: false
1879
+ });
1914
1880
  const rules3 = make(ruleInput);
1915
1881
  emitRuleSet({
1916
1882
  source: "rules",
@@ -2137,7 +2103,7 @@ var derived = (_valuesSchema) => {
2137
2103
  };
2138
2104
 
2139
2105
  // src/internal/dsl/rules.ts
2140
- var import_effect5 = require("effect");
2106
+ var import_effect4 = require("effect");
2141
2107
  var normalizePrefix = (prefix) => {
2142
2108
  const p = String(prefix ?? "").trim();
2143
2109
  if (!p) return "";
@@ -2190,8 +2156,12 @@ var makeFieldNode = (rule) => ({
2190
2156
  var fieldFromSchema = (schema) => ({
2191
2157
  validate: {
2192
2158
  schema: (value) => {
2193
- const decoded = import_effect5.Schema.decodeUnknownEither(schema)(value);
2194
- return decoded._tag === "Right" ? void 0 : schemaErrorMessage(decoded.left);
2159
+ try {
2160
+ import_effect4.Schema.decodeUnknownSync(schema)(value);
2161
+ return void 0;
2162
+ } catch (error) {
2163
+ return schemaErrorMessage(error);
2164
+ }
2195
2165
  }
2196
2166
  }
2197
2167
  });
@@ -2415,7 +2385,7 @@ var rules = (_valuesSchema) => {
2415
2385
  return field(args[0], args[1], args[2]);
2416
2386
  }
2417
2387
  const input = args[0];
2418
- if (import_effect5.Schema.isSchema(input)) return makeFieldNode(fieldFromSchema(input));
2388
+ if (import_effect4.Schema.isSchema(input)) return makeFieldNode(fieldFromSchema(input));
2419
2389
  return makeFieldNode(input);
2420
2390
  }
2421
2391
  build.field = field2;