From f40bebbc9b6fab690629ce30ab61860c808413e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Wed, 7 Feb 2024 00:32:23 +0100 Subject: [PATCH] InstantiableZodSchemaObject --- src/ZodSchemaClass.ts | 54 +------------ src/traits/InstantiableZodSchemaObject.ts | 95 +++++++++++++++++++++++ 2 files changed, 99 insertions(+), 50 deletions(-) create mode 100644 src/traits/InstantiableZodSchemaObject.ts diff --git a/src/ZodSchemaClass.ts b/src/ZodSchemaClass.ts index 8dd08e1..b0f43d7 100644 --- a/src/ZodSchemaClass.ts +++ b/src/ZodSchemaClass.ts @@ -1,59 +1,13 @@ import { Effect, pipe } from "effect" -import { AbstractClass, Class as ConcreteClass, Opaque } from "type-fest" +import { AbstractClass, Class, Opaque } from "type-fest" import { z } from "zod" import { DefinedDefaultValuesTag, NewZodSchemaInstanceArgs, NewZodSchemaInstanceInput, TZodSchemaClass } from "." -import { Class, ClassesInstances, ClassesStaticMembers, GetClassType, MergeInheritanceTree, MergeInheritanceTreeWithoutOverriding, parseZodTypeEffect } from "./util" -import { abstract, trait } from "@thilawyn/traitify-ts" +import { ClassesInstances, ClassesStaticMembers, GetClassType, MergeInheritanceTree, MergeInheritanceTreeWithoutOverriding, parseZodTypeEffect } from "./util" +import { Trait, abstract, trait } from "@thilawyn/traitify-ts" -export const InstantiableZodSchemaObject = < - SchemaT extends z.ZodRawShape, - SchemaUnknownKeys extends z.UnknownKeysParam, - SchemaCatchall extends z.ZodTypeAny, - Values extends {}, - DefaultValues extends Partial, ->() => trait( - abstract(), - - Super => class InstantiableZodSchemaObject extends Super { - 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)), - ) - } - }, -) +type T = Trait.Class export function ZodSchemaClassOf< diff --git a/src/traits/InstantiableZodSchemaObject.ts b/src/traits/InstantiableZodSchemaObject.ts new file mode 100644 index 0000000..2701350 --- /dev/null +++ b/src/traits/InstantiableZodSchemaObject.ts @@ -0,0 +1,95 @@ +import { abstract, trait } from "@thilawyn/traitify-ts" +import { Effect, pipe } from "effect" +import { Class } from "type-fest" +import { z } from "zod" +import { NewZodSchemaInstanceArgs, NewZodSchemaInstanceInput } from ".." +import { parseZodTypeEffect } from "../util" + + +type ZodSchemaClassStatic< + SchemaT extends z.ZodRawShape, + SchemaUnknownKeys extends z.UnknownKeysParam, + SchemaCatchall extends z.ZodTypeAny, + + Values extends {}, + DefaultValues extends Partial, +> = { + readonly schema: z.ZodObject + readonly defaultValues: DefaultValues +} + + +export const InstantiableZodSchemaObject = trait( + abstract(), + + Super => class InstantiableZodSchemaObject extends Super { + static create< + Self extends Class & ZodSchemaClassStatic, + + SchemaT extends z.ZodRawShape, + SchemaUnknownKeys extends z.UnknownKeysParam, + SchemaCatchall extends z.ZodTypeAny, + + Values extends {}, + DefaultValues extends Partial, + >( + this: Self, + + ...[values, params]: NewZodSchemaInstanceArgs< + NewZodSchemaInstanceInput + > + ) { + return new this( + this.schema.parse({ ...this.defaultValues, ...values }, params) + ) as InstanceType + } + + static async createPromise< + Self extends Class & ZodSchemaClassStatic, + + SchemaT extends z.ZodRawShape, + SchemaUnknownKeys extends z.UnknownKeysParam, + SchemaCatchall extends z.ZodTypeAny, + + Values extends {}, + DefaultValues extends Partial, + >( + this: Self, + + ...[values, params]: NewZodSchemaInstanceArgs< + NewZodSchemaInstanceInput + > + ) { + return new this( + await this.schema.parseAsync({ ...this.defaultValues, ...values }, params) + ) as Promise> + } + + static createEffect< + Self extends Class & ZodSchemaClassStatic, + + SchemaT extends z.ZodRawShape, + SchemaUnknownKeys extends z.UnknownKeysParam, + SchemaCatchall extends z.ZodTypeAny, + + Values extends {}, + DefaultValues extends Partial, + >( + this: Self, + + ...[values, params]: NewZodSchemaInstanceArgs< + NewZodSchemaInstanceInput + > + ) { + return pipe( + parseZodTypeEffect( + this.schema, + { ...this.defaultValues, ...values }, + params, + ), + + Effect.map(values => new this(values) as InstanceType), + ) + } + }, +)