cogsbox-shape 0.5.99 → 0.5.101

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/dist/schema.d.ts CHANGED
@@ -36,24 +36,18 @@ type SQLToZodType<T extends SQLType, TDefault extends boolean> = T["pk"] extends
36
36
  default: "CURRENT_TIMESTAMP";
37
37
  } ? TDefault extends true ? never : z.ZodDate : z.ZodDate : never;
38
38
  type ZodTypeFromPrimitive<T> = T extends string ? z.ZodString : T extends number ? z.ZodNumber : T extends boolean ? z.ZodBoolean : T extends Date ? z.ZodDate : z.ZodAny;
39
- type IsLiteralType<T> = T extends string ? string extends T ? false : true : T extends number ? number extends T ? false : true : T extends boolean ? boolean extends T ? false : true : false;
39
+ type CollapsedUnion<A extends z.ZodTypeAny, B extends z.ZodTypeAny> = A extends B ? (B extends A ? A : z.ZodUnion<[A, B]>) : z.ZodUnion<[A, B]>;
40
40
  export interface IBuilderMethods<T extends SQLType | RelationConfig<any>, TSql extends z.ZodTypeAny, TNew extends z.ZodTypeAny, TInitialValue, TClient extends z.ZodTypeAny, TValidation extends z.ZodTypeAny> {
41
41
  initialState: {
42
- <const TResult>(defaultValue: TResult): TResult extends () => infer R ? R extends z.ZodTypeAny ? Prettify<Builder<"new", T, TSql, R, z.infer<R>, InferSmartClientType<TSql, R>, InferSmartClientType<TSql, R>>> : Prettify<Builder<"new", T, TSql, z.ZodLiteral<R>, R, z.ZodUnion<[TSql, z.ZodLiteral<R>]>, z.ZodUnion<[TSql, z.ZodLiteral<R>]>>> : TResult extends z.ZodTypeAny ? Prettify<Builder<"new", T, TSql, TResult, z.infer<TResult>, InferSmartClientType<TSql, TResult>, InferSmartClientType<TSql, TResult>>> : TResult extends string | number | boolean ? Prettify<Builder<"new", T, TSql, z.ZodLiteral<TResult>, TResult, z.ZodUnion<[TSql, z.ZodLiteral<TResult>]>, z.ZodUnion<[TSql, z.ZodLiteral<TResult>]>>> : Prettify<Builder<"new", T, TSql, ZodTypeFromPrimitive<TResult>, TResult, InferSmartClientType<TSql, ZodTypeFromPrimitive<TResult>>, InferSmartClientType<TSql, ZodTypeFromPrimitive<TResult>>>>;
43
- <TNewNext extends z.ZodTypeAny, const TDefaultNext>(schema: ((tools: {
44
- sql: TSql;
45
- }) => TNewNext) | TNewNext, defaultValue: TDefaultNext): Prettify<Builder<"new", T, TSql, z.ZodUnion<[
46
- TNewNext,
47
- z.ZodLiteral<TDefaultNext extends () => infer R ? R : TDefaultNext>
48
- ]>, IsLiteralType<z.infer<TNewNext>> extends true ? TDefaultNext extends () => infer R ? R : TDefaultNext : z.infer<TNewNext>, (TDefaultNext extends () => infer R ? R : TDefaultNext) extends z.infer<TNewNext> ? z.ZodUnion<[TSql, TNewNext]> : z.ZodUnion<[
49
- TSql,
50
- TNewNext,
51
- z.ZodLiteral<TDefaultNext extends () => infer R ? R : TDefaultNext>
52
- ]>, (TDefaultNext extends () => infer R ? R : TDefaultNext) extends z.infer<TNewNext> ? z.ZodUnion<[TSql, TNewNext]> : z.ZodUnion<[
53
- TSql,
54
- TNewNext,
55
- z.ZodLiteral<TDefaultNext extends () => infer R ? R : TDefaultNext>
56
- ]>>>;
42
+ <const TValue>(value: TValue extends (...args: any[]) => void | undefined ? never : TValue): TValue extends (...args: any[]) => infer R ? R extends void | undefined ? never : TValue extends z.ZodTypeAny ? Prettify<Builder<"new", T, TSql, TValue, z.infer<TValue>, CollapsedUnion<TSql, TValue>, // <-- FIX
43
+ CollapsedUnion<TSql, TValue>>> : R extends string | number | boolean ? Prettify<Builder<"new", T, TSql, z.ZodLiteral<R>, R, CollapsedUnion<TSql, z.ZodLiteral<R>>, // <-- FIX
44
+ CollapsedUnion<TSql, z.ZodLiteral<R>>>> : Prettify<Builder<"new", T, TSql, ZodTypeFromPrimitive<R>, R, CollapsedUnion<TSql, ZodTypeFromPrimitive<R>>, // <-- FIX
45
+ CollapsedUnion<TSql, ZodTypeFromPrimitive<R>>>> : TValue extends z.ZodTypeAny ? Prettify<Builder<"new", T, TSql, TValue, z.infer<TValue>, CollapsedUnion<TSql, TValue>, // <-- FIX
46
+ CollapsedUnion<TSql, TValue>>> : TValue extends string | number | boolean ? Prettify<Builder<"new", T, TSql, z.ZodLiteral<TValue>, TValue, CollapsedUnion<TSql, z.ZodLiteral<TValue>>, // <-- FIX
47
+ CollapsedUnion<TSql, z.ZodLiteral<TValue>>>> : Prettify<Builder<"new", T, TSql, ZodTypeFromPrimitive<TValue>, TValue, CollapsedUnion<TSql, ZodTypeFromPrimitive<TValue>>, // <-- FIX
48
+ CollapsedUnion<TSql, ZodTypeFromPrimitive<TValue>>>>;
49
+ <const TValue, TSchema extends z.ZodTypeAny>(value: TValue extends (...args: any[]) => void | undefined ? never : TValue, schemaModifier: (baseSchema: TValue extends () => infer R ? R extends string | number | boolean ? z.ZodLiteral<R> : ZodTypeFromPrimitive<R> : TValue extends string | number | boolean ? z.ZodLiteral<TValue> : ZodTypeFromPrimitive<TValue>) => TSchema): Prettify<Builder<"new", T, TSql, TSchema, TValue extends () => infer R ? R : TValue, CollapsedUnion<TSql, TSchema>, // <-- FIX
50
+ CollapsedUnion<TSql, TSchema>>>;
57
51
  };
58
52
  reference: <TRefSchema extends {
59
53
  _tableName: string;
@@ -102,7 +96,6 @@ type StageMethods = {
102
96
  validation: "transform";
103
97
  done: never;
104
98
  };
105
- type InferSmartClientType<TSql extends z.ZodTypeAny, TNew extends z.ZodTypeAny> = z.infer<TNew> extends z.infer<TSql> ? TNew : z.ZodUnion<[TSql, TNew]>;
106
99
  type BuilderConfig<T extends SQLType | RelationConfig<any>, TSql extends z.ZodTypeAny, TNew extends z.ZodTypeAny, TInitialValue, TClient extends z.ZodTypeAny, TValidation extends z.ZodTypeAny> = {
107
100
  sql: T;
108
101
  zodSqlSchema: TSql;
package/dist/schema.js CHANGED
@@ -84,7 +84,7 @@ export const s = {
84
84
  sqlConfig: sqlConfig,
85
85
  sqlZod: sqlZodType,
86
86
  newZod: sqlZodType,
87
- initialValue: undefined,
87
+ initialValue: inferDefaultFromZod(sqlZodType, sqlConfig),
88
88
  clientZod: sqlZodType,
89
89
  validationZod: sqlZodType,
90
90
  });
@@ -153,56 +153,56 @@ function createBuilder(config) {
153
153
  clientTransform: config.clientTransform, // <-- FIX: Make sure transform is passed through
154
154
  validationTransform: config.validationTransform, // <-- FIX: Make sure transform is passed through
155
155
  },
156
- initialState: (schemaOrDefault, defaultValue) => {
156
+ initialState: (value, schemaModifier) => {
157
157
  if (completedStages.has("new")) {
158
158
  throw new Error("initialState() can only be called once in the chain");
159
159
  }
160
- // ... other error checks ...
161
- const hasSchemaArg = defaultValue !== undefined;
162
- // This logic is mostly from your original code.
163
- const newSchema = hasSchemaArg
164
- ? isFunction(schemaOrDefault)
165
- ? schemaOrDefault({ sql: config.sqlZod })
166
- : schemaOrDefault
167
- : config.sqlZod; // If only a primitive is passed, the "new" schema is still the SQL one.
168
- let finalDefaultValue;
169
- if (hasSchemaArg) {
170
- // Handles two arguments: .initialState(schema, defaultValue)
171
- finalDefaultValue = isFunction(defaultValue)
172
- ? defaultValue()
173
- : defaultValue;
160
+ let actualValue;
161
+ let baseSchema;
162
+ // Check if value is a Zod schema
163
+ if (value && typeof value === "object" && "_def" in value) {
164
+ // It's a Zod schema - infer the default value
165
+ baseSchema = value;
166
+ actualValue = inferDefaultFromZod(baseSchema, config.sqlConfig);
174
167
  }
175
168
  else {
176
- // Handles one argument: .initialState(z.email()) OR .initialState(() => uuid())
177
- const singleArg = schemaOrDefault;
178
- if (singleArg &&
179
- typeof singleArg === "object" &&
180
- singleArg._def) {
181
- // THIS IS THE FIX: If it's a Zod schema, INFER the value.
182
- finalDefaultValue = inferDefaultFromZod(singleArg, config.sqlConfig);
169
+ // Get the actual value
170
+ actualValue = isFunction(value) ? value() : value;
171
+ // Create base Zod schema from the value type
172
+ // Check if it's a literal value (string, number, boolean)
173
+ if (typeof actualValue === "string" ||
174
+ typeof actualValue === "number" ||
175
+ typeof actualValue === "boolean") {
176
+ baseSchema = z.literal(actualValue);
177
+ }
178
+ else if (actualValue instanceof Date) {
179
+ baseSchema = z.date();
180
+ }
181
+ else if (actualValue === null) {
182
+ baseSchema = z.null();
183
+ }
184
+ else if (actualValue === undefined) {
185
+ baseSchema = z.undefined();
183
186
  }
184
187
  else {
185
- // Otherwise, it's a function or primitive value.
186
- finalDefaultValue = isFunction(singleArg)
187
- ? singleArg({ sql: config.sqlZod })
188
- : singleArg;
188
+ baseSchema = z.any();
189
189
  }
190
190
  }
191
+ // Apply schema modifier if provided
192
+ const newSchema = schemaModifier
193
+ ? schemaModifier(baseSchema)
194
+ : baseSchema;
191
195
  const newCompletedStages = new Set(completedStages);
192
196
  newCompletedStages.add("new");
193
- // ---- THIS IS THE RUNTIME FIX THAT MATCHES YOUR INTERFACE ----
194
- // If a new schema was passed, create a union.
195
- // If ONLY a primitive was passed, we MUST also create a union.
196
- const newClientZod = hasSchemaArg
197
- ? z.union([config.sqlZod, newSchema])
198
- : z.union([config.sqlZod, z.any()]); // Create the union for the primitive case
197
+ // Create union for client/validation
198
+ const clientValidationSchema = z.union([config.sqlZod, newSchema]);
199
199
  return createBuilder({
200
200
  ...config,
201
201
  stage: "new",
202
202
  newZod: newSchema,
203
- initialValue: finalDefaultValue,
204
- clientZod: newClientZod,
205
- validationZod: newClientZod, // Keep validation and client in sync for this step
203
+ initialValue: actualValue,
204
+ clientZod: clientValidationSchema,
205
+ validationZod: clientValidationSchema,
206
206
  completedStages: newCompletedStages,
207
207
  });
208
208
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.99",
3
+ "version": "0.5.101",
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",