diff --git a/src/JsonifiedZodSchemaClass.ts b/src/JsonifiedZodSchemaClass.ts index eaa3ba6..289a66b 100644 --- a/src/JsonifiedZodSchemaClass.ts +++ b/src/JsonifiedZodSchemaClass.ts @@ -1,12 +1,12 @@ -import { TraitClass, expression } from "@thilawyn/traitify-ts" +import { expression } from "@thilawyn/traitify-ts" import { Class, Jsonifiable } from "type-fest" import { z } from "zod" -import { ZodSchemaObjectTrait } from "./lib" -import { JsonifiedZodSchemaObject } from "./traits/JsonifiedZodSchemaObject" +import { JsonifiedZodSchemaObject, OfClass, OfClassInstance } from "./traits/JsonifiedZodSchemaObject" export function JsonifiedZodSchemaClass< - Of extends TraitClass>, + Of extends OfClass, + OfInstance extends OfClassInstance, T extends z.ZodRawShape, Catchall extends z.ZodTypeAny, Values extends object, @@ -18,7 +18,7 @@ export function JsonifiedZodSchemaClass< DejsonifyCatchall extends z.ZodTypeAny, JsonifiedValues extends object & Jsonifiable, >( - of: Of | TraitClass>, + of: Of | OfClass, props: { jsonifySchema: ( @@ -38,5 +38,5 @@ export function JsonifiedZodSchemaClass< } } as Class ) - .expresses(JsonifiedZodSchemaObject(of, props)) + .expresses(JsonifiedZodSchemaObject(of as Of, props)) } diff --git a/src/tests.ts b/src/tests.ts index 172086f..0d8db8b 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -42,7 +42,7 @@ const jsonifiedUserExp = JsonifiedZodSchemaClass(User, { class JsonifiedUser extends jsonifiedUserExp.extends implements Implements {} const jsonifiedUser = JsonifiedUser.jsonify(inst) - +jsonifiedUser.dejsonify() const adminUserExp = User.extend(s => s.extend({ role: z.literal("Admin").default("Admin") diff --git a/src/traits/JsonifiedZodSchemaObject.ts b/src/traits/JsonifiedZodSchemaObject.ts index b34a527..4fe18b0 100644 --- a/src/traits/JsonifiedZodSchemaObject.ts +++ b/src/traits/JsonifiedZodSchemaObject.ts @@ -1,12 +1,35 @@ -import { ImplStatic, TraitClass, trait } from "@thilawyn/traitify-ts" +import { ImplStatic, TraitInstance, TraitStaticMembers, trait } from "@thilawyn/traitify-ts" import { Class, Jsonifiable } from "type-fest" import { z } from "zod" import { parseZodSchemaEffect } from "../util" import { ZodSchemaObjectTrait } from "./ZodSchemaObject" +export type OfClass< + Instance extends OfClassInstance, + + T extends z.ZodRawShape, + Catchall extends z.ZodTypeAny, + Values extends object, + PartialValues extends Partial, +> = ( + Class & + TraitStaticMembers> +) + +export type OfClassInstance< + T extends z.ZodRawShape, + Catchall extends z.ZodTypeAny, + Values extends object, + PartialValues extends Partial, +> = ( + Values & TraitInstance> +) + + export const JsonifiedZodSchemaObject = < - Of extends TraitClass>, + Of extends OfClass, + OfInstance extends OfClassInstance, T extends z.ZodRawShape, Catchall extends z.ZodTypeAny, Values extends object, @@ -18,7 +41,7 @@ export const JsonifiedZodSchemaObject = < DejsonifyCatchall extends z.ZodTypeAny, JsonifiedValues extends object & Jsonifiable, >( - of: Of | TraitClass>, + of: Of | OfClass, props: { jsonifySchema: ( @@ -31,6 +54,8 @@ export const JsonifiedZodSchemaObject = < }, ) => trait .implement(Super => class JsonifiedZodSchemaObjectImpl extends Super { + declare ["constructor"]: typeof JsonifiedZodSchemaObjectImpl + static readonly of = of as Of static readonly jsonifySchema = props.jsonifySchema(of.schema) static readonly dejsonifySchema = props.dejsonifySchema(of.schema) @@ -98,5 +123,22 @@ export const JsonifiedZodSchemaObject = < params, ) } + + + dejsonify(params?: Partial) { + return this.constructor.of.pipeSchemaIntoInstance( + this.constructor.dejsonifySchema + ).parse(this, params) + } + + dejsonifyPromise(params?: Partial) { + return this.constructor.of.pipeSchemaIntoInstance( + this.constructor.dejsonifySchema + ).parseAsync(this, params) + } + + dejsonifyEffect(params?: Partial) { + return parseZodSchemaEffect(this.constructor.dejsonifySchema, this, params) + } }) .build()