From f731ed407f1e122d81b6bd10fe9208a7bcfd1698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Sat, 24 Feb 2024 00:54:46 +0100 Subject: [PATCH] JsonifiableZodSchemaObject --- src/traits/JsonifiableZodSchemaObject.ts | 63 +++++++++++++++++++++++- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/src/traits/JsonifiableZodSchemaObject.ts b/src/traits/JsonifiableZodSchemaObject.ts index 258e69f..ab0f02e 100644 --- a/src/traits/JsonifiableZodSchemaObject.ts +++ b/src/traits/JsonifiableZodSchemaObject.ts @@ -1,8 +1,7 @@ import { trait } from "@thilawyn/traitify-ts" -import { Effect } from "effect" import { JsonifiableObject } from "type-fest/source/jsonifiable" import { z } from "zod" -import { JsonifiableZodSchemaClass, JsonifiableZodSchemas } from "../shapes/JsonifiableZodSchemaClass" +import { JsonifiableZodSchemas } from "../shapes/JsonifiableZodSchemaClass" import { parseZodTypeEffect } from "../util" @@ -37,5 +36,65 @@ export const JsonifiableZodSchemaObject = trait ) { return this.jsonifySchema.parse(this, params) } + + async jsonifyPromise< + JsonifySchemaT extends z.ZodRawShape, + JsonifySchemaUnknownKeys extends z.UnknownKeysParam, + JsonifySchemaCatchall extends z.ZodTypeAny, + + DejsonifySchemaT extends z.ZodRawShape, + DejsonifySchemaUnknownKeys extends z.UnknownKeysParam, + DejsonifySchemaCatchall extends z.ZodTypeAny, + + JsonifiedValues extends JsonifiableObject, + Values extends object, + >( + this: Values & JsonifiableZodSchemas< + JsonifySchemaT, + JsonifySchemaUnknownKeys, + JsonifySchemaCatchall, + + DejsonifySchemaT, + DejsonifySchemaUnknownKeys, + DejsonifySchemaCatchall, + + JsonifiedValues, + Values + >, + + params?: Partial, + ) { + return this.dejsonifySchema.parseAsync(this, params) + } + + jsonifyEffect< + JsonifySchemaT extends z.ZodRawShape, + JsonifySchemaUnknownKeys extends z.UnknownKeysParam, + JsonifySchemaCatchall extends z.ZodTypeAny, + + DejsonifySchemaT extends z.ZodRawShape, + DejsonifySchemaUnknownKeys extends z.UnknownKeysParam, + DejsonifySchemaCatchall extends z.ZodTypeAny, + + JsonifiedValues extends JsonifiableObject, + Values extends object, + >( + this: Values & JsonifiableZodSchemas< + JsonifySchemaT, + JsonifySchemaUnknownKeys, + JsonifySchemaCatchall, + + DejsonifySchemaT, + DejsonifySchemaUnknownKeys, + DejsonifySchemaCatchall, + + JsonifiedValues, + Values + >, + + params?: Partial, + ) { + return parseZodTypeEffect(this.jsonifySchema, this, params) + } }) .build()