cogsbox-shape 0.5.44 → 0.5.45

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
@@ -4827,9 +4827,7 @@ type InferDefaultValues2<T> = {
4827
4827
  };
4828
4828
  export declare function createSchema<T extends {
4829
4829
  _tableName: string;
4830
- }>(schema: T extends {
4831
- _tableName: string;
4832
- } ? T : never): {
4830
+ }>(schema: T): {
4833
4831
  sqlSchema: z.ZodObject<Prettify<InferSqlSchema<T>>>;
4834
4832
  clientSchema: z.ZodObject<Prettify<InferClientSchema<T>>>;
4835
4833
  validationSchema: z.ZodObject<Prettify<InferValidationSchema<T>>>;
package/dist/schema.js CHANGED
@@ -358,21 +358,19 @@ export function createSchema(schema) {
358
358
  if (!isRelation(relation)) {
359
359
  continue;
360
360
  }
361
- // Recursively process the nested schema
362
361
  const childSchemaResult = createSchema(relation.schema);
363
- // For to-many relations, wrap schemas in z.array()
364
362
  if (relation.type === "hasMany" || relation.type === "manyToMany") {
365
- const arraySchema = z.array(childSchemaResult.validationSchema);
366
- // Relations are often not present on creation, so they should be optional.
367
- validationFields[key] = arraySchema.optional();
363
+ // CORRECTLY ADD THE RELATION TO THE LIST OF VALIDATION FIELDS
364
+ // Make it optional, as relations are often not included on create/update.
365
+ validationFields[key] = z
366
+ .array(childSchemaResult.validationSchema)
367
+ .optional();
368
368
  clientFields[key] = z.array(childSchemaResult.clientSchema).optional();
369
369
  sqlFields[key] = z.array(childSchemaResult.sqlSchema).optional();
370
- const count = relation.defaultCount || 0;
371
- defaultValues[key] = Array.from({ length: count }, () => childSchemaResult.defaultValues);
370
+ defaultValues[key] = Array.from({ length: relation.defaultCount || 0 }, () => childSchemaResult.defaultValues);
372
371
  }
373
372
  else {
374
373
  // hasOne or belongsTo
375
- // Relations are often not present on creation, so they should be optional.
376
374
  validationFields[key] = childSchemaResult.validationSchema.optional();
377
375
  clientFields[key] = childSchemaResult.clientSchema.optional();
378
376
  sqlFields[key] = childSchemaResult.sqlSchema.optional();
@@ -382,19 +380,20 @@ export function createSchema(schema) {
382
380
  // Case 2: Handle reference() objects
383
381
  else if (field && field.type === "reference") {
384
382
  const referencedField = field.to();
385
- validationFields[key] = referencedField.config.zodValidationSchema;
386
- clientFields[key] = referencedField.config.zodClientSchema;
387
383
  sqlFields[key] = referencedField.config.zodSqlSchema;
384
+ clientFields[key] = referencedField.config.zodClientSchema;
385
+ validationFields[key] = referencedField.config.zodValidationSchema;
388
386
  defaultValues[key] = referencedField.config.initialValue;
389
387
  }
390
388
  // Case 3: Handle standard shape.sql() fields
391
389
  else if (field && typeof field === "object" && "config" in field) {
392
- validationFields[key] = field.config.zodValidationSchema;
393
- clientFields[key] = field.config.zodClientSchema;
394
390
  sqlFields[key] = field.config.zodSqlSchema;
391
+ clientFields[key] = field.config.zodClientSchema;
392
+ validationFields[key] = field.config.zodValidationSchema;
395
393
  defaultValues[key] = field.config.initialValue;
396
394
  }
397
395
  }
396
+ // Return the final, correctly typed Zod objects
398
397
  return {
399
398
  sqlSchema: z.object(sqlFields),
400
399
  clientSchema: z.object(clientFields),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.44",
3
+ "version": "0.5.45",
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",