Schemable JSON helpers

This commit is contained in:
Julien Valverdé
2024-01-02 03:37:09 +01:00
parent 0e9d946696
commit 713e679e35
2 changed files with 26 additions and 0 deletions

View File

@@ -1,3 +1,4 @@
export * from "./bigint" export * from "./bigint"
export * from "./date" export * from "./date"
export * from "./decimal" export * from "./decimal"
export * from "./schemable"

View File

@@ -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<InstanceType<C>, z.ZodTypeDef, InstanceType<C>>,
>(
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<InstanceType<C>, z.ZodTypeDef, InstanceType<C>>,
>(
class_: C | JsonifiableSchemableClass<$Config>,
schema: S,
) =>
class_.dejsonifySchema.transform(v => new class_(v)).pipe(schema)