0.1.1 #2
@@ -60,14 +60,14 @@ export function extendSchemableClass<
|
|||||||
readonly defaultValues = defaultValues
|
readonly defaultValues = defaultValues
|
||||||
} as unknown as (
|
} as unknown as (
|
||||||
ClassKind<
|
ClassKind<
|
||||||
Omit<C, "schema" | "defaultValues" | keyof ExtendSchemaValues> &
|
Omit<InstanceType<C>, "schema" | "defaultValues" | keyof ExtendSchemaValues> &
|
||||||
{
|
{
|
||||||
readonly schema: typeof schema,
|
readonly schema: typeof schema,
|
||||||
readonly defaultValues: typeof defaultValues,
|
readonly defaultValues: typeof defaultValues,
|
||||||
} &
|
} &
|
||||||
SchemaValues,
|
SchemaValues,
|
||||||
|
|
||||||
ConstructorParameters<C>
|
Parameters<(values: SchemaValues) => void>
|
||||||
> &
|
> &
|
||||||
|
|
||||||
Omit<StaticMembers<C>, "schema" | "defaultValues"> &
|
Omit<StaticMembers<C>, "schema" | "defaultValues"> &
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
export * from "./SchemableClass"
|
export * from "./SchemableClass"
|
||||||
export * from "./extendSchemableClass"
|
export * from "./extendSchemableClass"
|
||||||
|
export * from "./makeSchemableClass"
|
||||||
|
|||||||
62
src/makeSchemableClass.ts
Normal file
62
src/makeSchemableClass.ts
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import { AbstractClass, Class } from "type-fest"
|
||||||
|
import { z } from "zod"
|
||||||
|
import { StaticMembers } from "./util"
|
||||||
|
|
||||||
|
|
||||||
|
export function makeSchemableClassFrom<
|
||||||
|
C extends AbstractClass<any, []>,
|
||||||
|
|
||||||
|
SchemaT extends z.ZodRawShape,
|
||||||
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
|
SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
SchemaValues extends {},
|
||||||
|
DefaultValues extends Partial<SchemaValues>,
|
||||||
|
>(
|
||||||
|
extend: C,
|
||||||
|
|
||||||
|
schema: z.ZodObject<
|
||||||
|
SchemaT,
|
||||||
|
SchemaUnknownKeys,
|
||||||
|
SchemaCatchall,
|
||||||
|
SchemaValues,
|
||||||
|
SchemaValues
|
||||||
|
>,
|
||||||
|
|
||||||
|
defaultValues: DefaultValues,
|
||||||
|
) {
|
||||||
|
type ClassKind<T, Arguments extends unknown[]> = (
|
||||||
|
C extends Class<any>
|
||||||
|
? Class<T, Arguments>
|
||||||
|
: AbstractClass<T, Arguments>
|
||||||
|
)
|
||||||
|
|
||||||
|
return class extends (extend as unknown as Class<any, []>) {
|
||||||
|
static readonly schema = schema
|
||||||
|
readonly schema = schema
|
||||||
|
|
||||||
|
static readonly defaultValues = defaultValues
|
||||||
|
readonly defaultValues = defaultValues
|
||||||
|
|
||||||
|
constructor(values: SchemaValues) {
|
||||||
|
super()
|
||||||
|
Object.assign(this, values)
|
||||||
|
}
|
||||||
|
} as unknown as (
|
||||||
|
ClassKind<
|
||||||
|
Omit<InstanceType<C>, "schema" | "defaultValues"> &
|
||||||
|
{
|
||||||
|
readonly schema: typeof schema,
|
||||||
|
readonly defaultValues: typeof defaultValues,
|
||||||
|
} &
|
||||||
|
SchemaValues,
|
||||||
|
|
||||||
|
Parameters<(values: SchemaValues) => void>
|
||||||
|
> &
|
||||||
|
|
||||||
|
Omit<StaticMembers<C>, "schema" | "defaultValues"> &
|
||||||
|
{
|
||||||
|
readonly schema: typeof schema,
|
||||||
|
readonly defaultValues: typeof defaultValues,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user