cogsbox-shape 0.5.202 → 0.5.204
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, validateShapeRefines, wireShapeValidationOptions, type InferShapeBoxState, type ShapeRefineInfo, type ShapeSchemaBox, type ShapeSchemaBoxEntry, } from "./plugin.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { createShapePlugin, } from "./plugin.js";
|
|
1
|
+
export { createShapePlugin, validateShapeRefines, wireShapeValidationOptions, } from "./plugin.js";
|
|
@@ -1,12 +1,51 @@
|
|
|
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 TransformStateParams = {
|
|
24
|
+
stateKey: string;
|
|
25
|
+
setOptions: (options: {
|
|
26
|
+
validation?: {
|
|
27
|
+
zodSchemaV4?: z.ZodTypeAny;
|
|
28
|
+
onBlur?: "error" | "warning";
|
|
29
|
+
};
|
|
30
|
+
}) => void;
|
|
31
|
+
};
|
|
32
|
+
type FormUpdateParams = {
|
|
33
|
+
stateKey: string;
|
|
34
|
+
path: string[];
|
|
35
|
+
event: {
|
|
36
|
+
activityType: string;
|
|
37
|
+
};
|
|
38
|
+
getState: () => unknown;
|
|
39
|
+
addZodErrors: (errors: Array<{
|
|
40
|
+
path: string[];
|
|
41
|
+
message: string;
|
|
42
|
+
code?: string;
|
|
43
|
+
}>) => void;
|
|
44
|
+
};
|
|
45
|
+
export declare function wireShapeValidationOptions(box: ShapeSchemaBox, params: TransformStateParams): void;
|
|
46
|
+
/** Cross-field refine errors only — field rules are handled by state via setOptions. */
|
|
47
|
+
export declare function validateShapeRefines(box: ShapeSchemaBox, params: FormUpdateParams): void;
|
|
48
|
+
export declare function createShapePlugin<const TBox extends ShapeSchemaBox>(box: TBox): import("cogsbox-state").CogsPluginBuilder<"shape", {
|
|
49
|
+
logs: boolean | undefined;
|
|
50
|
+
}, unknown, unknown, never, {}, true, false, true, false, false, true, InferShapeBoxState<TBox>>;
|
|
51
|
+
export {};
|
|
@@ -1,15 +1,83 @@
|
|
|
1
1
|
import { createPluginContext } from "cogsbox-state";
|
|
2
|
-
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
function mapZodIssues(issues) {
|
|
4
|
+
return issues.map((issue) => ({
|
|
5
|
+
path: issue.path.map(String),
|
|
6
|
+
message: issue.message,
|
|
7
|
+
code: issue.code,
|
|
8
|
+
}));
|
|
9
|
+
}
|
|
10
|
+
function getRelatedFields(entry, field) {
|
|
11
|
+
const groupIndexes = entry.refineInfo?.fieldToGroup[field];
|
|
12
|
+
if (!groupIndexes?.length)
|
|
13
|
+
return null;
|
|
14
|
+
const related = new Set([field]);
|
|
15
|
+
for (const index of groupIndexes) {
|
|
16
|
+
const deps = entry.refineInfo?.groups[index]?.deps;
|
|
17
|
+
if (!deps)
|
|
18
|
+
continue;
|
|
19
|
+
for (const dep of deps)
|
|
20
|
+
related.add(dep);
|
|
21
|
+
}
|
|
22
|
+
return related;
|
|
23
|
+
}
|
|
24
|
+
export function wireShapeValidationOptions(box, params) {
|
|
25
|
+
const entry = box[params.stateKey];
|
|
26
|
+
if (!entry)
|
|
27
|
+
return;
|
|
28
|
+
params.setOptions({
|
|
29
|
+
validation: {
|
|
30
|
+
zodSchemaV4: entry.schemas.client,
|
|
31
|
+
onBlur: "error",
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/** Cross-field refine errors only — field rules are handled by state via setOptions. */
|
|
36
|
+
export function validateShapeRefines(box, params) {
|
|
37
|
+
if (params.event.activityType !== "blur")
|
|
38
|
+
return;
|
|
39
|
+
const entry = box[params.stateKey];
|
|
40
|
+
const clientSchema = entry?.schemas.client;
|
|
41
|
+
if (!entry || !clientSchema)
|
|
42
|
+
return;
|
|
43
|
+
const field = params.path.at(-1);
|
|
44
|
+
if (!field)
|
|
45
|
+
return;
|
|
46
|
+
const relatedFields = getRelatedFields(entry, field);
|
|
47
|
+
if (!relatedFields)
|
|
48
|
+
return;
|
|
49
|
+
const result = clientSchema.safeParse(params.getState());
|
|
50
|
+
if (result.success)
|
|
51
|
+
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));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
3
57
|
function buildInitialState(box) {
|
|
4
58
|
const state = {};
|
|
5
59
|
for (const key of Object.keys(box)) {
|
|
6
60
|
const entry = box[key];
|
|
7
61
|
if (!entry)
|
|
8
62
|
continue;
|
|
9
|
-
state[key] =
|
|
63
|
+
state[key] =
|
|
64
|
+
entry.generateDefaults();
|
|
10
65
|
}
|
|
11
66
|
return state;
|
|
12
67
|
}
|
|
68
|
+
const { createPlugin } = createPluginContext({
|
|
69
|
+
options: z.object({
|
|
70
|
+
logs: z.boolean().optional(),
|
|
71
|
+
}),
|
|
72
|
+
});
|
|
13
73
|
export function createShapePlugin(box) {
|
|
14
|
-
return createPlugin("shape")
|
|
74
|
+
return createPlugin("shape")
|
|
75
|
+
.initialState(() => buildInitialState(box))
|
|
76
|
+
.transformState((params) => wireShapeValidationOptions(box, params))
|
|
77
|
+
.onFormUpdate((params) => {
|
|
78
|
+
if (params.options?.logs) {
|
|
79
|
+
console.log("[shape]", params.stateKey, params.path, params.event.activityType);
|
|
80
|
+
}
|
|
81
|
+
validateShapeRefines(box, params);
|
|
82
|
+
});
|
|
15
83
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.204",
|
|
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",
|