From 73303a174855ffb3c5c20e49868e8cd906088de6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Sun, 17 Mar 2024 12:29:07 +0100 Subject: [PATCH] ZodSchemaObject refactoring --- src/ZodSchemaClass.ts | 16 +-- src/traits/ExtendableZodSchemaObject.ts | 4 +- src/traits/ZodSchemaObject.ts | 164 ++++++++++++------------ 3 files changed, 90 insertions(+), 94 deletions(-) diff --git a/src/ZodSchemaClass.ts b/src/ZodSchemaClass.ts index c13f9d6..c246529 100644 --- a/src/ZodSchemaClass.ts +++ b/src/ZodSchemaClass.ts @@ -1,15 +1,9 @@ import { expression } from "@thilawyn/traitify-ts" +import { Class } from "type-fest" import { z } from "zod" import { ZodSchemaObject } from "./lib" -class ZodSchemaObjectConstructor { - constructor(values: Values) { - Object.assign(this, values) - } -} - - export function ZodSchemaClass< T extends z.ZodRawShape, Catchall extends z.ZodTypeAny, @@ -19,6 +13,12 @@ export function ZodSchemaClass< schemaWithDefaults: z.ZodObject ) { return expression - .extends(ZodSchemaObjectConstructor) + .extends( + class ZodSchemaObjectConstructor { + constructor(values: Values) { + Object.assign(this, values) + } + } as Class + ) .expresses(ZodSchemaObject(schemaWithDefaults)) } diff --git a/src/traits/ExtendableZodSchemaObject.ts b/src/traits/ExtendableZodSchemaObject.ts index c89c66d..9c9ec54 100644 --- a/src/traits/ExtendableZodSchemaObject.ts +++ b/src/traits/ExtendableZodSchemaObject.ts @@ -12,8 +12,8 @@ export const ExtendableZodSchemaObject = < schemaWithDefaults: z.ZodObject, ) => expression .expresses(ZodSchemaObject(schemaWithDefaults)) - .buildAnyway() + .build() .subtrait() - .implement(Super => class ExtendableZodSchemaObject extends Super { + .implement(Super => class ExtendableZodSchemaObjectImpl extends Super { }) .build() diff --git a/src/traits/ZodSchemaObject.ts b/src/traits/ZodSchemaObject.ts index 850121f..03b94fd 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 { StaticMembers, parseZodSchemaEffect, stripZodObjectDefaults } from "../util" +import { parseZodSchemaEffect, stripZodObjectDefaults } from "../util" type CreateArgs = ( @@ -19,93 +19,89 @@ export const ZodSchemaObject = < >( schemaWithDefaults: z.ZodObject, ) => trait - .implement(Super => { - class ZodSchemaObject extends Super { - static readonly schema = stripZodObjectDefaults(schemaWithDefaults) - static readonly schemaWithDefaults = schemaWithDefaults + .implement(Super => class ZodSchemaObjectImpl extends Super { + static readonly schema = stripZodObjectDefaults(schemaWithDefaults) + static readonly schemaWithDefaults = schemaWithDefaults - static transform< - Instance extends Values, + static transform< + Instance extends Values, - TransformT extends z.ZodRawShape, - TransformUnknownKeys extends z.UnknownKeysParam, - TransformCatchall extends z.ZodTypeAny, - TransformOutput extends Values, - TransformInput, - >( - 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.schemaWithDefaults) - .parse(values, params) - } - - static createPromise< - Instance extends Values - >( - this: ( - Class & - ImplStatic - ), - ...[values, params]: CreateArgs - ) { - return this - .transform(this.schemaWithDefaults) - .parseAsync(values, params) - } - - static createEffect< - Instance extends Values - >( - this: ( - Class & - ImplStatic - ), - ...[values, params]: CreateArgs - ) { - return parseZodSchemaEffect( - this.transform(this.schemaWithDefaults), - values, - params, - ) - } - - - // static extend< - // Self extends AbstractClass & ImplStatic, - - // ExtendedT extends z.ZodRawShape, - // ExtendedCatchall extends z.ZodTypeAny, - // ExtendedValues extends Values, - // ExtendedPartialValues extends Partial, - // >( - // this: Self, - - // schemaWithDefaults: ( - // schemaWithDefaults: typeof this.schemaWithDefaults - // ) => z.ZodObject, - // ) { - // return expression - // .extends(this) - // .expresses(ZodSchemaObject(schemaWithDefaults(this.schemaWithDefaults))) - // } + TransformT extends z.ZodRawShape, + TransformUnknownKeys extends z.UnknownKeysParam, + TransformCatchall extends z.ZodTypeAny, + TransformOutput extends Values, + TransformInput, + >( + this: Class, + schema: z.ZodObject, + ) { + return schema.transform(values => new this(values)) } - return ZodSchemaObject as Class & StaticMembers + + static create< + Instance extends Values + >( + this: ( + Class & + ImplStatic + ), + ...[values, params]: CreateArgs + ) { + return this + .transform(this.schemaWithDefaults) + .parse(values, params) + } + + static createPromise< + Instance extends Values + >( + this: ( + Class & + ImplStatic + ), + ...[values, params]: CreateArgs + ) { + return this + .transform(this.schemaWithDefaults) + .parseAsync(values, params) + } + + static createEffect< + Instance extends Values + >( + this: ( + Class & + ImplStatic + ), + ...[values, params]: CreateArgs + ) { + return parseZodSchemaEffect( + this.transform(this.schemaWithDefaults), + values, + params, + ) + } + + + // static extend< + // Self extends AbstractClass & ImplStatic, + + // ExtendedT extends z.ZodRawShape, + // ExtendedCatchall extends z.ZodTypeAny, + // ExtendedValues extends Values, + // ExtendedPartialValues extends Partial, + // >( + // this: Self, + + // schemaWithDefaults: ( + // schemaWithDefaults: typeof this.schemaWithDefaults + // ) => z.ZodObject, + // ) { + // return expression + // .extends(this) + // .expresses(ZodSchemaObject(schemaWithDefaults(this.schemaWithDefaults))) + // } }) .build()