diff --git a/src/builders/ZodSchemaClassBuilder.ts b/src/builders/ZodSchemaClassBuilder.ts index 52e6dbd..ba1d94e 100644 --- a/src/builders/ZodSchemaClassBuilder.ts +++ b/src/builders/ZodSchemaClassBuilder.ts @@ -63,6 +63,7 @@ extends TraitExpressionBuilder { [ ...this.expressionTraits, + // ZodSchemaObjectInstantiationSchemas, InstantiableZodSchemaObject, ExtendableZodSchemaObject, ], diff --git a/src/traits/InstantiableZodSchemaObject.ts b/src/traits/InstantiableZodSchemaObject.ts index 09c2fc8..898ca9e 100644 --- a/src/traits/InstantiableZodSchemaObject.ts +++ b/src/traits/InstantiableZodSchemaObject.ts @@ -1,9 +1,9 @@ -import { trait } from "@thilawyn/traitify-ts" -import { Effect } from "effect" +import { TraitStaticMembers, expression } from "@thilawyn/traitify-ts" import { HasRequiredKeys } from "type-fest" import { z } from "zod" import { ZodSchemaClass } from "../shapes/ZodSchemaClass" import { parseZodTypeEffect } from "../util" +import { ZodSchemaObjectInstantiationSchemas } from "./ZodSchemaObjectInstantiationSchemas" type NewZodSchemaInstanceInput< @@ -26,102 +26,66 @@ type NewZodSchemaInstanceArgs = ( ) -export const InstantiableZodSchemaObject = trait - .implement(Super => class InstantiableZodSchemaObject extends Super { - static instantiationSchema< - Instance extends Values, - - SchemaT extends z.ZodRawShape, - SchemaUnknownKeys extends z.UnknownKeysParam, - SchemaCatchall extends z.ZodTypeAny, - - Values extends object, - >( - this: ZodSchemaClass, - ) { - return this.schema.transform(values => new this(values)) - } - - static instantiationSchemaWithDefaultValues< - Instance extends Values, - - SchemaWithDefaultValuesT extends z.ZodRawShape, - SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, - SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, - - Values extends object, - PartialValues extends Partial, - >( - this: ZodSchemaClass, - ) { - return this.schemaWithDefaultValues.transform(values => new this(values)) - } - - - static create< - Instance extends Values, - - SchemaT extends z.ZodRawShape, - SchemaUnknownKeys extends z.UnknownKeysParam, - SchemaCatchall extends z.ZodTypeAny, - - SchemaWithDefaultValuesT extends z.ZodRawShape, - SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, - SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, - - Values extends object, - PartialValues extends Partial, - >( - this: ZodSchemaClass, - ...[values, params]: NewZodSchemaInstanceArgs - ) { - return new this( - this.schemaWithDefaultValues.parse(values, params) - ) - } - - static async createPromise< - Instance extends Values, - - SchemaT extends z.ZodRawShape, - SchemaUnknownKeys extends z.UnknownKeysParam, - SchemaCatchall extends z.ZodTypeAny, - - SchemaWithDefaultValuesT extends z.ZodRawShape, - SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, - SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, - - Values extends object, - PartialValues extends Partial, - >( - this: ZodSchemaClass, - ...[values, params]: NewZodSchemaInstanceArgs - ) { - return new this( - await this.schemaWithDefaultValues.parseAsync(values, params) - ) - } - - static createEffect< - Instance extends Values, - - SchemaT extends z.ZodRawShape, - SchemaUnknownKeys extends z.UnknownKeysParam, - SchemaCatchall extends z.ZodTypeAny, - - SchemaWithDefaultValuesT extends z.ZodRawShape, - SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, - SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, - - Values extends object, - PartialValues extends Partial, - >( - this: ZodSchemaClass, - ...[values, params]: NewZodSchemaInstanceArgs - ) { - return parseZodTypeEffect(this.schemaWithDefaultValues, values, params).pipe( - Effect.map(values => new this(values)), - ) - } - }) +export const InstantiableZodSchemaObject = expression + .expresses(ZodSchemaObjectInstantiationSchemas) .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() diff --git a/src/traits/ZodSchemaObjectInstantiationSchemas.ts b/src/traits/ZodSchemaObjectInstantiationSchemas.ts new file mode 100644 index 0000000..4af87cb --- /dev/null +++ b/src/traits/ZodSchemaObjectInstantiationSchemas.ts @@ -0,0 +1,37 @@ +import { trait } from "@thilawyn/traitify-ts" +import { z } from "zod" +import { ZodSchemaClass } from "../shapes/ZodSchemaClass" + + +export const ZodSchemaObjectInstantiationSchemas = trait + .implement(Super => class ZodSchemaObjectInstantiationSchemas extends Super { + static instantiationSchema< + Instance extends Values, + + SchemaT extends z.ZodRawShape, + SchemaUnknownKeys extends z.UnknownKeysParam, + SchemaCatchall extends z.ZodTypeAny, + + Values extends object, + >( + this: ZodSchemaClass, + ) { + return this.schema.transform(values => new this(values)) + } + + static instantiationSchemaWithDefaultValues< + Instance extends Values, + + SchemaWithDefaultValuesT extends z.ZodRawShape, + SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam, + SchemaWithDefaultValuesCatchall extends z.ZodTypeAny, + + Values extends object, + PartialValues extends Partial, + >( + this: ZodSchemaClass, + ) { + return this.schemaWithDefaultValues.transform(values => new this(values)) + } + }) + .build()