cogsbox-shape 0.5.201 → 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.
@@ -0,0 +1 @@
1
+ export declare function Example(): null;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Minimal end-to-end example — open and hover inside Example.
3
+ * Run from cogsbox-shape repo after: pnpm build
4
+ */
5
+ import { createCogsState } from "cogsbox-state";
6
+ import { createSchemaBox, s, schema } from "cogsbox-shape";
7
+ import { z } from "zod";
8
+ import { createShapePlugin } from "../plugin.js";
9
+ const taskItemSchema = z.object({
10
+ id: z.number(),
11
+ title: z.string(),
12
+ done: z.boolean(),
13
+ });
14
+ const taskManagerSchema = schema({
15
+ _tableName: "client",
16
+ tasks: s.clientInput({
17
+ value: [],
18
+ schema: z.array(taskItemSchema),
19
+ }),
20
+ filter: s.clientInput({
21
+ value: "all",
22
+ schema: z.string(),
23
+ }),
24
+ });
25
+ const demoBox = createSchemaBox({
26
+ taskManager: taskManagerSchema,
27
+ }, {});
28
+ const shapePlugin = createShapePlugin(demoBox);
29
+ const { useCogsState } = createCogsState({}, { plugins: [shapePlugin] });
30
+ export function Example() {
31
+ const taskManager = useCogsState("taskManager");
32
+ // hover ↓
33
+ const tasks = taskManager.tasks;
34
+ const filter = taskManager.filter;
35
+ void tasks;
36
+ void filter;
37
+ return null;
38
+ }
@@ -1 +1 @@
1
- export { createShapePlugin } 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,11 +1,40 @@
1
- type ShapeBoxEntry = {
2
- generateDefaults: () => any;
3
- stateType?: any;
4
- };
5
- type InferShapeEntryState<TEntry> = TEntry extends {
6
- stateType: infer S;
7
- } ? S : TEntry extends {
8
- generateDefaults: () => infer R;
9
- } ? R : never;
10
- export declare function createShapePlugin<const TBox extends Record<string, ShapeBoxEntry>>(box: TBox): import("cogsbox-state").CogsPluginBuilder<"shape", undefined, unknown, unknown, never, {}, false, false, false, false, false, true, { [K in keyof TBox]: InferShapeEntryState<TBox[K]>; }>;
1
+ import { z } from "zod";
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
+ };
9
+ export type ShapeSchemaBoxEntry = {
10
+ /** Field-key value map from DeriveStateType (not z.infer on a flattened client object). */
11
+ stateType: Record<string, unknown>;
12
+ generateDefaults: () => unknown;
13
+ schemas: {
14
+ client: z.ZodTypeAny;
15
+ };
16
+ refineInfo?: ShapeRefineInfo;
17
+ };
18
+ export type ShapeSchemaBox = Record<string, ShapeSchemaBoxEntry>;
19
+ /** Per-box-key state: each entry's field keys stay typed via stateType. */
20
+ export type InferShapeBoxState<TBox extends ShapeSchemaBox> = {
21
+ [K in keyof TBox]: TBox[K]["stateType"];
22
+ };
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>>;
11
40
  export {};
@@ -1,14 +1,91 @@
1
1
  import { createPluginContext } from "cogsbox-state";
2
- const { createPlugin } = createPluginContext();
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
+ }
66
+ function buildInitialState(box) {
67
+ const state = {};
68
+ for (const key of Object.keys(box)) {
69
+ const entry = box[key];
70
+ if (!entry)
71
+ continue;
72
+ state[key] =
73
+ entry.generateDefaults();
74
+ }
75
+ return state;
76
+ }
77
+ const { createPlugin } = createPluginContext({
78
+ options: z.object({
79
+ logs: z.boolean().optional(),
80
+ }),
81
+ });
3
82
  export function createShapePlugin(box) {
4
- return createPlugin("shape").initialState(() => {
5
- const state = {};
6
- for (const key of Object.keys(box)) {
7
- const entry = box[key];
8
- if (!entry)
9
- continue;
10
- state[key] = entry.generateDefaults();
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);
11
88
  }
12
- return state;
89
+ validateShapeFormUpdate(box, params);
13
90
  });
