cogsbox-shape 0.5.203 → 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, validateShapeFormUpdate, type InferShapeBoxState, type ShapeRefineInfo, 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, validateShapeFormUpdate, } from "./plugin.js";
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[];
@@ -33,8 +42,10 @@ type FormUpdateParams = {
33
42
  code?: string;
34
43
  }>) => void;
35
44
  };
36
- export declare function validateShapeFormUpdate(box: ShapeSchemaBox, params: FormUpdateParams): void;
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;
37
48
  export declare function createShapePlugin<const TBox extends ShapeSchemaBox>(box: TBox): import("cogsbox-state").CogsPluginBuilder<"shape", {
38
49
  logs: boolean | undefined;
39
- }, unknown, unknown, never, {}, false, false, true, false, false, true, InferShapeBoxState<TBox>>;
50
+ }, unknown, unknown, never, {}, true, false, true, false, false, true, InferShapeBoxState<TBox>>;
40
51
  export {};
@@ -1,21 +1,17 @@
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];
3
+ function mapZodIssues(issues) {
4
+ return issues.map((issue) => ({
5
+ path: issue.path.map(String),
6
+ message: issue.message,
7
+ code: issue.code,
8
+ }));
15
9
  }
16
10
  function getRelatedFields(entry, field) {
11
+ const groupIndexes = entry.refineInfo?.fieldToGroup[field];
12
+ if (!groupIndexes?.length)
13
+ return null;
17
14
  const related = new Set([field]);
18
- const groupIndexes = entry.refineInfo?.fieldToGroup[field] ?? [];
19
15
  for (const index of groupIndexes) {
20
16
  const deps = entry.refineInfo?.groups[index]?.deps;
21
17
  if (!deps)
@@ -25,42 +21,37 @@ function getRelatedFields(entry, field) {
25
21
  }
26
22
  return related;
27
23
  }
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
- }));
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
34
  }
35
- export function validateShapeFormUpdate(box, params) {
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;
36
39
  const entry = box[params.stateKey];
37
40
  const clientSchema = entry?.schemas.client;
38
41
  if (!entry || !clientSchema)
39
42
  return;
40
- const state = params.getState();
41
43
  const field = params.path.at(-1);
42
44
  if (!field)
43
45
  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
- }
46
+ const relatedFields = getRelatedFields(entry, field);
47
+ if (!relatedFields)
53
48
  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));
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));
64
55
  }
65
56
  }
66
57
  function buildInitialState(box) {
@@ -82,10 +73,11 @@ const { createPlugin } = createPluginContext({
82
73
  export function createShapePlugin(box) {
83
74
  return createPlugin("shape")
84
75
  .initialState(() => buildInitialState(box))
76
+ .transformState((params) => wireShapeValidationOptions(box, params))
85
77
  .onFormUpdate((params) => {
86
78
  if (params.options?.logs) {
87
79
  console.log("[shape]", params.stateKey, params.path, params.event.activityType);
88
80
  }
89
- validateShapeFormUpdate(box, params);
81
+ validateShapeRefines(box, params);
90
82
  });
91
83
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.203",
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",