From c16bfe24fdf68d006ad84837e12bfe3a0c188c2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Mon, 18 Mar 2024 11:34:50 +0100 Subject: [PATCH] JsonifiedZodSchemaClass --- src/JsonifiedZodSchemaClass.ts | 42 ++++++++++++++++++++++++++ src/tests.ts | 24 +++++++-------- src/traits/JsonifiedZodSchemaObject.ts | 2 +- 3 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 src/JsonifiedZodSchemaClass.ts diff --git a/src/JsonifiedZodSchemaClass.ts b/src/JsonifiedZodSchemaClass.ts new file mode 100644 index 0000000..eaa3ba6 --- /dev/null +++ b/src/JsonifiedZodSchemaClass.ts @@ -0,0 +1,42 @@ +import { TraitClass, expression } from "@thilawyn/traitify-ts" +import { Class, Jsonifiable } from "type-fest" +import { z } from "zod" +import { ZodSchemaObjectTrait } from "./lib" +import { JsonifiedZodSchemaObject } from "./traits/JsonifiedZodSchemaObject" + + +export function JsonifiedZodSchemaClass< + Of extends TraitClass>, + T extends z.ZodRawShape, + Catchall extends z.ZodTypeAny, + Values extends object, + PartialValues extends Partial, + + JsonifyT extends z.ZodRawShape, + JsonifyCatchall extends z.ZodTypeAny, + DejsonifyT extends z.ZodRawShape, + DejsonifyCatchall extends z.ZodTypeAny, + JsonifiedValues extends object & Jsonifiable, +>( + of: Of | TraitClass>, + + props: { + jsonifySchema: ( + schema: typeof of.schema + ) => z.ZodObject, + + dejsonifySchema: ( + schema: typeof of.schema + ) => z.ZodObject, + }, +) { + return expression + .extends( + class JsonifiedZodSchemaObjectConstructor { + constructor(values: JsonifiedValues) { + Object.assign(this, values) + } + } as Class + ) + .expresses(JsonifiedZodSchemaObject(of, props)) +} diff --git a/src/tests.ts b/src/tests.ts index cb84277..172086f 100644 --- a/src/tests.ts +++ b/src/tests.ts @@ -1,8 +1,8 @@ -import { Implements, expression } from "@thilawyn/traitify-ts" +import { Implements } from "@thilawyn/traitify-ts" import { z } from "zod" +import { JsonifiedZodSchemaClass } from "./JsonifiedZodSchemaClass" import { ZodSchemaClass } from "./ZodSchemaClass" import { dejsonify, jsonify } from "./schema/jsonifiable" -import { JsonifiedZodSchemaObject } from "./traits/JsonifiedZodSchemaObject" import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject" @@ -28,21 +28,21 @@ const inst = User.create({ id: 1n, name: "User" }) const instEffect = User.createEffect({ id: 1n, name: "User" }) -const jsonifiedUserExp = expression.expresses( - JsonifiedZodSchemaObject(User, { - jsonifySchema: s => s.extend({ - id: jsonify.bigint(s.shape.id) - }), +const jsonifiedUserExp = JsonifiedZodSchemaClass(User, { + jsonifySchema: s => s.extend({ + id: jsonify.bigint(s.shape.id) + }), - dejsonifySchema: s => s.extend({ - id: dejsonify.bigint(s.shape.id) - }), - }) -).build() + dejsonifySchema: s => s.extend({ + id: dejsonify.bigint(s.shape.id) + }), +}).build() @jsonifiedUserExp.staticImplements class JsonifiedUser extends jsonifiedUserExp.extends implements Implements {} +const jsonifiedUser = JsonifiedUser.jsonify(inst) + 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 ed0da83..b34a527 100644 --- a/src/traits/JsonifiedZodSchemaObject.ts +++ b/src/traits/JsonifiedZodSchemaObject.ts @@ -16,7 +16,7 @@ export const JsonifiedZodSchemaObject = < JsonifyCatchall extends z.ZodTypeAny, DejsonifyT extends z.ZodRawShape, DejsonifyCatchall extends z.ZodTypeAny, - JsonifiedValues extends Jsonifiable, + JsonifiedValues extends object & Jsonifiable, >( of: Of | TraitClass>,