Refactoring
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julien Valverdé
2024-01-28 02:33:05 +01:00
parent a4b8166af8
commit e35db63da8
2 changed files with 21 additions and 17 deletions

View File

@@ -2,7 +2,7 @@ import { Effect } from "effect"
import { AbstractClass, Class as ConcreteClass, HasRequiredKeys, Opaque } from "type-fest"
import { z } from "zod"
import { DefinedDefaultValuesTag } from "."
import { Class, ClassType, GetClassType, StaticMembers } from "./util"
import { Class, ClassType, ClassesInstances, ClassesStaticMembers, GetClassType, MergeInheritanceTree } from "./util"
export type NewZodSchemaInstanceInput<
@@ -25,20 +25,22 @@ export type NewZodSchemaInstanceArgs<Input extends object> =
type ExtendZodSchemaClass<
Parent extends AbstractClass<any>,
Super extends AbstractClass<any>,
Self extends AbstractClass<any>,
> = (
Class<
GetClassType<Parent>,
GetClassType<Super>,
Omit<InstanceType<Parent>, keyof InstanceType<DefaultZodSchemaClass>> &
InstanceType<Self>,
MergeInheritanceTree<
ClassesInstances<[Super, Self]>
>,
ConstructorParameters<Self>
> &
Omit<Parent, keyof DefaultZodSchemaClass> &
StaticMembers<Self>
MergeInheritanceTree<
ClassesStaticMembers<[Super, Self]>
>
)
@@ -82,7 +84,7 @@ export type TZodSchemaClass<
): Effect.Effect<never, z.ZodError<Values>, Values>
extend<
Parent extends AbstractClass<any>,
Super extends AbstractClass<any>,
ExtendedSchemaT extends z.ZodRawShape,
ExtendedSchemaUnknownKeys extends z.UnknownKeysParam,
@@ -91,7 +93,7 @@ export type TZodSchemaClass<
ExtendedValues extends Values,
ExtendedDefaultValues extends Partial<ExtendedValues>,
>(
this: Parent,
this: Super,
props: {
schema: (props: {
@@ -102,7 +104,7 @@ export type TZodSchemaClass<
defaultValues: (defaultValues: DefaultValues) => Opaque<ExtendedDefaultValues, DefinedDefaultValuesTag>
},
): ExtendZodSchemaClass<
Parent,
Super,
TZodSchemaClass<
ExtendedSchemaT,

View File

@@ -6,7 +6,7 @@ import { Class, GetClassType, MergeInheritanceTree, MergeInheritanceTreeWithoutO
export function ZodSchemaClassOf<
Parent extends AbstractClass<any, []>,
Super extends AbstractClass<any, []>,
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
@@ -15,7 +15,7 @@ export function ZodSchemaClassOf<
Values extends {},
DefaultValues extends Partial<Values>,
>(
of: Parent,
of: Super,
{ schema, defaultValues }: {
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
@@ -107,10 +107,10 @@ export function ZodSchemaClassOf<
}
} as unknown as (
Class<
GetClassType<Parent>,
GetClassType<Super>,
MergeInheritanceTreeWithoutOverriding<[
InstanceType<Parent>,
InstanceType<Super>,
InstanceType<TZodSchemaClassImpl>,
]> &
@@ -118,13 +118,15 @@ export function ZodSchemaClassOf<
> &
MergeInheritanceTree<[
StaticMembers<Parent>,
StaticMembers<Super>,
StaticMembers<TZodSchemaClassImpl>,
]>
)
}
class DefaultRoot {}
export function ZodSchemaClass<
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
@@ -138,5 +140,5 @@ export function ZodSchemaClass<
defaultValues: Opaque<DefaultValues, DefinedDefaultValuesTag>
},
) {
return ZodSchemaClassOf(Object, props)
return ZodSchemaClassOf(DefaultRoot, props)
}