cogsbox-shape 0.5.204 → 0.5.205

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.
@@ -41,6 +41,7 @@ type FormUpdateParams = {
41
41
  message: string;
42
42
  code?: string;
43
43
  }>) => void;
44
+ clearZodErrors: (paths: string[][]) => void;
44
45
  };
45
46
  export declare function wireShapeValidationOptions(box: ShapeSchemaBox, params: TransformStateParams): void;
46
47
  /** Cross-field refine errors only — field rules are handled by state via setOptions. */
@@ -1,5 +1,12 @@
1
1
  import { createPluginContext } from "cogsbox-state";
2
2
  import { z } from "zod";
3
+ function pathKey(path) {
4
+ return path.join("\0");
5
+ }
6
+ function resolveRelatedPaths(blurPath, relatedFields) {
7
+ const parent = blurPath.slice(0, -1);
8
+ return [...relatedFields].map((field) => [...parent, field]);
9
+ }
3
10
  function mapZodIssues(issues) {
4
11
  return issues.map((issue) => ({
5
12
  path: issue.path.map(String),
@@ -21,6 +28,10 @@ function getRelatedFields(entry, field) {
21
28
  }
22
29
  return related;
23
30
  }
31
+ function issueMatchesRelatedFields(issue, relatedFields) {
32
+ const leaf = String(issue.path.at(-1) ?? "");
33
+ return relatedFields.has(leaf);
34
+ }
24
35
  export function wireShapeValidationOptions(box, params) {
25
36
  const entry = box[params.stateKey];
26
37
  if (!entry)
@@ -46,12 +57,21 @@ export function validateShapeRefines(box, params) {
46
57
  const relatedFields = getRelatedFields(entry, field);
47
58
  if (!relatedFields)
48
59
  return;
60
+ const relatedPaths = resolveRelatedPaths(params.path, relatedFields);
49
61
  const result = clientSchema.safeParse(params.getState());
50
- if (result.success)
62
+ if (result.success) {
63
+ params.clearZodErrors(relatedPaths);
51
64
  return;
52
- const issues = result.error.issues.filter((issue) => relatedFields.has(String(issue.path[0])));
53
- if (issues.length > 0) {
54
- params.addZodErrors(mapZodIssues(issues));
65
+ }
66
+ const issues = result.error.issues.filter((issue) => issueMatchesRelatedFields(issue, relatedFields));
67
+ const mapped = mapZodIssues(issues);
68
+ const activeKeys = new Set(mapped.map((entry) => pathKey(entry.path)));
69
+ const stalePaths = relatedPaths.filter((targetPath) => !activeKeys.has(pathKey(targetPath)));
70
+ if (stalePaths.length > 0) {
71
+ params.clearZodErrors(stalePaths);
72
+ }
73
+ if (mapped.length > 0) {
74
+ params.addZodErrors(mapped);
55
75
  }
56
76
  }
57
77
  function buildInitialState(box) {
package/dist/schema.js CHANGED
@@ -832,7 +832,7 @@ function createViewObject(initialRegistryKey, selection, registry, tableNameToRe
832
832
  : registryEntry.zodSchemas.clientSchema;
833
833
  const primitiveShape = baseSchema.shape;
834
834
  if (subSelection === true) {
835
- return z.object(primitiveShape);
835
+ return baseSchema;
836
836
  }
837
837
  const selectedRelationShapes = {};
838
838
  if (typeof subSelection === "object") {
@@ -85,4 +85,36 @@ describe("refine runtime behavior", () => {
85
85
  const good = box.rules.schemas.clientInput.safeParse({ id: 1, min: 10, max: 1 });
86
86
  expect(good.success).toBe(true);
87
87
  });
88
+ it("view keeps leaf refines and prefixes issue paths", () => {
89
+ const rules = schema({
90
+ _tableName: "rules",
91
+ id: s.sqlite({ type: "int", pk: true }),
92
+ min: s.sqlite({ type: "int" }).clientInput({ value: 0 }),
93
+ max: s.sqlite({ type: "int" }).clientInput({ value: 0 }),
94
+ }).refine((r) => [
95
+ r("client", (row) => row.min >= row.max
96
+ ? { path: ["max"], message: "Max must be > min" }
97
+ : undefined, ["min", "max"]),
98
+ ]);
99
+ const journal = schema({
100
+ _tableName: "journal",
101
+ id: s.sqlite({ type: "int", pk: true }),
102
+ rules: s.hasOne(true),
103
+ });
104
+ const box = createSchemaBox({ rules, journal }, {
105
+ journal: { rules: { fromKey: "id", toKey: rules.id } },
106
+ });
107
+ const view = box.journal.createView({ rules: true });
108
+ const bad = view.schemas.client.safeParse({
109
+ id: 1,
110
+ rules: { id: 1, min: 10, max: 1 },
111
+ });
112
+ expect(bad.success).toBe(false);
113
+ if (!bad.success) {
114
+ expect(bad.error.issues[0]).toMatchObject({
115
+ path: ["rules", "max"],
116
+ message: "Max must be > min",
117
+ });
118
+ }
119
+ });
88
120
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.204",
3
+ "version": "0.5.205",
4
4
  "description": "A TypeScript library for creating type-safe database schemas with Zod validation, SQL type definitions, and automatic client/server transformations. Unifies client, server, and database types through a single schema definition, with built-in support for relationships and serialization.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",