From 0b0af7b128967a739bbc8a5a8cb0eef35812e5b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Sun, 25 Feb 2024 23:57:46 +0100 Subject: [PATCH] ZodSchemaClassExtender.jsonifiable work --- src/builders/ZodSchemaClassExtender.ts | 107 ++++++++++++++++--------- 1 file changed, 69 insertions(+), 38 deletions(-) diff --git a/src/builders/ZodSchemaClassExtender.ts b/src/builders/ZodSchemaClassExtender.ts index 5ba03b9..fc24961 100644 --- a/src/builders/ZodSchemaClassExtender.ts +++ b/src/builders/ZodSchemaClassExtender.ts @@ -1,10 +1,10 @@ import { expression } from "@thilawyn/traitify-ts" import { AbstractClass, Simplify } from "type-fest" +import { JsonifiableObject } from "type-fest/source/jsonifiable" import { z } from "zod" +import { JsonifiableZodSchemaAbstractClass } from "../shapes/JsonifiableZodSchemaClass" import { ZodSchemaAbstractClass } from "../shapes/ZodSchemaClass" import { Extend, Override, StaticMembers } from "../util" -import { JsonifiableZodSchemaAbstractClass, JsonifiableZodSchemaClass } from "../shapes/JsonifiableZodSchemaClass" -import { JsonifiableObject } from "type-fest/source/jsonifiable" export class ZodSchemaClassExtender> { @@ -66,22 +66,44 @@ export class ZodSchemaClassExtender> { } jsonifiable< - Super extends JsonifiableZodSchemaAbstractClass< - Instance, + Super extends ( + ZodSchemaAbstractClass & + JsonifiableZodSchemaAbstractClass< + SuperInstance, - JsonifySchemaT, - JsonifySchemaUnknownKeys, - JsonifySchemaCatchall, + SuperJsonifySchemaT, + SuperJsonifySchemaUnknownKeys, + SuperJsonifySchemaCatchall, - DejsonifySchemaT, - DejsonifySchemaUnknownKeys, - DejsonifySchemaCatchall, + SuperDejsonifySchemaT, + SuperDejsonifySchemaUnknownKeys, + SuperDejsonifySchemaCatchall, - JsonifiedValues, - Values - >, - Instance extends Values, + SuperJsonifiedValues, + SuperValues + > + ), + SuperInstance extends SuperValues, + + SuperSchemaT extends z.ZodRawShape, + SuperSchemaUnknownKeys extends z.UnknownKeysParam, + SuperSchemaCatchall extends z.ZodTypeAny, + + SuperValues extends object, + SuperDefaultValues extends Partial, + + SuperJsonifySchemaT extends z.ZodRawShape, + SuperJsonifySchemaUnknownKeys extends z.UnknownKeysParam, + SuperJsonifySchemaCatchall extends z.ZodTypeAny, + + SuperDejsonifySchemaT extends z.ZodRawShape, + SuperDejsonifySchemaUnknownKeys extends z.UnknownKeysParam, + SuperDejsonifySchemaCatchall extends z.ZodTypeAny, + + SuperJsonifiedValues extends JsonifiableObject, + + /* New jsonifiable schemas */ JsonifySchemaT extends z.ZodRawShape, JsonifySchemaUnknownKeys extends z.UnknownKeysParam, JsonifySchemaCatchall extends z.ZodTypeAny, @@ -90,56 +112,65 @@ export class ZodSchemaClassExtender> { DejsonifySchemaUnknownKeys extends z.UnknownKeysParam, DejsonifySchemaCatchall extends z.ZodTypeAny, - JsonifiedValues extends JsonifiableObject, - Values extends object, + JsonifiedValues extends SuperJsonifiedValues, + Values extends SuperValues, >( this: ZodSchemaClassExtender< - Super | JsonifiableZodSchemaAbstractClass< - Instance, + Super | ( + ZodSchemaAbstractClass & + JsonifiableZodSchemaAbstractClass< + SuperInstance, - JsonifySchemaT, - JsonifySchemaUnknownKeys, - JsonifySchemaCatchall, + SuperJsonifySchemaT, + SuperJsonifySchemaUnknownKeys, + SuperJsonifySchemaCatchall, - DejsonifySchemaT, - DejsonifySchemaUnknownKeys, - DejsonifySchemaCatchall, + SuperDejsonifySchemaT, + SuperDejsonifySchemaUnknownKeys, + SuperDejsonifySchemaCatchall, - JsonifiedValues, - Values - > + SuperJsonifiedValues, + SuperValues + > + ) >, props: { jsonifySchema: ( - schema: z.ZodObject + schema: Super["schema"], + jsonifySchema: Super["jsonifySchema"], ) => z.ZodObject dejsonifySchema: ( - schema: z.ZodObject + schema: Super["schema"], + dejsonifySchema: Super["dejsonifySchema"], ) => z.ZodObject }, ) { - const schema = props.schema(this.superclass.schema) - const defaultValues = props.defaultValues(this.superclass.defaultValues) + const jsonifySchema = props.jsonifySchema(this.superclass.schema, this.superclass.jsonifySchema) + const dejsonifySchema = props.dejsonifySchema(this.superclass.schema, this.superclass.dejsonifySchema) - class Schemas extends (this.superclass as AbstractClass) { - static readonly schema = schema - static readonly defaultValues = defaultValues + class JsonifiableSchemas extends (this.superclass as AbstractClass) { + static readonly jsonifySchema = jsonifySchema + readonly jsonifySchema = jsonifySchema + static readonly dejsonifySchema = dejsonifySchema + readonly dejsonifySchema = dejsonifySchema } return new this.constructor( - Schemas as unknown as AbstractClass< + JsonifiableSchemas as unknown as AbstractClass< Simplify< - Extend<[SuperInstance, Values]> + Override<[SuperInstance, JsonifiableSchemas]> >, - [values: Values] + ConstructorParameters< + ZodSchemaAbstractClass + > > & Simplify< Override<[ StaticMembers, - StaticMembers, + StaticMembers, ]> > )