This commit is contained in:
@@ -1,59 +1,13 @@
|
||||
import { Effect, pipe } from "effect"
|
||||
import { AbstractClass, Class as ConcreteClass, Opaque } from "type-fest"
|
||||
import { AbstractClass, Class, Opaque } from "type-fest"
|
||||
import { z } from "zod"
|
||||
import { DefinedDefaultValuesTag, NewZodSchemaInstanceArgs, NewZodSchemaInstanceInput, TZodSchemaClass } from "."
|
||||
import { Class, ClassesInstances, ClassesStaticMembers, GetClassType, MergeInheritanceTree, MergeInheritanceTreeWithoutOverriding, parseZodTypeEffect } from "./util"
|
||||
import { abstract, trait } from "@thilawyn/traitify-ts"
|
||||
import { ClassesInstances, ClassesStaticMembers, GetClassType, MergeInheritanceTree, MergeInheritanceTreeWithoutOverriding, parseZodTypeEffect } from "./util"
|
||||
import { Trait, abstract, trait } from "@thilawyn/traitify-ts"
|
||||
|
||||
|
||||
export const InstantiableZodSchemaObject = <
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
Values extends {},
|
||||
DefaultValues extends Partial<Values>,
|
||||
>() => trait(
|
||||
abstract(),
|
||||
|
||||
Super => class InstantiableZodSchemaObject extends Super {
|
||||
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)),
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
type T = Trait.Class<typeof InstantiableZodSchemaObject>
|
||||
|
||||
|
||||
export function ZodSchemaClassOf<
|
||||
|
||||
Reference in New Issue
Block a user