14
91
  }
package/dist/schema.d.ts CHANGED
@@ -179,9 +179,28 @@ export type Reference<TGetter extends () => any> = {
179
179
  getter: TGetter;
180
180
  };
181
181
  interface ShapeAPI {
182
- clientInput: <const TValue>(value: TValue | ((tools: {
182
+ clientInput<const TValue, const TSchema extends z.ZodTypeAny>(options: {
183
+ value: TValue | ((tools: {
184
+ uuid: () => string;
185
+ }) => TValue);
186
+ schema: TSchema | ((base: ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>) => TSchema);
187
+ clientPk?: boolean | ((val: any) => boolean);
188
+ }): Builder<"clientInput", null, z.ZodUndefined, TValue extends () => infer R ? R : TValue, TSchema, TSchema>;
189
+ clientInput<const TValue>(options: {
190
+ value: TValue | ((tools: {
191
+ uuid: () => string;
192
+ }) => TValue);
193
+ schema?: never;
194
+ clientPk?: boolean | ((val: any) => boolean);
195
+ }): Builder<"clientInput", null, z.ZodUndefined, TValue extends () => infer R ? R : TValue, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>>;
196
+ clientInput<const TSchema extends z.ZodTypeAny>(options: {
197
+ value?: never;
198
+ schema: TSchema;
199
+ clientPk?: boolean | ((val: any) => boolean);
200
+ }): Builder<"clientInput", null, z.ZodUndefined, z.infer<TSchema>, TSchema, TSchema>;
201
+ clientInput<const TValue>(value: TValue | ((tools: {
183
202
  uuid: () => string;
184
- }) => TValue)) => Builder<"clientInput", null, z.ZodUndefined, TValue extends () => infer R ? R : TValue, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>>;
203
+ }) => TValue)): Builder<"clientInput", null, z.ZodUndefined, TValue extends () => infer R ? R : TValue, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>>;
185
204
  sqlite: <const T extends SQLTypeInput>(sqlConfig: T) => Builder<"sql", WithDialect<T, "sqlite">, SQLToZodType<T, false>, z.infer<SQLToZodType<T, false>>, SQLToZodType<T, false>, SQLToZodType<T, false>>;
186
205
  postgres: <const T extends SQLTypeInput>(sqlConfig: T) => Builder<"sql", WithDialect<T, "postgres">, SQLToZodType<T, false>, z.infer<SQLToZodType<T, false>>, SQLToZodType<T, false>, SQLToZodType<T, false>>;
187
206
  mysql: <const T extends SQLTypeInput>(sqlConfig: T) => Builder<"sql", WithDialect<T, "mysql">, SQLToZodType<T, false>, z.infer<SQLToZodType<T, false>>, SQLToZodType<T, false>, SQLToZodType<T, false>>;
package/dist/schema.js CHANGED
@@ -49,8 +49,29 @@ function createSqlBuilder(dialect, sqlConfig) {
49
49
  validationZod: sqlZodType,
50
50
  });
51
51
  }
52
+ function isClientInputOptions(value) {
53
+ return (value !== undefined &&
54
+ typeof value === "object" &&
55
+ value !== null &&
56
+ !isFunction(value) &&
57
+ !("_def" in value) &&
58
+ !("parse" in value) &&
59
+ ("value" in value || "schema" in value || "clientPk" in value));
60
+ }
52
61
  export const s = {
53
- clientInput: (value) => {
62
+ clientInput: (...args) => {
63
+ const first = args[0];
64
+ if (isClientInputOptions(first)) {
65
+ return createBuilder({
66
+ stage: "sql",
67
+ sqlConfig: null,
68
+ sqlZod: z.undefined(),
69
+ initialValue: undefined,
70
+ clientZod: z.undefined(),
71
+ validationZod: z.undefined(),
72
+ }).clientInput(first);
73
+ }
74
+ const value = first;
54
75
  const sample = isFunction(value) ? value({ uuid }) : value;
55
76
  let inferredZodType;
56
77
  if (typeof sample === "string") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.201",
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",