diff --git a/src/ZodSchemaClass.ts b/src/ZodSchemaClass.ts index b0f43d7..42425ae 100644 --- a/src/ZodSchemaClass.ts +++ b/src/ZodSchemaClass.ts @@ -1,136 +1,34 @@ -import { Effect, pipe } from "effect" -import { AbstractClass, Class, Opaque } from "type-fest" +import { expression } from "@thilawyn/traitify-ts" +import { NoInfer } from "effect/Types" +import { AbstractClass, Opaque } from "type-fest" import { z } from "zod" -import { DefinedDefaultValuesTag, NewZodSchemaInstanceArgs, NewZodSchemaInstanceInput, TZodSchemaClass } from "." -import { ClassesInstances, ClassesStaticMembers, GetClassType, MergeInheritanceTree, MergeInheritanceTreeWithoutOverriding, parseZodTypeEffect } from "./util" -import { Trait, abstract, trait } from "@thilawyn/traitify-ts" - - - -type T = Trait.Class +import { DefinedDefaultValuesTag } from "." +import { InstantiableZodSchemaObject } from "./traits/InstantiableZodSchemaObject" export function ZodSchemaClassOf< - Super extends AbstractClass, + Superclass extends AbstractClass<{}, []>, SchemaT extends z.ZodRawShape, SchemaUnknownKeys extends z.UnknownKeysParam, SchemaCatchall extends z.ZodTypeAny, Values extends {}, - DefaultValues extends Partial, + DefaultValues extends NoInfer>, >( - of: Super, + of: Superclass, { schema, defaultValues }: { schema: z.ZodObject - defaultValues: Opaque + defaultValues: DefaultValues }, ) { - type TZodSchemaClassImpl = TZodSchemaClass< - SchemaT, - SchemaUnknownKeys, - SchemaCatchall, + const exp = expression + .extends(of) + .expresses(InstantiableZodSchemaObject) + .build() - Values, - DefaultValues - > - - return class extends (of as unknown as ConcreteClass) { - static readonly schema = schema - static readonly defaultValues = defaultValues - - constructor(values: Values) { - super() - Object.assign(this, values) - } - - static create( - ...[values, params]: NewZodSchemaInstanceArgs< - NewZodSchemaInstanceInput - > - ) { - return new this( - this.schema.parse({ ...this.defaultValues, ...values }, params) - ) - } - - static async createPromise( - ...[values, params]: NewZodSchemaInstanceArgs< - NewZodSchemaInstanceInput - > - ) { - return new this( - await this.schema.parseAsync({ ...this.defaultValues, ...values }, params) - ) - } - - static createEffect( - ...[values, params]: NewZodSchemaInstanceArgs< - NewZodSchemaInstanceInput - > - ) { - return pipe( - parseZodTypeEffect( - this.schema, - { ...this.defaultValues, ...values }, - params, - ), - - Effect.map(values => new this(values)), - ) - } - - static extend< - ExtendedSchemaT extends z.ZodRawShape, - ExtendedSchemaUnknownKeys extends z.UnknownKeysParam, - ExtendedSchemaCatchall extends z.ZodTypeAny, - - ExtendedValues extends Values, - ExtendedDefaultValues extends Partial, - >( - props: { - schema: (props: { - schema: z.ZodObject - shape: SchemaT - }) => z.ZodObject - - defaultValues: (defaultValues: DefaultValues) => Opaque - }, - ) { - const schema = props.schema({ - schema: this.schema, - shape: this.schema.shape, - }) - - const defaultValues = props.defaultValues(this.defaultValues) - - return class extends (this as unknown as ConcreteClass) { - static readonly schema = schema - static readonly defaultValues = defaultValues - } - } - } as unknown as ( - Class< - GetClassType, - - MergeInheritanceTreeWithoutOverriding< - ClassesInstances<[ - Super, - TZodSchemaClassImpl, - ]> - > & - - ConstructorParameters - > & - - MergeInheritanceTree< - ClassesStaticMembers<[ - Super, - TZodSchemaClassImpl, - ]> - > - ) + return exp }