diff --git a/src/ZodSchemaClass.ts b/src/ZodSchemaClass.ts index f8fe517..fee8c37 100644 --- a/src/ZodSchemaClass.ts +++ b/src/ZodSchemaClass.ts @@ -3,6 +3,7 @@ import { AbstractClass, Simplify } from "type-fest" import { JsonifiableObject } from "type-fest/source/jsonifiable" import { z } from "zod" import { ZodSchemaAbstractClass } from "./shapes/ZodSchemaClass" +import { DejsonifiableZodSchemaObject } from "./traits/DejsonifiableZodSchemaObject" import { ExtendableZodSchemaObject } from "./traits/ExtendableZodSchemaObject" import { InstantiableZodSchemaObject } from "./traits/InstantiableZodSchemaObject" import { Extend, StaticMembers } from "./util" @@ -135,6 +136,7 @@ extends TraitExpressionBuilder { [ ...this.expressionTraits, + DejsonifiableZodSchemaObject, ], ) } diff --git a/src/shapes/JsonifiableZodSchemaClass.ts b/src/shapes/JsonifiableZodSchemaClass.ts index 0f15bd2..36abdfc 100644 --- a/src/shapes/JsonifiableZodSchemaClass.ts +++ b/src/shapes/JsonifiableZodSchemaClass.ts @@ -6,9 +6,6 @@ import { z } from "zod" export type JsonifiableZodSchemaClass< Instance extends Values, - Values extends object, - JsonifiedValues extends JsonifiableObject, - JsonifySchemaT extends z.ZodRawShape, JsonifySchemaUnknownKeys extends z.UnknownKeysParam, JsonifySchemaCatchall extends z.ZodTypeAny, @@ -16,28 +13,28 @@ export type JsonifiableZodSchemaClass< DejsonifySchemaT extends z.ZodRawShape, DejsonifySchemaUnknownKeys extends z.UnknownKeysParam, DejsonifySchemaCatchall extends z.ZodTypeAny, + + Values extends object, + JsonifiedValues extends JsonifiableObject, > = ( Class & JsonifiableZodSchemaClassStatic< - Values, - JsonifiedValues, - JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, DejsonifySchemaT, DejsonifySchemaUnknownKeys, - DejsonifySchemaCatchall + DejsonifySchemaCatchall, + + Values, + JsonifiedValues > ) export type JsonifiableZodSchemaAbstractClass< Instance extends Values, - Values extends object, - JsonifiedValues extends JsonifiableObject, - JsonifySchemaT extends z.ZodRawShape, JsonifySchemaUnknownKeys extends z.UnknownKeysParam, JsonifySchemaCatchall extends z.ZodTypeAny, @@ -45,26 +42,26 @@ export type JsonifiableZodSchemaAbstractClass< DejsonifySchemaT extends z.ZodRawShape, DejsonifySchemaUnknownKeys extends z.UnknownKeysParam, DejsonifySchemaCatchall extends z.ZodTypeAny, + + Values extends object, + JsonifiedValues extends JsonifiableObject, > = ( AbstractClass & JsonifiableZodSchemaClassStatic< - Values, - JsonifiedValues, - JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, DejsonifySchemaT, DejsonifySchemaUnknownKeys, - DejsonifySchemaCatchall + DejsonifySchemaCatchall, + + Values, + JsonifiedValues > ) export type JsonifiableZodSchemaClassStatic< - Values extends object, - JsonifiedValues extends JsonifiableObject, - JsonifySchemaT extends z.ZodRawShape, JsonifySchemaUnknownKeys extends z.UnknownKeysParam, JsonifySchemaCatchall extends z.ZodTypeAny, @@ -72,6 +69,9 @@ export type JsonifiableZodSchemaClassStatic< DejsonifySchemaT extends z.ZodRawShape, DejsonifySchemaUnknownKeys extends z.UnknownKeysParam, DejsonifySchemaCatchall extends z.ZodTypeAny, + + Values extends object, + JsonifiedValues extends JsonifiableObject, > = { readonly jsonifySchema: z.ZodObject readonly dejsonifySchema: z.ZodObject diff --git a/src/tests.ts b/src/tests.ts index f812a71..0f80ec8 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -33,6 +33,7 @@ class Test extends newTestExp.extends implements Implements { Test.defaultValues const inst = Test.create({ id: 1n, name: "" }) console.log(inst) +Test.dejsonify({ id: 1n }) class SubTest extends Test.extend({ diff --git a/src/traits/DejsonifiableZodSchemaObject.ts b/src/traits/DejsonifiableZodSchemaObject.ts index 4ff593d..9f38dc3 100644 --- a/src/traits/DejsonifiableZodSchemaObject.ts +++ b/src/traits/DejsonifiableZodSchemaObject.ts @@ -1,11 +1,9 @@ import { trait } from "@thilawyn/traitify-ts" -import { Effect, pipe } from "effect" -import { HasRequiredKeys } from "type-fest" -import { z } from "zod" -import { ZodSchemaClass } from "../shapes/ZodSchemaClass" -import { parseZodTypeEffect } from "../util" -import { JsonifiableZodSchemaClass } from "../shapes/JsonifiableZodSchemaClass" +import { Effect } from "effect" import { JsonifiableObject } from "type-fest/source/jsonifiable" +import { z } from "zod" +import { JsonifiableZodSchemaClass } from "../shapes/JsonifiableZodSchemaClass" +import { parseZodTypeEffect } from "../util" export const DejsonifiableZodSchemaObject = trait @@ -38,58 +36,87 @@ export const DejsonifiableZodSchemaObject = trait Values, JsonifiedValues >, + + values: JsonifiedValues, + params?: Partial, ) { return new this( - this.schema.parse({ ...this.defaultValues, ...values }, params) + this.dejsonifySchema.parse(values, params) ) } - // static async createPromise< - // Instance extends Values, + static async dejsonifyPromise< + Instance extends Values, - // SchemaT extends z.ZodRawShape, - // SchemaUnknownKeys extends z.UnknownKeysParam, - // SchemaCatchall extends z.ZodTypeAny, + JsonifySchemaT extends z.ZodRawShape, + JsonifySchemaUnknownKeys extends z.UnknownKeysParam, + JsonifySchemaCatchall extends z.ZodTypeAny, - // Values extends object, - // DefaultValues extends Partial, - // >( - // this: ZodSchemaClass, + DejsonifySchemaT extends z.ZodRawShape, + DejsonifySchemaUnknownKeys extends z.UnknownKeysParam, + DejsonifySchemaCatchall extends z.ZodTypeAny, - // ...[values, params]: NewZodSchemaInstanceArgs< - // NewZodSchemaInstanceInput - // > - // ) { - // return new this( - // await this.schema.parseAsync({ ...this.defaultValues, ...values }, params) - // ) - // } + Values extends object, + JsonifiedValues extends JsonifiableObject, + >( + this: JsonifiableZodSchemaClass< + Instance, - // static createEffect< - // Instance extends Values, + JsonifySchemaT, + JsonifySchemaUnknownKeys, + JsonifySchemaCatchall, - // SchemaT extends z.ZodRawShape, - // SchemaUnknownKeys extends z.UnknownKeysParam, - // SchemaCatchall extends z.ZodTypeAny, + DejsonifySchemaT, + DejsonifySchemaUnknownKeys, + DejsonifySchemaCatchall, - // Values extends object, - // DefaultValues extends Partial, - // >( - // this: ZodSchemaClass, + Values, + JsonifiedValues + >, - // ...[values, params]: NewZodSchemaInstanceArgs< - // NewZodSchemaInstanceInput - // > - // ) { - // return pipe( - // parseZodTypeEffect( - // this.schema, - // { ...this.defaultValues, ...values }, - // params, - // ), + values: JsonifiedValues, + params?: Partial, + ) { + return new this( + await this.dejsonifySchema.parseAsync(values, params) + ) + } - // Effect.map(values => new this(values)), - // ) - // } + static dejsonifyEffect< + Instance extends Values, + + JsonifySchemaT extends z.ZodRawShape, + JsonifySchemaUnknownKeys extends z.UnknownKeysParam, + JsonifySchemaCatchall extends z.ZodTypeAny, + + DejsonifySchemaT extends z.ZodRawShape, + DejsonifySchemaUnknownKeys extends z.UnknownKeysParam, + DejsonifySchemaCatchall extends z.ZodTypeAny, + + Values extends object, + JsonifiedValues extends JsonifiableObject, + >( + this: JsonifiableZodSchemaClass< + Instance, + + JsonifySchemaT, + JsonifySchemaUnknownKeys, + JsonifySchemaCatchall, + + DejsonifySchemaT, + DejsonifySchemaUnknownKeys, + DejsonifySchemaCatchall, + + Values, + JsonifiedValues + >, + + values: JsonifiedValues, + params?: Partial, + ) { + return parseZodTypeEffect(this.dejsonifySchema, values, params).pipe( + Effect.map(values => new this(values)), + ) + } }) .build()