0.1.2 #3
@@ -1,136 +1,34 @@
|
||||
import { Effect, pipe } from "effect"
|
||||
import { AbstractClass, Class, Opaque } from "type-fest"
|
||||
import { expression } from "@thilawyn/traitify-ts"
|
||||
import { NoInfer } from "effect/Types"
|
||||
import { AbstractClass, Opaque } from "type-fest"
|
||||
import { z } from "zod"
|
||||
import { DefinedDefaultValuesTag, NewZodSchemaInstanceArgs, NewZodSchemaInstanceInput, TZodSchemaClass } from "."
|
||||
import { ClassesInstances, ClassesStaticMembers, GetClassType, MergeInheritanceTree, MergeInheritanceTreeWithoutOverriding, parseZodTypeEffect } from "./util"
|
||||
import { Trait, abstract, trait } from "@thilawyn/traitify-ts"
|
||||
|
||||
|
||||
|
||||
type T = Trait.Class<typeof InstantiableZodSchemaObject>
|
||||
import { DefinedDefaultValuesTag } from "."
|
||||
import { InstantiableZodSchemaObject } from "./traits/InstantiableZodSchemaObject"
|
||||
|
||||
|
||||
export function ZodSchemaClassOf<
|
||||
Super extends AbstractClass<any, []>,
|
||||
Superclass extends AbstractClass<{}, []>,
|
||||
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
Values extends {},
|
||||
DefaultValues extends Partial<Values>,
|
||||
DefaultValues extends NoInfer<Partial<Values>>,
|
||||
>(
|
||||
of: Super,
|
||||
of: Superclass,
|
||||
|
||||
{ schema, defaultValues }: {
|
||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||
defaultValues: Opaque<DefaultValues, DefinedDefaultValuesTag>
|
||||
defaultValues: DefaultValues
|
||||
},
|
||||
) {
|
||||
type TZodSchemaClassImpl = TZodSchemaClass<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
const exp = expression
|
||||
.extends(of)
|
||||
.expresses(InstantiableZodSchemaObject)
|
||||
.build()
|
||||
|
||||
Values,
|
||||
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,
|
||||
]>
|
||||
>
|
||||
)
|
||||
return exp
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user