cogsbox-shape 0.5.16 → 0.5.17
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 +15 -4
- package/dist/schema.js +25 -0
- package/package.json +1 -1
package/dist/schema.d.ts
CHANGED
|
@@ -1416,9 +1416,24 @@ 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>;
|
|
1420
|
+
type InferMixedSchema<T extends Schema<any>> = {
|
|
1421
|
+
[K in keyof T as K extends "_tableName" | "__schemaId" ? never : K]: T[K] extends {
|
|
1422
|
+
zodClientSchema: infer C;
|
|
1423
|
+
zodDbSchema: infer D;
|
|
1424
|
+
} ? C extends z.ZodTypeAny ? D extends z.ZodTypeAny ? z.ZodUnion<[C, D]> : C : D extends z.ZodTypeAny ? D : never : T[K] extends () => {
|
|
1425
|
+
type: "hasMany";
|
|
1426
|
+
schema: infer S extends Schema<any>;
|
|
1427
|
+
} ? z.ZodArray<z.ZodObject<InferMixedSchema<S>>> : T[K] extends () => {
|
|
1428
|
+
type: "hasOne" | "belongsTo";
|
|
1429
|
+
schema: infer S extends Schema<any>;
|
|
1430
|
+
} ? z.ZodObject<InferMixedSchema<S>> : never;
|
|
1431
|
+
};
|
|
1432
|
+
type ConversionType<T extends Schema<any>> = SchemaTypes<T>["client"] & SchemaTypes<T>["db"];
|
|
1419
1433
|
export declare function createSchema<T extends Schema<any>>(schema: T): {
|
|
1420
1434
|
dbSchema: z.ZodObject<Prettify<InferDBSchema<T>>>;
|
|
1421
1435
|
clientSchema: z.ZodObject<Prettify<OmitNever<InferSchema<T>>>>;
|
|
1436
|
+
mixedSchema: z.ZodObject<Prettify<InferMixedSchema<T>>>;
|
|
1422
1437
|
defaultValues: Prettify<OmitNever<ConfigWithOptionalProps<T>>>;
|
|
1423
1438
|
initialValues: () => Prettify<OmitNever<ConfigWithOptionalProps<T>>>;
|
|
1424
1439
|
serialized: Prettify<InferSerializedSchema<T>> & {
|
|
@@ -1426,10 +1441,6 @@ export declare function createSchema<T extends Schema<any>>(schema: T): {
|
|
|
1426
1441
|
__schemaId: string;
|
|
1427
1442
|
};
|
|
1428
1443
|
};
|
|
1429
|
-
type DeepConversionType<ClientType, DbType> = ClientType extends Date | string | number | boolean | null | undefined ? ClientType | DbType : DbType extends Date | string | number | boolean | null | undefined ? ClientType | DbType : ClientType extends Array<infer ClientItem> ? DbType extends Array<infer DbItem> ? Array<DeepConversionType<ClientItem, DbItem>> : ClientType | DbType : ClientType extends object ? DbType extends object ? {
|
|
1430
|
-
[K in keyof ClientType | keyof DbType]: K extends keyof ClientType ? K extends keyof DbType ? DeepConversionType<ClientType[K], DbType[K]> : ClientType[K] : K extends keyof DbType ? DbType[K] : never;
|
|
1431
|
-
} : ClientType | DbType : ClientType | DbType;
|
|
1432
|
-
type ConversionType<T extends Schema<any>> = DeepConversionType<SchemaTypes<T>["client"], SchemaTypes<T>["db"]>;
|
|
1433
1444
|
type OmitNever<T> = {
|
|
1434
1445
|
[K in keyof T as T[K] extends never ? never : K]: T[K];
|
|
1435
1446
|
};
|
package/dist/schema.js
CHANGED
|
@@ -391,6 +391,29 @@ 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
|
|
397
|
+
const mixedFields = {};
|
|
398
|
+
// Get all unique keys from both schemas
|
|
399
|
+
const allKeys = new Set([
|
|
400
|
+
...Object.keys(clientSchema.shape),
|
|
401
|
+
...Object.keys(dbSchema.shape),
|
|
402
|
+
]);
|
|
403
|
+
for (const key of allKeys) {
|
|
404
|
+
const clientField = clientSchema.shape[key];
|
|
405
|
+
const dbField = dbSchema.shape[key];
|
|
406
|
+
if (clientField && dbField) {
|
|
407
|
+
// If field exists in both, allow either type
|
|
408
|
+
mixedFields[key] = z.union([clientField, dbField]);
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
// If field only exists in one, use that type
|
|
412
|
+
mixedFields[key] = clientField || dbField;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return z.object(mixedFields);
|
|
416
|
+
}
|
|
394
417
|
export function createSchema(schema) {
|
|
395
418
|
const serialized = createSerializableSchema(schema);
|
|
396
419
|
const dbFields = {};
|
|
@@ -444,9 +467,11 @@ export function createSchema(schema) {
|
|
|
444
467
|
defaultValues[key] =
|
|
445
468
|
value.defaultValue ?? inferDefaultFromZod(value.zodClientSchema);
|
|
446
469
|
}
|
|
470
|
+
const mixedSchema = createMixedValidationSchema(schema);
|
|
447
471
|
return {
|
|
448
472
|
dbSchema: z.object(dbFields),
|
|
449
473
|
clientSchema: z.object(clientFields),
|
|
474
|
+
mixedSchema: mixedSchema,
|
|
450
475
|
defaultValues: defaultValues,
|
|
451
476
|
initialValues: () => defaultValues,
|
|
452
477
|
serialized: serialized,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.17",
|
|
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",
|