diff --git a/src/ZodSchemaClass.ts b/src/ZodSchemaClass.ts index 320b695..0893682 100644 --- a/src/ZodSchemaClass.ts +++ b/src/ZodSchemaClass.ts @@ -31,6 +31,6 @@ export function ZodSchemaClass< } return expression - .extends(ZodSchemaObjectConstructor as AbstractClass) + .extends(ZodSchemaObjectConstructor) .expresses(ZodSchemaObject(schema, schemaWithDefaultValues)) } diff --git a/src/traits/ZodSchemaObject.ts b/src/traits/ZodSchemaObject.ts index d6efdf6..4ccb7d7 100644 --- a/src/traits/ZodSchemaObject.ts +++ b/src/traits/ZodSchemaObject.ts @@ -1,7 +1,7 @@ import { ImplStatic, trait } from "@thilawyn/traitify-ts" import { Class, HasRequiredKeys } from "type-fest" import { z } from "zod" -import { parseZodSchemaEffect } from "../util" +import { StaticMembers, parseZodSchemaEffect } from "../util" type CreateArgs = ( @@ -23,69 +23,73 @@ export const ZodSchemaObject = < schema: z.ZodObject, schemaWithDefaultValues: z.ZodObject, ) => trait - .implement(Super => class ZodSchemaObject extends Super { - static readonly schema = schema - static readonly schemaWithDefaultValues = schemaWithDefaultValues + .implement(Super => { + class ZodSchemaObject extends Super { + static readonly schema = schema + static readonly schemaWithDefaultValues = schemaWithDefaultValues - static transform< - Instance extends Values, + static transform< + Instance extends Values, - T extends z.ZodRawShape, - UnknownKeys extends z.UnknownKeysParam, - Catchall extends z.ZodTypeAny, - Output extends Values, - Input, - >( - this: Class, - schema: z.ZodObject, - ) { - return schema.transform(values => new this(values)) + T extends z.ZodRawShape, + UnknownKeys extends z.UnknownKeysParam, + Catchall extends z.ZodTypeAny, + Output extends Values, + Input, + >( + this: Class, + schema: z.ZodObject, + ) { + return schema.transform(values => new this(values)) + } + + + static create< + Instance extends Values + >( + this: ( + Class & + ImplStatic + ), + ...[values, params]: CreateArgs + ) { + return this + .transform(this.schemaWithDefaultValues) + .parse(values, params) + } + + static createPromise< + Instance extends Values + >( + this: ( + Class & + ImplStatic + ), + ...[values, params]: CreateArgs + ) { + return this + .transform(this.schemaWithDefaultValues) + .parseAsync(values, params) + } + + static createEffect< + Instance extends Values + >( + this: ( + Class & + ImplStatic + ), + ...[values, params]: CreateArgs + ) { + return parseZodSchemaEffect( + this.transform(this.schemaWithDefaultValues), + values, + params, + ) + } } - - static create< - Instance extends Values - >( - this: ( - Class & - ImplStatic - ), - ...[values, params]: CreateArgs - ) { - return this - .transform(this.schemaWithDefaultValues) - .parse(values, params) - } - - static createPromise< - Instance extends Values - >( - this: ( - Class & - ImplStatic - ), - ...[values, params]: CreateArgs - ) { - return this - .transform(this.schemaWithDefaultValues) - .parseAsync(values, params) - } - - static createEffect< - Instance extends Values - >( - this: ( - Class & - ImplStatic - ), - ...[values, params]: CreateArgs - ) { - return parseZodSchemaEffect( - this.transform(this.schemaWithDefaultValues), - values, - params, - ) - } + return ZodSchemaObject as Class & StaticMembers }) .build()