This commit is contained in:
@@ -1,136 +1,34 @@
|
|||||||
import { Effect, pipe } from "effect"
|
import { expression } from "@thilawyn/traitify-ts"
|
||||||
import { AbstractClass, Class, Opaque } from "type-fest"
|
import { NoInfer } from "effect/Types"
|
||||||
|
import { AbstractClass, Opaque } from "type-fest"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { DefinedDefaultValuesTag, NewZodSchemaInstanceArgs, NewZodSchemaInstanceInput, TZodSchemaClass } from "."
|
import { DefinedDefaultValuesTag } from "."
|
||||||
import { ClassesInstances, ClassesStaticMembers, GetClassType, MergeInheritanceTree, MergeInheritanceTreeWithoutOverriding, parseZodTypeEffect } from "./util"
|
import { InstantiableZodSchemaObject } from "./traits/InstantiableZodSchemaObject"
|
||||||
import { Trait, abstract, trait } from "@thilawyn/traitify-ts"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
type T = Trait.Class<typeof InstantiableZodSchemaObject>
|
|
||||||
|
|
||||||
|
|
||||||
export function ZodSchemaClassOf<
|
export function ZodSchemaClassOf<
|
||||||
Super extends AbstractClass<any, []>,
|
Superclass extends AbstractClass<{}, []>,
|
||||||
|
|
||||||
SchemaT extends z.ZodRawShape,
|
SchemaT extends z.ZodRawShape,
|
||||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
SchemaCatchall extends z.ZodTypeAny,
|
SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
Values extends {},
|
Values extends {},
|
||||||
DefaultValues extends Partial<Values>,
|
DefaultValues extends NoInfer<Partial<Values>>,
|
||||||
>(
|
>(
|
||||||
of: Super,
|
of: Superclass,
|
||||||
|
|
||||||
{ schema, defaultValues }: {
|
{ schema, defaultValues }: {
|
||||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||||
defaultValues: Opaque<DefaultValues, DefinedDefaultValuesTag>
|
defaultValues: DefaultValues
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
type TZodSchemaClassImpl = TZodSchemaClass<
|
const exp = expression
|
||||||
SchemaT,
|
.extends(of)
|
||||||
SchemaUnknownKeys,
|
.expresses(InstantiableZodSchemaObject)
|
||||||
SchemaCatchall,
|
.build()
|
||||||
|
|
||||||
Values,
|
return exp
|
||||||
DefaultValues
|
|
||||||
>
|
|
||||||
|
|
||||||
return class extends (of as unknown as ConcreteClass<any, []>) {
|
|
||||||
static readonly schema = schema
|
|
||||||
static readonly defaultValues = defaultValues
|
|
||||||
|
|
||||||
constructor(values: Values) {
|
|
||||||
super()
|
|
||||||
Object.assign(this, values)
|
|
||||||
}
|
|
||||||
|
|
||||||
static create(
|
|
||||||
...[values, params]: NewZodSchemaInstanceArgs<
|
|
||||||
NewZodSchemaInstanceInput<Values, DefaultValues>
|
|
||||||
>
|
|
||||||
) {
|
|
||||||
return new this(
|
|
||||||
this.schema.parse({ ...this.defaultValues, ...values }, params)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
static async createPromise(
|
|
||||||
...[values, params]: NewZodSchemaInstanceArgs<
|
|
||||||
NewZodSchemaInstanceInput<Values, DefaultValues>
|
|
||||||
>
|
|
||||||
) {
|
|
||||||
return new this(
|
|
||||||
await this.schema.parseAsync({ ...this.defaultValues, ...values }, params)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
static createEffect(
|
|
||||||
...[values, params]: NewZodSchemaInstanceArgs<
|
|
||||||
NewZodSchemaInstanceInput<Values, DefaultValues>
|
|
||||||
>
|
|
||||||
) {
|
|
||||||
return pipe(
|
|
||||||
parseZodTypeEffect(
|
|
||||||
this.schema,
|
|
||||||
{ ...this.defaultValues, ...values },
|
|
||||||
params,
|
|
||||||
),
|
|
||||||
|
|
||||||
Effect.map(values => new this(values)),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
static extend<
|
|
||||||
ExtendedSchemaT extends z.ZodRawShape,
|
|
||||||
ExtendedSchemaUnknownKeys extends z.UnknownKeysParam,
|
|
||||||
ExtendedSchemaCatchall extends z.ZodTypeAny,
|
|
||||||
|
|
||||||
ExtendedValues extends Values,
|
|
||||||
ExtendedDefaultValues extends Partial<ExtendedValues>,
|
|
||||||
>(
|
|
||||||
props: {
|
|
||||||
schema: (props: {
|
|
||||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
|
||||||
shape: SchemaT
|
|
||||||
}) => z.ZodObject<ExtendedSchemaT, ExtendedSchemaUnknownKeys, ExtendedSchemaCatchall, ExtendedValues, ExtendedValues>
|
|
||||||
|
|
||||||
defaultValues: (defaultValues: DefaultValues) => Opaque<ExtendedDefaultValues, DefinedDefaultValuesTag>
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
const schema = props.schema({
|
|
||||||
schema: this.schema,
|
|
||||||
shape: this.schema.shape,
|
|
||||||
})
|
|
||||||
|
|
||||||
const defaultValues = props.defaultValues(this.defaultValues)
|
|
||||||
|
|
||||||
return class extends (this as unknown as ConcreteClass<any, []>) {
|
|
||||||
static readonly schema = schema
|
|
||||||
static readonly defaultValues = defaultValues
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} as unknown as (
|
|
||||||
Class<
|
|
||||||
GetClassType<Super>,
|
|
||||||
|
|
||||||
MergeInheritanceTreeWithoutOverriding<
|
|
||||||
ClassesInstances<[
|
|
||||||
Super,
|
|
||||||
TZodSchemaClassImpl,
|
|
||||||
]>
|
|
||||||
> &
|
|
||||||
|
|
||||||
ConstructorParameters<TZodSchemaClassImpl>
|
|
||||||
> &
|
|
||||||
|
|
||||||
MergeInheritanceTree<
|
|
||||||
ClassesStaticMembers<[
|
|
||||||
Super,
|
|
||||||
TZodSchemaClassImpl,
|
|
||||||
]>
|
|
||||||
>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user