cogsbox-shape 0.5.202 → 0.5.203
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 +1 @@
|
|
|
1
|
-
export { createShapePlugin, type InferShapeBoxState, type ShapeSchemaBox, type ShapeSchemaBoxEntry, } from "./plugin.js";
|
|
1
|
+
export { createShapePlugin, validateShapeFormUpdate, type InferShapeBoxState, type ShapeRefineInfo, type ShapeSchemaBox, type ShapeSchemaBoxEntry, } from "./plugin.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { createShapePlugin, } from "./plugin.js";
|
|
1
|
+
export { createShapePlugin, validateShapeFormUpdate, } from "./plugin.js";
|
|
@@ -1,12 +1,40 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
1
2
|
/** Minimal shape of a createSchemaBox entry — matches journalSchemaBox.journalTechnical etc. */
|
|
3
|
+
export type ShapeRefineInfo = {
|
|
4
|
+
fieldToGroup: Record<string, number[]>;
|
|
5
|
+
groups: {
|
|
6
|
+
deps: string[] | null;
|
|
7
|
+
}[];
|
|
8
|
+
};
|
|
2
9
|
export type ShapeSchemaBoxEntry = {
|
|
3
10
|
/** Field-key → value map from DeriveStateType (not z.infer on a flattened client object). */
|
|
4
11
|
stateType: Record<string, unknown>;
|
|
5
12
|
generateDefaults: () => unknown;
|
|
13
|
+
schemas: {
|
|
14
|
+
client: z.ZodTypeAny;
|
|
15
|
+
};
|
|
16
|
+
refineInfo?: ShapeRefineInfo;
|
|
6
17
|
};
|
|
7
18
|
export type ShapeSchemaBox = Record<string, ShapeSchemaBoxEntry>;
|
|
8
19
|
/** Per-box-key state: each entry's field keys stay typed via stateType. */
|
|
9
20
|
export type InferShapeBoxState<TBox extends ShapeSchemaBox> = {
|
|
10
21
|
[K in keyof TBox]: TBox[K]["stateType"];
|
|
11
22
|
};
|
|
12
|
-
|
|
23
|
+
type FormUpdateParams = {
|
|
24
|
+
stateKey: string;
|
|
25
|
+
path: string[];
|
|
26
|
+
event: {
|
|
27
|
+
activityType: string;
|
|
28
|
+
};
|
|
29
|
+
getState: () => unknown;
|
|
30
|
+
addZodErrors: (errors: Array<{
|
|
31
|
+
path: string[];
|
|
32
|
+
message: string;
|
|
33
|
+
code?: string;
|
|
34
|
+
}>) => void;
|
|
35
|
+
};
|
|
36
|
+
export declare function validateShapeFormUpdate(box: ShapeSchemaBox, params: FormUpdateParams): void;
|
|
37
|
+
export declare function createShapePlugin<const TBox extends ShapeSchemaBox>(box: TBox): import("cogsbox-state").CogsPluginBuilder<"shape", {
|
|
38
|
+
logs: boolean | undefined;
|
|
39
|
+
}, unknown, unknown, never, {}, false, false, true, false, false, true, InferShapeBoxState<TBox>>;
|
|
40
|
+
export {};
|
|
@@ -1,15 +1,91 @@
|
|
|
1
1
|
import { createPluginContext } from "cogsbox-state";
|
|
2
|
-
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
function getValueAtPath(state, path) {
|
|
4
|
+
return path.reduce((current, key) => {
|
|
5
|
+
if (current !== null && typeof current === "object") {
|
|
6
|
+
return current[key];
|
|
7
|
+
}
|
|
8
|
+
return undefined;
|
|
9
|
+
}, state);
|
|
10
|
+
}
|
|
11
|
+
function getClientFieldSchema(clientSchema, field) {
|
|
12
|
+
const shape = clientSchema
|
|
13
|
+
.shape;
|
|
14
|
+
return shape?.[field];
|
|
15
|
+
}
|
|
16
|
+
function getRelatedFields(entry, field) {
|
|
17
|
+
const related = new Set([field]);
|
|
18
|
+
const groupIndexes = entry.refineInfo?.fieldToGroup[field] ?? [];
|
|
19
|
+
for (const index of groupIndexes) {
|
|
20
|
+
const deps = entry.refineInfo?.groups[index]?.deps;
|
|
21
|
+
if (!deps)
|
|
22
|
+
continue;
|
|
23
|
+
for (const dep of deps)
|
|
24
|
+
related.add(dep);
|
|
25
|
+
}
|
|
26
|
+
return related;
|
|
27
|
+
}
|
|
28
|
+
function mapZodIssues(issues, pathPrefix = []) {
|
|
29
|
+
return issues.map((issue) => ({
|
|
30
|
+
path: [...pathPrefix, ...issue.path.map(String)],
|
|
31
|
+
message: issue.message,
|
|
32
|
+
code: issue.code,
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
export function validateShapeFormUpdate(box, params) {
|
|
36
|
+
const entry = box[params.stateKey];
|
|
37
|
+
const clientSchema = entry?.schemas.client;
|
|
38
|
+
if (!entry || !clientSchema)
|
|
39
|
+
return;
|
|
40
|
+
const state = params.getState();
|
|
41
|
+
const field = params.path.at(-1);
|
|
42
|
+
if (!field)
|
|
43
|
+
return;
|
|
44
|
+
if (params.event.activityType === "blur") {
|
|
45
|
+
const result = clientSchema.safeParse(state);
|
|
46
|
+
if (result.success)
|
|
47
|
+
return;
|
|
48
|
+
const relatedFields = getRelatedFields(entry, field);
|
|
49
|
+
const issues = result.error.issues.filter((issue) => relatedFields.has(String(issue.path[0])));
|
|
50
|
+
if (issues.length > 0) {
|
|
51
|
+
params.addZodErrors(mapZodIssues(issues));
|
|
52
|
+
}
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (params.event.activityType === "input") {
|
|
56
|
+
const fieldSchema = getClientFieldSchema(clientSchema, field);
|
|
57
|
+
if (!fieldSchema)
|
|
58
|
+
return;
|
|
59
|
+
const value = getValueAtPath(state, params.path);
|
|
60
|
+
const result = fieldSchema.safeParse(value);
|
|
61
|
+
if (result.success)
|
|
62
|
+
return;
|
|
63
|
+
params.addZodErrors(mapZodIssues(result.error.issues, params.path));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
3
66
|
function buildInitialState(box) {
|
|
4
67
|
const state = {};
|
|
5
68
|
for (const key of Object.keys(box)) {
|
|
6
69
|
const entry = box[key];
|
|
7
70
|
if (!entry)
|
|
8
71
|
continue;
|
|
9
|
-
state[key] =
|
|
72
|
+
state[key] =
|
|
73
|
+
entry.generateDefaults();
|
|
10
74
|
}
|
|
11
75
|
return state;
|
|
12
76
|
}
|
|
77
|
+
const { createPlugin } = createPluginContext({
|
|
78
|
+
options: z.object({
|
|
79
|
+
logs: z.boolean().optional(),
|
|
80
|
+
}),
|
|
81
|
+
});
|
|
13
82
|
export function createShapePlugin(box) {
|
|
14
|
-
return createPlugin("shape")
|
|
83
|
+
return createPlugin("shape")
|
|
84
|
+
.initialState(() => buildInitialState(box))
|
|
85
|
+
.onFormUpdate((params) => {
|
|
86
|
+
if (params.options?.logs) {
|
|
87
|
+
console.log("[shape]", params.stateKey, params.path, params.event.activityType);
|
|
88
|
+
}
|
|
89
|
+
validateShapeFormUpdate(box, params);
|
|
90
|
+
});
|
|
15
91
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.203",
|
|
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",
|