From d45708dc2681a85b5627c5d321d8ec5a07cd323b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Sun, 17 Mar 2024 13:12:42 +0100 Subject: [PATCH] ExtendableZodSchemaObject --- src/ZodSchemaClass.ts | 4 ++-- src/traits/ExtendableZodSchemaObject.ts | 21 ++++++++++++++++++++- src/traits/ZodSchemaObject.ts | 20 -------------------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/ZodSchemaClass.ts b/src/ZodSchemaClass.ts index c246529..19ee1e9 100644 --- a/src/ZodSchemaClass.ts +++ b/src/ZodSchemaClass.ts @@ -1,7 +1,7 @@ import { expression } from "@thilawyn/traitify-ts" import { Class } from "type-fest" import { z } from "zod" -import { ZodSchemaObject } from "./lib" +import { ExtendableZodSchemaObject } from "./traits/ExtendableZodSchemaObject" export function ZodSchemaClass< @@ -20,5 +20,5 @@ export function ZodSchemaClass< } } as Class ) - .expresses(ZodSchemaObject(schemaWithDefaults)) + .expresses(ExtendableZodSchemaObject(schemaWithDefaults)) } diff --git a/src/traits/ExtendableZodSchemaObject.ts b/src/traits/ExtendableZodSchemaObject.ts index 9c9ec54..aebadee 100644 --- a/src/traits/ExtendableZodSchemaObject.ts +++ b/src/traits/ExtendableZodSchemaObject.ts @@ -1,4 +1,5 @@ -import { expression } from "@thilawyn/traitify-ts" +import { ImplStatic, expression } from "@thilawyn/traitify-ts" +import { AbstractClass } from "type-fest" import { z } from "zod" import { ZodSchemaObject } from "./ZodSchemaObject" @@ -15,5 +16,23 @@ export const ExtendableZodSchemaObject = < .build() .subtrait() .implement(Super => class ExtendableZodSchemaObjectImpl extends Super { + 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(ExtendableZodSchemaObject(schemaWithDefaults(this.schemaWithDefaults))) + } }) .build() diff --git a/src/traits/ZodSchemaObject.ts b/src/traits/ZodSchemaObject.ts index fc7a319..7564b10 100644 --- a/src/traits/ZodSchemaObject.ts +++ b/src/traits/ZodSchemaObject.ts @@ -82,26 +82,6 @@ export const ZodSchemaObject = < 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()