91 lines
3.8 KiB
TypeScript
91 lines
3.8 KiB
TypeScript
import { TraitStaticMembers, trait } from "@thilawyn/traitify-ts"
|
|
import { HasRequiredKeys } from "type-fest"
|
|
import { z } from "zod"
|
|
import { ZodSchemaClass } from "../shapes/ZodSchemaClass"
|
|
import { parseZodTypeEffect } from "../util"
|
|
import { ZodSchemaObjectInstantiationSchemas } from "./ZodSchemaObjectInstantiationSchemas"
|
|
|
|
|
|
type ZodSchemaObjectInstantiationSchemasStaticMembers = TraitStaticMembers<typeof ZodSchemaObjectInstantiationSchemas>
|
|
|
|
type NewZodSchemaInstanceInput<
|
|
Values extends object,
|
|
DefaultValues extends Partial<Values>,
|
|
> = {
|
|
[Key in Exclude<keyof Values, keyof DefaultValues>]: Values[Key]
|
|
} & {
|
|
[Key in keyof DefaultValues]?: Key extends keyof Values
|
|
? Values[Key]
|
|
: never
|
|
}
|
|
|
|
type ParseParamsArgs = [] | [params: Partial<z.ParseParams>]
|
|
|
|
type NewZodSchemaInstanceArgs<Input extends object> = (
|
|
HasRequiredKeys<Input> extends true
|
|
? [values: Input, ...args: ParseParamsArgs]
|
|
: [] | [values: Input, ...args: ParseParamsArgs]
|
|
)
|
|
|
|
|
|
export const InstantiableZodSchemaObject = trait
|
|
.implement(Super => class InstantiableZodSchemaObject extends Super {
|
|
static create<
|
|
Instance extends Values,
|
|
|
|
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
|
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
|
|
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
|
|
|
Values extends object,
|
|
PartialValues extends Partial<Values>,
|
|
>(
|
|
this: (
|
|
ZodSchemaClass<Instance, any, any, any, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues> &
|
|
ZodSchemaObjectInstantiationSchemasStaticMembers
|
|
),
|
|
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
|
|
) {
|
|
return this.instantiationSchemaWithDefaultValues().parse(values, params)
|
|
}
|
|
|
|
static async createPromise<
|
|
Instance extends Values,
|
|
|
|
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
|
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
|
|
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
|
|
|
Values extends object,
|
|
PartialValues extends Partial<Values>,
|
|
>(
|
|
this: (
|
|
ZodSchemaClass<Instance, any, any, any, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues> &
|
|
ZodSchemaObjectInstantiationSchemasStaticMembers
|
|
),
|
|
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
|
|
) {
|
|
return this.instantiationSchemaWithDefaultValues().parseAsync(values, params)
|
|
}
|
|
|
|
static createEffect<
|
|
Instance extends Values,
|
|
|
|
SchemaWithDefaultValuesT extends z.ZodRawShape,
|
|
SchemaWithDefaultValuesUnknownKeys extends z.UnknownKeysParam,
|
|
SchemaWithDefaultValuesCatchall extends z.ZodTypeAny,
|
|
|
|
Values extends object,
|
|
PartialValues extends Partial<Values>,
|
|
>(
|
|
this: (
|
|
ZodSchemaClass<Instance, any, any, any, SchemaWithDefaultValuesT, SchemaWithDefaultValuesUnknownKeys, SchemaWithDefaultValuesCatchall, Values, PartialValues> &
|
|
ZodSchemaObjectInstantiationSchemasStaticMembers
|
|
),
|
|
...[values, params]: NewZodSchemaInstanceArgs<PartialValues>
|
|
) {
|
|
return parseZodTypeEffect(this.instantiationSchemaWithDefaultValues(), values, params)
|
|
}
|
|
})
|
|
.build()
|