cogsbox-shape 0.5.210 → 0.5.211
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.
|
@@ -1,11 +1,23 @@
|
|
|
1
|
+
import { type ChainMethodContext } from "cogsbox-state";
|
|
1
2
|
import { z } from "zod";
|
|
2
3
|
/** Minimal shape of a createSchemaBox entry — matches journalSchemaBox.journalTechnical etc. */
|
|
3
4
|
export type ShapeRefineInfo = {
|
|
4
5
|
fieldToGroup: Record<string, number[]>;
|
|
5
6
|
groups: {
|
|
6
|
-
deps: string[] | null;
|
|
7
|
+
deps: readonly string[] | null;
|
|
7
8
|
}[];
|
|
8
9
|
};
|
|
10
|
+
export type ShapeIssue = {
|
|
11
|
+
path: string[];
|
|
12
|
+
message: string;
|
|
13
|
+
code?: string;
|
|
14
|
+
};
|
|
15
|
+
export type ShapeErrorGroup = {
|
|
16
|
+
hasErrors: boolean;
|
|
17
|
+
message: string;
|
|
18
|
+
issues: ShapeIssue[];
|
|
19
|
+
};
|
|
20
|
+
export type ShapeErrorGroups = Record<string, ShapeErrorGroup>;
|
|
9
21
|
export type ShapeSchemaBoxEntry = {
|
|
10
22
|
/** Field-key → value map from DeriveStateType (not z.infer on a flattened client object). */
|
|
11
23
|
stateType: Record<string, unknown>;
|
|
@@ -45,11 +57,25 @@ type FormUpdateParams = {
|
|
|
45
57
|
code?: string;
|
|
46
58
|
}>) => void;
|
|
47
59
|
clearZodErrors: (paths: string[][]) => void;
|
|
60
|
+
setErrorGroups?: (parentPath: string[], groups: ShapeErrorGroups) => void;
|
|
48
61
|
};
|
|
62
|
+
type MutableTuple<T> = T extends readonly [...infer TItems] ? TItems : never;
|
|
63
|
+
type ExtractShapeRefineGroupFields<TEntry> = TEntry extends {
|
|
64
|
+
refineInfo?: {
|
|
65
|
+
groups: readonly (infer TGroup)[];
|
|
66
|
+
};
|
|
67
|
+
} ? TGroup extends {
|
|
68
|
+
deps: infer TDeps;
|
|
69
|
+
} ? TDeps extends readonly string[] ? MutableTuple<TDeps> : never : never : never;
|
|
70
|
+
type ShapeRefineGroupFields<TBox extends ShapeSchemaBox> = [
|
|
71
|
+
ExtractShapeRefineGroupFields<TBox[keyof TBox]>
|
|
72
|
+
] extends [never] ? string[] : ExtractShapeRefineGroupFields<TBox[keyof TBox]>;
|
|
49
73
|
export declare function wireShapeValidationOptions(box: ShapeSchemaBox, params: TransformStateParams): void;
|
|
50
74
|
/** Cross-field refine errors only — field rules are handled by state via setOptions. */
|
|
51
75
|
export declare function validateShapeRefines(box: ShapeSchemaBox, params: FormUpdateParams): void;
|
|
52
76
|
export declare function createShapePlugin<const TBox extends ShapeSchemaBox>(box: TBox): import("cogsbox-state").CogsPluginBuilder<"shape", {
|
|
53
77
|
logs: boolean | undefined;
|
|
54
|
-
}, unknown, unknown, never, {
|
|
78
|
+
}, unknown, unknown, never, {
|
|
79
|
+
$errorGroups: import("cogsbox-state").ChainMethodDefinition<(_ctx: ChainMethodContext, ...fields: ShapeRefineGroupFields<TBox>) => ShapeErrorGroup>;
|
|
80
|
+
}, true, false, true, true, false, true, InferShapeBoxState<TBox>>;
|
|
55
81
|
export {};
|
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
import { createPluginContext } from "cogsbox-state";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
+
const emptyErrorGroup = {
|
|
4
|
+
hasErrors: false,
|
|
5
|
+
message: "",
|
|
6
|
+
issues: [],
|
|
7
|
+
};
|
|
3
8
|
function pathKey(path) {
|
|
4
9
|
return path.join("\0");
|
|
5
10
|
}
|
|
11
|
+
function errorGroupStorageKey(stateKey, parentPath) {
|
|
12
|
+
return [stateKey, ...parentPath].join("\0");
|
|
13
|
+
}
|
|
14
|
+
function groupKey(fields) {
|
|
15
|
+
return fields.join("+");
|
|
16
|
+
}
|
|
6
17
|
function resolveRelatedPaths(blurPath, relatedFields) {
|
|
7
18
|
const parent = blurPath.slice(0, -1);
|
|
8
19
|
return [...relatedFields].map((field) => [...parent, field]);
|
|
@@ -32,6 +43,23 @@ function issueMatchesRelatedFields(issue, relatedFields) {
|
|
|
32
43
|
const leaf = String(issue.path.at(-1) ?? "");
|
|
33
44
|
return relatedFields.has(leaf);
|
|
34
45
|
}
|
|
46
|
+
function buildShapeErrorGroups(entry, groupIndexes, fallbackField, issues) {
|
|
47
|
+
const groups = {};
|
|
48
|
+
for (const index of groupIndexes) {
|
|
49
|
+
const deps = entry.refineInfo?.groups[index]?.deps ?? [fallbackField];
|
|
50
|
+
const relatedFields = new Set(deps);
|
|
51
|
+
const mapped = mapZodIssues(issues.filter((issue) => issueMatchesRelatedFields(issue, relatedFields)));
|
|
52
|
+
groups[groupKey(deps)] =
|
|
53
|
+
mapped.length > 0
|
|
54
|
+
? {
|
|
55
|
+
hasErrors: true,
|
|
56
|
+
message: mapped[0]?.message ?? "",
|
|
57
|
+
issues: mapped,
|
|
58
|
+
}
|
|
59
|
+
: emptyErrorGroup;
|
|
60
|
+
}
|
|
61
|
+
return groups;
|
|
62
|
+
}
|
|
35
63
|
export function wireShapeValidationOptions(box, params) {
|
|
36
64
|
const entry = box[params.stateKey];
|
|
37
65
|
if (!entry)
|
|
@@ -54,15 +82,19 @@ export function validateShapeRefines(box, params) {
|
|
|
54
82
|
const field = params.path.at(-1);
|
|
55
83
|
if (!field)
|
|
56
84
|
return;
|
|
85
|
+
const groupIndexes = entry.refineInfo?.fieldToGroup[field];
|
|
57
86
|
const relatedFields = getRelatedFields(entry, field);
|
|
58
87
|
if (!relatedFields)
|
|
59
88
|
return;
|
|
60
89
|
const relatedPaths = resolveRelatedPaths(params.path, relatedFields);
|
|
61
90
|
const result = clientSchema.safeParse(params.getState());
|
|
91
|
+
const parentPath = params.path.slice(0, -1);
|
|
62
92
|
if (result.success) {
|
|
93
|
+
params.setErrorGroups?.(parentPath, buildShapeErrorGroups(entry, groupIndexes ?? [], field, []));
|
|
63
94
|
params.clearZodErrors(relatedPaths);
|
|
64
95
|
return;
|
|
65
96
|
}
|
|
97
|
+
params.setErrorGroups?.(parentPath, buildShapeErrorGroups(entry, groupIndexes ?? [], field, result.error.issues));
|
|
66
98
|
const issues = result.error.issues.filter((issue) => issueMatchesRelatedFields(issue, relatedFields));
|
|
67
99
|
const mapped = mapZodIssues(issues);
|
|
68
100
|
const activeKeys = new Set(mapped.map((entry) => pathKey(entry.path)));
|
|
@@ -91,6 +123,14 @@ const { createPlugin } = createPluginContext({
|
|
|
91
123
|
}),
|
|
92
124
|
});
|
|
93
125
|
export function createShapePlugin(box) {
|
|
126
|
+
const errorGroupsByPath = new Map();
|
|
127
|
+
const setErrorGroups = (stateKey, parentPath, groups) => {
|
|
128
|
+
const key = errorGroupStorageKey(stateKey, parentPath);
|
|
129
|
+
errorGroupsByPath.set(key, {
|
|
130
|
+
...errorGroupsByPath.get(key),
|
|
131
|
+
...groups,
|
|
132
|
+
});
|
|
133
|
+
};
|
|
94
134
|
return createPlugin("shape")
|
|
95
135
|
.initialState(() => buildInitialState(box))
|
|
96
136
|
.transformState((params) => wireShapeValidationOptions(box, params))
|
|
@@ -98,6 +138,15 @@ export function createShapePlugin(box) {
|
|
|
98
138
|
if (params.options?.logs) {
|
|
99
139
|
console.log("[shape]", params.stateKey, params.path, params.event.activityType);
|
|
100
140
|
}
|
|
101
|
-
validateShapeRefines(box,
|
|
102
|
-
|
|
141
|
+
validateShapeRefines(box, {
|
|
142
|
+
...params,
|
|
143
|
+
setErrorGroups: (parentPath, groups) => setErrorGroups(params.stateKey, parentPath, groups),
|
|
144
|
+
});
|
|
145
|
+
})
|
|
146
|
+
.methods(({ object }) => ({
|
|
147
|
+
$errorGroups: object((_ctx, ...fields) => {
|
|
148
|
+
const key = errorGroupStorageKey(_ctx.stateKey, _ctx.path);
|
|
149
|
+
return (errorGroupsByPath.get(key)?.[groupKey(fields)] ?? emptyErrorGroup);
|
|
150
|
+
}),
|
|
151
|
+
}));
|
|
103
152
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.211",
|
|
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",
|