diff --git a/src/tests.ts b/src/tests.ts index a43865c..f812a71 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -19,6 +19,7 @@ const newTestExp = new ZodSchemaClassBuilder(TraitExpression.NullSuperclass, []) jsonifySchema: ({ schema, shape }) => schema.extend({ id: jsonify.bigint(shape.id) }), + dejsonifySchema: ({ schema, shape }) => schema.extend({ id: dejsonify.bigint(shape.id) }), diff --git a/src/traits/DejsonifiableZodSchemaObject.ts b/src/traits/DejsonifiableZodSchemaObject.ts new file mode 100644 index 0000000..4ff593d --- /dev/null +++ b/src/traits/DejsonifiableZodSchemaObject.ts @@ -0,0 +1,95 @@ +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 { JsonifiableObject } from "type-fest/source/jsonifiable" + + +export const DejsonifiableZodSchemaObject = trait + .implement(Super => class DejsonifiableZodSchemaObject extends Super { + static dejsonify< + 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 + >, + ) { + return new this( + this.schema.parse({ ...this.defaultValues, ...values }, params) + ) + } + + // static async createPromise< + // Instance extends Values, + + // SchemaT extends z.ZodRawShape, + // SchemaUnknownKeys extends z.UnknownKeysParam, + // SchemaCatchall extends z.ZodTypeAny, + + // Values extends object, + // DefaultValues extends Partial, + // >( + // this: ZodSchemaClass, + + // ...[values, params]: NewZodSchemaInstanceArgs< + // NewZodSchemaInstanceInput + // > + // ) { + // return new this( + // await this.schema.parseAsync({ ...this.defaultValues, ...values }, params) + // ) + // } + + // static createEffect< + // Instance extends Values, + + // SchemaT extends z.ZodRawShape, + // SchemaUnknownKeys extends z.UnknownKeysParam, + // SchemaCatchall extends z.ZodTypeAny, + + // Values extends object, + // DefaultValues extends Partial, + // >( + // this: ZodSchemaClass, + + // ...[values, params]: NewZodSchemaInstanceArgs< + // NewZodSchemaInstanceInput + // > + // ) { + // return pipe( + // parseZodTypeEffect( + // this.schema, + // { ...this.defaultValues, ...values }, + // params, + // ), + + // Effect.map(values => new this(values)), + // ) + // } + }) + .build()