diff --git a/bun.lockb b/bun.lockb index dc2bf92..6e480f4 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 04f6667..7800bca 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "rollup": "^4.9.4", "rollup-plugin-cleanup": "^3.2.1", "rollup-plugin-ts": "^3.4.5", + "ts-functional-pipe": "^3.1.2", "tsx": "^4.7.0", "typescript": "^5.3.3" } diff --git a/src/extendSchemableClass.ts b/src/extendSchemableClass.ts index 5d30d2b..9074869 100644 --- a/src/extendSchemableClass.ts +++ b/src/extendSchemableClass.ts @@ -34,15 +34,25 @@ export function extendSchemableClass< ExtendDefaultValues >, - schemaApplier: (schema: C["schema"]) => z.ZodObject< - SchemaT, - SchemaUnknownKeys, - SchemaCatchall, - SchemaValues, - SchemaValues - >, + props: { + schema: ( + schema: z.ZodObject< + ExtendSchemaT, + ExtendSchemaUnknownKeys, + ExtendSchemaCatchall, + ExtendSchemaValues, + ExtendSchemaValues + > + ) => z.ZodObject< + SchemaT, + SchemaUnknownKeys, + SchemaCatchall, + SchemaValues, + SchemaValues + > - defaultValuesApplier: (defaultValues: ExtendDefaultValues) => DefaultValues, + defaultValues: (defaultValues: ExtendDefaultValues) => DefaultValues + }, ) { type Class = ( C extends ConcreteClass @@ -50,8 +60,8 @@ export function extendSchemableClass< : AbstractClass ) - const schema = schemaApplier(extend.schema) - const defaultValues = defaultValuesApplier(extend.defaultValues) + const schema = props.schema(extend.schema) + const defaultValues = props.defaultValues(extend.defaultValues) return class extends extend { static readonly schema = schema diff --git a/src/makeSchemableClass.ts b/src/makeSchemableClass.ts index 8268b52..544dade 100644 --- a/src/makeSchemableClass.ts +++ b/src/makeSchemableClass.ts @@ -21,15 +21,17 @@ export function makeSchemableClassFrom< >( extend: C, - schema: z.ZodObject< - SchemaT, - SchemaUnknownKeys, - SchemaCatchall, - Values, - Values - >, + { schema, defaultValues }: { + schema: z.ZodObject< + SchemaT, + SchemaUnknownKeys, + SchemaCatchall, + Values, + Values + > - defaultValues: DefaultValues, + defaultValues: DefaultValues + }, ) { type Class = ( C extends ConcreteClass @@ -77,15 +79,17 @@ export function makeSchemableClass< Values extends {}, DefaultValues extends Partial, >( - schema: z.ZodObject< - SchemaT, - SchemaUnknownKeys, - SchemaCatchall, - Values, - Values - >, + props: { + schema: z.ZodObject< + SchemaT, + SchemaUnknownKeys, + SchemaCatchall, + Values, + Values + > - defaultValues: DefaultValues, + defaultValues: DefaultValues + } ) { - return makeSchemableClassFrom(Object, schema, defaultValues) + return makeSchemableClassFrom(Object, props) } diff --git a/src/tests.ts b/src/tests.ts index 5e8740f..ce37480 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -1,3 +1,4 @@ +import { pipeInto } from "ts-functional-pipe" import { z } from "zod" import { makeSchemableClass, newSchemable } from "." import { makeJsonifiableSchemableClass } from "./jsonifiable" @@ -7,29 +8,44 @@ import { dejsonifyBigIntSchema, jsonifyBigIntSchema } from "./legacy/jsonifiable const UserLevel = z.enum(["User", "Admin"]) -const UserProto = makeSchemableClass(z.object({ - id: z.bigint(), - name: z.string(), - level: UserLevel, -}), { - level: "User" -} as const) +const UserProto = makeSchemableClass({ + schema: z.object({ + id: z.bigint(), + name: z.string(), + level: UserLevel, + }), + + defaultValues: { + level: "User" + } as const, +}) UserProto.defaultValues -const JsonifiableUserProto = makeJsonifiableSchemableClass(UserProto, { - jsonifySchema: ({ schema, shape }) => schema.extend({ - id: jsonifyBigIntSchema(shape.id) +class User extends pipeInto( + makeSchemableClass({ + schema: z.object({ + id: z.bigint(), + name: z.string(), + level: UserLevel, + }), + + defaultValues: { + level: "User" + } as const, }), - dejsonifySchema: ({ schema, shape }) => schema.extend({ - id: dejsonifyBigIntSchema(shape.id) - }), -}) + v => makeJsonifiableSchemableClass(v, { + jsonifySchema: ({ schema, shape }) => schema.extend({ + id: jsonifyBigIntSchema(shape.id) + }), - -class User extends JsonifiableUserProto {} + dejsonifySchema: ({ schema, shape }) => schema.extend({ + id: dejsonifyBigIntSchema(shape.id) + }), + }) +) {} const user1 = newSchemable(User, { id: 1n, name: "User" })