cogsbox-shape 0.5.22 → 0.5.23

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
@@ -1416,7 +1416,7 @@ export declare function reference<TTargetField extends SchemaField, TField exten
1416
1416
  type: "reference";
1417
1417
  to: () => TTargetField;
1418
1418
  };
1419
- export declare function createMixedValidationSchema<T extends Schema<any>>(schema: T): z.ZodObject<any>;
1419
+ export declare function createMixedValidationSchema<T extends Schema<any>>(schema: T, clientSchema?: z.ZodObject<any>, dbSchema?: z.ZodObject<any>): z.ZodObject<any>;
1420
1420
  type InferMixedSchema<T extends Schema<any>> = {
1421
1421
  [K in keyof T as K extends "_tableName" | "__schemaId" ? never : K]: T[K] extends {
1422
1422
  zodClientSchema: infer C;
package/dist/schema.js CHANGED
@@ -391,25 +391,65 @@ function createSerializableSchema(schema) {
391
391
  }
392
392
  return serializableSchema;
393
393
  }
394
- export function createMixedValidationSchema(schema) {
395
- const { clientSchema, dbSchema } = createSchema(schema);
396
- // Create a schema that accepts either client or db format for each field
394
+ export function createMixedValidationSchema(schema, clientSchema, dbSchema) {
395
+ // If schemas are provided, use them (to avoid circular calls)
396
+ if (clientSchema && dbSchema) {
397
+ const mixedFields = {};
398
+ const allKeys = new Set([
399
+ ...Object.keys(clientSchema.shape),
400
+ ...Object.keys(dbSchema.shape),
401
+ ]);
402
+ for (const key of allKeys) {
403
+ const clientField = clientSchema.shape[key];
404
+ const dbField = dbSchema.shape[key];
405
+ if (clientField && dbField) {
406
+ mixedFields[key] = z.union([clientField, dbField]);
407
+ }
408
+ else {
409
+ mixedFields[key] = clientField || dbField;
410
+ }
411
+ }
412
+ return z.object(mixedFields);
413
+ }
414
+ // Build schemas manually without calling createSchema
415
+ const clientFields = {};
416
+ const dbFields = {};
417
+ for (const [key, value] of Object.entries(schema)) {
418
+ if (key === "_tableName")
419
+ continue;
420
+ if (typeof value === "function") {
421
+ const relation = value();
422
+ if (!isRelation(relation))
423
+ continue;
424
+ // For relations, create mixed schemas recursively
425
+ const childMixedSchema = createMixedValidationSchema(relation.schema);
426
+ if (relation.type === "hasMany") {
427
+ clientFields[key] = z.array(childMixedSchema);
428
+ dbFields[key] = z.array(childMixedSchema);
429
+ }
430
+ else {
431
+ clientFields[key] = childMixedSchema;
432
+ dbFields[key] = childMixedSchema;
433
+ }
434
+ continue;
435
+ }
436
+ clientFields[key] = value.zodClientSchema;
437
+ dbFields[key] = value.zodDbSchema;
438
+ }
439
+ // Now create mixed fields
397
440
  const mixedFields = {};
398
- // Get all unique keys from both schemas
399
441
  const allKeys = new Set([
400
- ...Object.keys(clientSchema.shape),
401
- ...Object.keys(dbSchema.shape),
442
+ ...Object.keys(clientFields),
443
+ ...Object.keys(dbFields),
402
444
  ]);
403
445
  for (const key of allKeys) {
404
- const clientField = clientSchema.shape[key];
405
- const dbField = dbSchema.shape[key];
446
+ const clientField = clientFields[key];
447
+ const dbField = dbFields[key];
406
448
  if (clientField && dbField) {
407
- // If field exists in both, allow either type
408
449
  mixedFields[key] = z.union([clientField, dbField]);
409
450
  }
410
451
  else {
411
- // If field only exists in one, use that type
412
- mixedFields[key] = clientField || dbField;
452
+ mixedFields[key] = (clientField || dbField);
413
453
  }
414
454
  }
415
455
  return z.object(mixedFields);
@@ -419,6 +459,7 @@ export function createSchema(schema) {
419
459
  const dbFields = {};
420
460
  const clientFields = {};
421
461
  const defaultValues = {};
462
+ // ... existing schema building logic ...
422
463
  for (const [key, value] of Object.entries(schema)) {
423
464
  if (key === "_tableName")
424
465
  continue;
@@ -428,25 +469,7 @@ export function createSchema(schema) {
428
469
  throw new Error(`Invalid relation for key ${key}`);
429
470
  }
430
471
  const childSchema = createSchema(relation.schema);
431
- const serializedChildren = createSerializableSchema(relation.schema);
432
- // Get toKey value by calling the function
433
- const toKeyField = relation.toKey.type === "reference"
434
- ? relation.toKey.to()
435
- : relation.toKey;
436
- serialized[key] = {
437
- type: "relation",
438
- relationType: relation.type,
439
- fromKey: relation.fromKey,
440
- toKey: {
441
- sql: toKeyField.sql,
442
- jsonSchema: zodToJsonSchema(toKeyField.zodClientSchema),
443
- defaultValue: toKeyField.defaultValue,
444
- },
445
- schema: serializedChildren,
446
- ...(relation.type === "hasMany" && {
447
- defaultCount: relation.defaultCount,
448
- }),
449
- };
472
+ // ... existing relation logic ...
450
473
  if (relation.type === "hasMany") {
451
474
  dbFields[key] = z.array(z.object(childSchema.dbSchema.shape));
452
475
  clientFields[key] = z.array(z.object(childSchema.clientSchema.shape));
@@ -467,10 +490,13 @@ export function createSchema(schema) {
467
490
  defaultValues[key] =
468
491
  value.defaultValue ?? inferDefaultFromZod(value.zodClientSchema);
469
492
  }
470
- const mixedSchema = createMixedValidationSchema(schema);
493
+ const clientSchemaObj = z.object(clientFields);
494
+ const dbSchemaObj = z.object(dbFields);
495
+ // Pass the built schemas to avoid circular reference
496
+ const mixedSchema = createMixedValidationSchema(schema, clientSchemaObj, dbSchemaObj);
471
497
  return {
472
- dbSchema: z.object(dbFields),
473
- clientSchema: z.object(clientFields),
498
+ dbSchema: dbSchemaObj,
499
+ clientSchema: clientSchemaObj,
474
500
  mixedSchema: mixedSchema,
475
501
  defaultValues: defaultValues,
476
502
  initialValues: () => defaultValues,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.22",
3
+ "version": "0.5.23",
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",