cogsbox-shape 0.5.216 → 0.5.218
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,4 +1,3 @@
|
|
|
1
|
-
import type { ChainMethodContext } from "cogsbox-state";
|
|
2
1
|
import { z } from "zod";
|
|
3
2
|
/** Minimal shape of a createSchemaBox entry — matches journalSchemaBox.journalTechnical etc. */
|
|
4
3
|
export type ShapeRefineInfo = {
|
|
@@ -80,19 +79,19 @@ export declare function validateShapeKeys(box: ShapeSchemaBox, params: ShapeKeyV
|
|
|
80
79
|
key: string;
|
|
81
80
|
path: string[];
|
|
82
81
|
success: boolean;
|
|
83
|
-
data:
|
|
82
|
+
data: unknown;
|
|
84
83
|
}[];
|
|
85
84
|
};
|
|
86
85
|
export declare function createShapePlugin<const TBox extends ShapeSchemaBox>(box: TBox): import("cogsbox-state").CogsPluginBuilder<"shape", {
|
|
87
86
|
logs: boolean | undefined;
|
|
88
87
|
}, unknown, unknown, never, {
|
|
89
|
-
validateGroup: import("cogsbox-state").ChainMethodDefinition<(ctx: ChainMethodContext<any, any>, keys?: readonly string[] | undefined) => {
|
|
88
|
+
validateGroup: import("cogsbox-state").ChainMethodDefinition<(ctx: import("cogsbox-state").ChainMethodContext<any, any>, keys?: readonly string[] | undefined) => {
|
|
90
89
|
success: boolean;
|
|
91
90
|
results: {
|
|
92
91
|
key: string;
|
|
93
92
|
path: string[];
|
|
94
93
|
success: boolean;
|
|
95
|
-
data:
|
|
94
|
+
data: unknown;
|
|
96
95
|
}[];
|
|
97
96
|
}>;
|
|
98
97
|
}, true, true, true, true, false, true, InferShapeBoxState<TBox>>;
|
|
@@ -3,6 +3,15 @@ import { z } from "zod";
|
|
|
3
3
|
function pathKey(path) {
|
|
4
4
|
return path.join("\0");
|
|
5
5
|
}
|
|
6
|
+
function getValueAtPath(value, path) {
|
|
7
|
+
let cursor = value;
|
|
8
|
+
for (const segment of path) {
|
|
9
|
+
if (cursor === null || typeof cursor !== "object")
|
|
10
|
+
return undefined;
|
|
11
|
+
cursor = cursor[segment];
|
|
12
|
+
}
|
|
13
|
+
return cursor;
|
|
14
|
+
}
|
|
6
15
|
function resolveRelatedPaths(blurPath, relatedFields) {
|
|
7
16
|
const parent = blurPath.slice(0, -1);
|
|
8
17
|
return [...relatedFields].map((field) => [...parent, field]);
|
|
@@ -108,31 +117,6 @@ function addValidationIssues(params, issues) {
|
|
|
108
117
|
params.addZodErrors(issues);
|
|
109
118
|
notifyValidationPaths(params.stateKey, issues.map((issue) => issue.path));
|
|
110
119
|
}
|
|
111
|
-
function setValidationIssues(stateKey, issues) {
|
|
112
|
-
if (issues.length === 0)
|
|
113
|
-
return;
|
|
114
|
-
const store = getGlobalStore.getState();
|
|
115
|
-
for (const issue of issues) {
|
|
116
|
-
const currentMeta = store.getShadowMetadata(stateKey, issue.path) || {};
|
|
117
|
-
store.setShadowMetadata(stateKey, issue.path, {
|
|
118
|
-
...currentMeta,
|
|
119
|
-
validation: {
|
|
120
|
-
status: "INVALID",
|
|
121
|
-
errors: [
|
|
122
|
-
{
|
|
123
|
-
source: "client",
|
|
124
|
-
message: issue.message,
|
|
125
|
-
severity: "error",
|
|
126
|
-
code: issue.code,
|
|
127
|
-
},
|
|
128
|
-
],
|
|
129
|
-
lastValidated: Date.now(),
|
|
130
|
-
validatedValue: store.getShadowValue(stateKey, issue.path),
|
|
131
|
-
},
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
notifyValidationPaths(stateKey, issues.map((issue) => issue.path));
|
|
135
|
-
}
|
|
136
120
|
function issueMatchesSelectedKeys(issue, parentPath, selectedKeys) {
|
|
137
121
|
const issuePath = issue.path.map(String);
|
|
138
122
|
if (issuePath.length <= parentPath.length)
|
|
@@ -276,27 +260,17 @@ export function validateShapeKeys(box, params) {
|
|
|
276
260
|
const clientSchema = entry?.validators?.client ?? entry?.schemas.client;
|
|
277
261
|
if (!entry || !clientSchema)
|
|
278
262
|
return { success: true, results: [] };
|
|
279
|
-
const
|
|
280
|
-
const rootState = params.getState?.() ?? store.getShadowValue(params.stateKey, []);
|
|
263
|
+
const rootState = params.getState?.() ?? getGlobalStore.getState().getShadowValue(params.stateKey, []);
|
|
281
264
|
const result = clientSchema.safeParse(rootState);
|
|
282
265
|
const selectedKeys = params.keys ? new Set(params.keys) : null;
|
|
283
|
-
const targetPaths = params.keys?.map((key) => [...params.path, key]) ??
|
|
284
|
-
(result.success
|
|
285
|
-
? []
|
|
286
|
-
: mapZodIssues(result.error.issues).map((issue) => issue.path));
|
|
287
266
|
if (result.success) {
|
|
288
|
-
clearValidationPaths({
|
|
289
|
-
stateKey: params.stateKey,
|
|
290
|
-
getState: () => rootState,
|
|
291
|
-
addZodErrors: () => { },
|
|
292
|
-
}, targetPaths);
|
|
293
267
|
return {
|
|
294
268
|
success: true,
|
|
295
269
|
results: params.keys?.map((key) => ({
|
|
296
270
|
key,
|
|
297
271
|
path: [...params.path, key],
|
|
298
272
|
success: true,
|
|
299
|
-
data:
|
|
273
|
+
data: getValueAtPath(rootState, [...params.path, key]),
|
|
300
274
|
})) ?? [],
|
|
301
275
|
};
|
|
302
276
|
}
|
|
@@ -304,14 +278,6 @@ export function validateShapeKeys(box, params) {
|
|
|
304
278
|
? result.error.issues.filter((issue) => issueMatchesSelectedKeys(issue, params.path, selectedKeys))
|
|
305
279
|
: result.error.issues;
|
|
306
280
|
const mapped = mapZodIssues(issues);
|
|
307
|
-
const activeKeys = new Set(mapped.map((issue) => pathKey(issue.path)));
|
|
308
|
-
const stalePaths = targetPaths.filter((targetPath) => !activeKeys.has(pathKey(targetPath)));
|
|
309
|
-
clearValidationPaths({
|
|
310
|
-
stateKey: params.stateKey,
|
|
311
|
-
getState: () => rootState,
|
|
312
|
-
addZodErrors: () => { },
|
|
313
|
-
}, stalePaths);
|
|
314
|
-
setValidationIssues(params.stateKey, mapped);
|
|
315
281
|
return {
|
|
316
282
|
success: mapped.length === 0,
|
|
317
283
|
results: params.keys?.map((key) => {
|
|
@@ -322,7 +288,7 @@ export function validateShapeKeys(box, params) {
|
|
|
322
288
|
path: keyPath,
|
|
323
289
|
success: keyIssues.length === 0,
|
|
324
290
|
data: keyIssues.length === 0
|
|
325
|
-
?
|
|
291
|
+
? getValueAtPath(rootState, keyPath)
|
|
326
292
|
: undefined,
|
|
327
293
|
error: keyIssues.length === 0 ? undefined : { issues: keyIssues },
|
|
328
294
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.218",
|
|
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",
|