diff --git a/src/builders/ZodSchemaClassBuilder.ts b/src/builders/ZodSchemaClassBuilder.ts index ba1d94e..1ad8caa 100644 --- a/src/builders/ZodSchemaClassBuilder.ts +++ b/src/builders/ZodSchemaClassBuilder.ts @@ -7,6 +7,7 @@ import { DejsonifiableZodSchemaObject } from "../traits/DejsonifiableZodSchemaOb import { ExtendableZodSchemaObject } from "../traits/ExtendableZodSchemaObject" import { InstantiableZodSchemaObject } from "../traits/InstantiableZodSchemaObject" import { JsonifiableZodSchemaObject } from "../traits/JsonifiableZodSchemaObject" +import { ZodSchemaObjectInstantiationSchemas } from "../traits/ZodSchemaObjectInstantiationSchemas" import { StaticMembers } from "../util" @@ -63,7 +64,7 @@ extends TraitExpressionBuilder { [ ...this.expressionTraits, - // ZodSchemaObjectInstantiationSchemas, + ZodSchemaObjectInstantiationSchemas, InstantiableZodSchemaObject, ExtendableZodSchemaObject, ], diff --git a/src/tests.ts b/src/tests.ts index ae935a1..f233e7f 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -41,6 +41,8 @@ const test = User.schema.extend({ ) const inst = User.create({ id: 1n, name: "" }) +const instEffect = User.createEffect({ id: 1n, name: "" }) + // console.log(inst) const jsonifiedUser = await inst.jsonifyPromise() diff --git a/src/traits/InstantiableZodSchemaObject.ts b/src/traits/InstantiableZodSchemaObject.ts index 898ca9e..1817ab6 100644 --- a/src/traits/InstantiableZodSchemaObject.ts +++ b/src/traits/InstantiableZodSchemaObject.ts @@ -1,4 +1,4 @@ -import { TraitStaticMembers, expression } from "@thilawyn/traitify-ts" +import { TraitStaticMembers, trait } from "@thilawyn/traitify-ts" import { HasRequiredKeys } from "type-fest" import { z } from "zod" import { ZodSchemaClass } from "../shapes/ZodSchemaClass" @@ -6,6 +6,8 @@ import { parseZodTypeEffect } from "../util" import { ZodSchemaObjectInstantiationSchemas } from "./ZodSchemaObjectInstantiationSchemas" +type ZodSchemaObjectInstantiationSchemasStaticMembers = TraitStaticMembers + type NewZodSchemaInstanceInput< Values extends object, DefaultValues extends Partial, @@ -26,66 +28,63 @@ type NewZodSchemaInstanceArgs = ( ) -export const InstantiableZodSchemaObject = expression - .expresses(ZodSchemaObjectInstantiationSchemas) +export const InstantiableZodSchemaObject = trait + .implement(Super => class InstantiableZodSchemaObject extends Super { + static create< + Instance extends Values, + + SchemaWithDefaultValuesT extends z.ZodRawShape, + SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, + SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, + + Values extends object, + PartialValues extends Partial, + >( + this: ( + ZodSchemaClass & + ZodSchemaObjectInstantiationSchemasStaticMembers + ), + ...[values, params]: NewZodSchemaInstanceArgs + ) { + return this.instantiationSchemaWithDefaultValues().parse(values, params) + } + + static async createPromise< + Instance extends Values, + + SchemaWithDefaultValuesT extends z.ZodRawShape, + SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, + SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, + + Values extends object, + PartialValues extends Partial, + >( + this: ( + ZodSchemaClass & + ZodSchemaObjectInstantiationSchemasStaticMembers + ), + ...[values, params]: NewZodSchemaInstanceArgs + ) { + return this.instantiationSchemaWithDefaultValues().parseAsync(values, params) + } + + static createEffect< + Instance extends Values, + + SchemaWithDefaultValuesT extends z.ZodRawShape, + SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, + SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, + + Values extends object, + PartialValues extends Partial, + >( + this: ( + ZodSchemaClass & + ZodSchemaObjectInstantiationSchemasStaticMembers + ), + ...[values, params]: NewZodSchemaInstanceArgs + ) { + return parseZodTypeEffect(this.instantiationSchemaWithDefaultValues(), values, params) + } + }) .build() - .subtrait() - .implement(Super => class InstantiableZodSchemaObject extends Super { - static create< - Instance extends Values, - - SchemaWithDefaultValuesT extends z.ZodRawShape, - SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, - SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, - - Values extends object, - PartialValues extends Partial, - >( - this: ( - ZodSchemaClass & - TraitStaticMembers - ), - ...[values, params]: NewZodSchemaInstanceArgs - ) { - return this.instantiationSchemaWithDefaultValues().parse(values, params) - } - - static async createPromise< - Instance extends Values, - - SchemaWithDefaultValuesT extends z.ZodRawShape, - SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, - SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, - - Values extends object, - PartialValues extends Partial, - >( - this: ( - ZodSchemaClass & - TraitStaticMembers - ), - ...[values, params]: NewZodSchemaInstanceArgs - ) { - return this.instantiationSchemaWithDefaultValues().parseAsync(values, params) - } - - static createEffect< - Instance extends Values, - - SchemaWithDefaultValuesT extends z.ZodRawShape, - SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, - SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, - - Values extends object, - PartialValues extends Partial, - >( - this: ( - ZodSchemaClass & - TraitStaticMembers - ), - ...[values, params]: NewZodSchemaInstanceArgs - ) { - return parseZodTypeEffect(this.instantiationSchemaWithDefaultValues(), values, params) - } - }) - .build()