From 713e679e3558ab61d3dcb55ef627cc9a9de9b277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Tue, 2 Jan 2024 03:37:09 +0100 Subject: [PATCH] Schemable JSON helpers --- src/jsonifiable/schema/index.ts | 1 + src/jsonifiable/schema/schemable.ts | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/jsonifiable/schema/schemable.ts diff --git a/src/jsonifiable/schema/index.ts b/src/jsonifiable/schema/index.ts index f4bad9b..08ecef8 100644 --- a/src/jsonifiable/schema/index.ts +++ b/src/jsonifiable/schema/index.ts @@ -1,3 +1,4 @@ export * from "./bigint" export * from "./date" export * from "./decimal" +export * from "./schemable" diff --git a/src/jsonifiable/schema/schemable.ts b/src/jsonifiable/schema/schemable.ts new file mode 100644 index 0000000..c6e2a2d --- /dev/null +++ b/src/jsonifiable/schema/schemable.ts @@ -0,0 +1,25 @@ +import { z } from "zod" +import { JsonifiableSchemableClass, JsonifiableSchemableConfig } from ".." + + +// TODO: try to find a way to get rid of the 'class_' arg +export const jsonifySchemableSchema = < + C extends JsonifiableSchemableClass<$Config>, + $Config extends JsonifiableSchemableConfig, + S extends z.ZodType, z.ZodTypeDef, InstanceType>, +>( + class_: C | JsonifiableSchemableClass<$Config>, + schema: S, +) => + schema.pipe(class_.jsonifySchema) + +// TODO: try to find a way to get rid of the 'class_' arg +export const dejsonifySchemableSchema = < + C extends JsonifiableSchemableClass<$Config>, + $Config extends JsonifiableSchemableConfig, + S extends z.ZodType, z.ZodTypeDef, InstanceType>, +>( + class_: C | JsonifiableSchemableClass<$Config>, + schema: S, +) => + class_.dejsonifySchema.transform(v => new class_(v)).pipe(schema)