From bf429fc63b41f8d3835352d5b1e97326443f62ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Sun, 25 Feb 2024 04:45:58 +0100 Subject: [PATCH] ExtendableZodSchemaObject --- src/builders/ZodSchemaClassExtender.ts | 5 +++ src/tests.ts | 21 +++++++--- src/traits/ExtendableZodSchemaObject.ts | 52 +++---------------------- 3 files changed, 25 insertions(+), 53 deletions(-) diff --git a/src/builders/ZodSchemaClassExtender.ts b/src/builders/ZodSchemaClassExtender.ts index d365476..24331a8 100644 --- a/src/builders/ZodSchemaClassExtender.ts +++ b/src/builders/ZodSchemaClassExtender.ts @@ -56,4 +56,9 @@ export class ZodSchemaClassExtender> { ]> ) } + + + build() { + return this.superclass + } } diff --git a/src/tests.ts b/src/tests.ts index 8183320..dd2cecd 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -38,13 +38,22 @@ const inst = User.create({ id: 1n, name: "" }) const jsonifiedUser = await inst.jsonifyPromise() -class SubTest extends User.extend({ - schema: ({ schema }) => schema.extend({ - prout: z.string() - }), +const extended = User.extend() + .schema({ + schema: s => s.extend({ + name: z.literal("Admin") + }), + defaultValues: v => v, + }) - defaultValues: v => v, -}) {} + +class SubTest extends User.extend() + .schema({ + schema: s => s.extend({}), + defaultValues: v => v, + }) + .build() +{} const subInst = await SubTest.createPromise({ name: "", prout: "" }) // console.log(subInst) diff --git a/src/traits/ExtendableZodSchemaObject.ts b/src/traits/ExtendableZodSchemaObject.ts index 06a2e79..d9803a3 100644 --- a/src/traits/ExtendableZodSchemaObject.ts +++ b/src/traits/ExtendableZodSchemaObject.ts @@ -1,58 +1,16 @@ import { trait } from "@thilawyn/traitify-ts" -import { AbstractClass } from "type-fest" -import { z } from "zod" -import { ZodSchemaAbstractClass } from "../shapes/ZodSchemaClass" -import { Extend, StaticMembers } from "../util" +import { ZodSchemaClassExtender } from "../builders/ZodSchemaClassExtender" +import { ZodSchemaClass } from "../shapes/ZodSchemaClass" export const ExtendableZodSchemaObject = trait .implement(Super => class ExtendableZodSchemaObject extends Super { static extend< - Super extends ZodSchemaAbstractClass, - SuperInstance extends SuperValues, - - SuperSchemaT extends z.ZodRawShape, - SuperSchemaUnknownKeys extends z.UnknownKeysParam, - SuperSchemaCatchall extends z.ZodTypeAny, - SuperValues extends object, - SuperDefaultValues extends Partial, - - SchemaT extends z.ZodRawShape, - SchemaUnknownKeys extends z.UnknownKeysParam, - SchemaCatchall extends z.ZodTypeAny, - Values extends SuperValues, - DefaultValues extends Partial, + Self extends ZodSchemaClass, >( - this: Super | ZodSchemaAbstractClass, - - props: { - schema: (props: { - schema: Super["schema"] - shape: Super["schema"]["shape"] - }) => z.ZodObject - - defaultValues: (defaultValues: SuperDefaultValues) => DefaultValues - }, - ): ( - AbstractClass< - Extend<[SuperInstance, Values]>, - [values: Values] - > & - Extend<[ - StaticMembers, - { - readonly schema: z.ZodObject, - readonly defaultValues: DefaultValues, - }, - ]> + this: Self ) { - const schema = props.schema({ schema: this.schema, shape: this.schema.shape }) - const defaultValues = props.defaultValues(this.defaultValues) - - return class extends this { - static readonly schema = schema - static readonly defaultValues = defaultValues - } as any + return new ZodSchemaClassExtender(this) } }) .build()