cogsbox-shape 0.5.203 → 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.
- package/cogsbox-shape-state/dist/index.d.ts +1 -1
- package/cogsbox-shape-state/dist/index.js +1 -1
- package/cogsbox-shape-state/dist/plugin.d.ts +14 -2
- package/cogsbox-shape-state/dist/plugin.js +51 -39
- package/dist/schema.js +1 -1
- package/dist/vitest/refineRuntime.test.js +32 -0
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
export { createShapePlugin,
|
|
1
|
+
export { createShapePlugin, validateShapeRefines, wireShapeValidationOptions, type InferShapeBoxState, type ShapeRefineInfo, type ShapeSchemaBox, type ShapeSchemaBoxEntry, } from "./plugin.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { createShapePlugin,
|
|
1
|
+
export { createShapePlugin, validateShapeRefines, wireShapeValidationOptions, } from "./plugin.js";
|
|
@@ -20,6 +20,15 @@ export type ShapeSchemaBox = Record<string, ShapeSchemaBoxEntry>;
|
|
|
20
20
|
export type InferShapeBoxState<TBox extends ShapeSchemaBox> = {
|
|
21
21
|
[K in keyof TBox]: TBox[K]["stateType"];
|
|
22
22
|
};
|
|
23
|
+
type TransformStateParams = {
|
|
24
|
+
stateKey: string;
|
|
25
|
+
setOptions: (options: {
|
|
26
|
+
validation?: {
|
|
27
|
+
zodSchemaV4?: z.ZodTypeAny;
|
|
28
|
+
onBlur?: "error" | "warning";
|
|
29
|
+
};
|
|
30
|
+
}) => void;
|
|
31
|
+
};
|
|
23
32
|
type FormUpdateParams = {
|
|
24
33
|
stateKey: string;
|
|
25
34
|
path: string[];
|
|
@@ -32,9 +41,12 @@ type FormUpdateParams = {
|
|
|
32
41
|
message: string;
|
|
33
42
|
code?: string;
|
|
34
43
|
}>) => void;
|
|
44
|
+
clearZodErrors: (paths: string[][]) => void;
|
|
35
45
|
};
|
|
36
|
-
export declare function
|
|
46
|
+
export declare function wireShapeValidationOptions(box: ShapeSchemaBox, params: TransformStateParams): void;
|
|
47
|
+
/** Cross-field refine errors only — field rules are handled by state via setOptions. */
|
|
48
|
+
export declare function validateShapeRefines(box: ShapeSchemaBox, params: FormUpdateParams): void;
|
|
37
49
|
export declare function createShapePlugin<const TBox extends ShapeSchemaBox>(box: TBox): import("cogsbox-state").CogsPluginBuilder<"shape", {
|
|
38
50
|
logs: boolean | undefined;
|
|
39
|
-
}, unknown, unknown, never, {},
|
|
51
|
+
}, unknown, unknown, never, {}, true, false, true, false, false, true, InferShapeBoxState<TBox>>;
|
|
40
52
|
export {};
|
|
@@ -1,21 +1,24 @@
|
|
|
1
1
|
import { createPluginContext } from "cogsbox-state";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
function
|
|
4
|
-
return path.
|
|
5
|
-
if (current !== null && typeof current === "object") {
|
|
6
|
-
return current[key];
|
|
7
|
-
}
|
|
8
|
-
return undefined;
|
|
9
|
-
}, state);
|
|
3
|
+
function pathKey(path) {
|
|
4
|
+
return path.join("\0");
|
|
10
5
|
}
|
|
11
|
-
function
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
6
|
+
function resolveRelatedPaths(blurPath, relatedFields) {
|
|
7
|
+
const parent = blurPath.slice(0, -1);
|
|
8
|
+
return [...relatedFields].map((field) => [...parent, field]);
|
|
9
|
+
}
|
|
10
|
+
function mapZodIssues(issues) {
|
|
11
|
+
return issues.map((issue) => ({
|
|
12
|
+
path: issue.path.map(String),
|
|
13
|
+
message: issue.message,
|
|
14
|
+
code: issue.code,
|
|
15
|
+
}));
|
|
15
16
|
}
|
|
16
17
|
function getRelatedFields(entry, field) {
|
|
18
|
+
const groupIndexes = entry.refineInfo?.fieldToGroup[field];
|
|
19
|
+
if (!groupIndexes?.length)
|
|
20
|
+
return null;
|
|
17
21
|
const related = new Set([field]);
|
|
18
|
-
const groupIndexes = entry.refineInfo?.fieldToGroup[field] ?? [];
|
|
19
22
|
for (const index of groupIndexes) {
|
|
20
23
|
const deps = entry.refineInfo?.groups[index]?.deps;
|
|
21
24
|
if (!deps)
|
|
@@ -25,42 +28,50 @@ function getRelatedFields(entry, field) {
|
|
|
25
28
|
}
|
|
26
29
|
return related;
|
|
27
30
|
}
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
message: issue.message,
|
|
32
|
-
code: issue.code,
|
|
33
|
-
}));
|
|
31
|
+
function issueMatchesRelatedFields(issue, relatedFields) {
|
|
32
|
+
const leaf = String(issue.path.at(-1) ?? "");
|
|
33
|
+
return relatedFields.has(leaf);
|
|
34
34
|
}
|
|
35
|
-
export function
|
|
35
|
+
export function wireShapeValidationOptions(box, params) {
|
|
36
|
+
const entry = box[params.stateKey];
|
|
37
|
+
if (!entry)
|
|
38
|
+
return;
|
|
39
|
+
params.setOptions({
|
|
40
|
+
validation: {
|
|
41
|
+
zodSchemaV4: entry.schemas.client,
|
|
42
|
+
onBlur: "error",
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/** Cross-field refine errors only — field rules are handled by state via setOptions. */
|
|
47
|
+
export function validateShapeRefines(box, params) {
|
|
48
|
+
if (params.event.activityType !== "blur")
|
|
49
|
+
return;
|
|
36
50
|
const entry = box[params.stateKey];
|
|
37
51
|
const clientSchema = entry?.schemas.client;
|
|
38
52
|
if (!entry || !clientSchema)
|
|
39
53
|
return;
|
|
40
|
-
const state = params.getState();
|
|
41
54
|
const field = params.path.at(-1);
|
|
42
55
|
if (!field)
|
|
43
56
|
return;
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
}
|
|
57
|
+
const relatedFields = getRelatedFields(entry, field);
|
|
58
|
+
if (!relatedFields)
|
|
53
59
|
return;
|
|
60
|
+
const relatedPaths = resolveRelatedPaths(params.path, relatedFields);
|
|
61
|
+
const result = clientSchema.safeParse(params.getState());
|
|
62
|
+
if (result.success) {
|
|
63
|
+
params.clearZodErrors(relatedPaths);
|
|
64
|
+
return;
|
|
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);
|
|
54
72
|
}
|
|
55
|
-
if (
|
|
56
|
-
|
|
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));
|
|
73
|
+
if (mapped.length > 0) {
|
|
74
|
+
params.addZodErrors(mapped);
|
|
64
75
|
}
|
|
65
76
|
}
|
|
66
77
|
function buildInitialState(box) {
|
|
@@ -82,10 +93,11 @@ const { createPlugin } = createPluginContext({
|
|
|
82
93
|
export function createShapePlugin(box) {
|
|
83
94
|
return createPlugin("shape")
|
|
84
95
|
.initialState(() => buildInitialState(box))
|
|
96
|
+
.transformState((params) => wireShapeValidationOptions(box, params))
|
|
85
97
|
.onFormUpdate((params) => {
|
|
86
98
|
if (params.options?.logs) {
|
|
87
99
|
console.log("[shape]", params.stateKey, params.path, params.event.activityType);
|
|
88
100
|
}
|
|
89
|
-
|
|
101
|
+
validateShapeRefines(box, params);
|
|
90
102
|
});
|
|
91
103
|
}
|
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
|
|
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.
|
|
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",
|