extendSchemableClass work
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
79
src/extendSchemableClass.ts
Normal file
79
src/extendSchemableClass.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { AbstractClass, Class } from "type-fest"
|
||||
import { z } from "zod"
|
||||
import { SchemableClass } from "."
|
||||
import { StaticMembers } from "./util"
|
||||
|
||||
|
||||
export function extendSchemableClass<
|
||||
C extends SchemableClass<
|
||||
ExtendSchemaT,
|
||||
ExtendSchemaUnknownKeys,
|
||||
ExtendSchemaCatchall,
|
||||
ExtendSchemaValues,
|
||||
ExtendDefaultValues
|
||||
>,
|
||||
|
||||
ExtendSchemaT extends z.ZodRawShape,
|
||||
ExtendSchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
ExtendSchemaCatchall extends z.ZodTypeAny,
|
||||
ExtendSchemaValues extends {},
|
||||
ExtendDefaultValues extends Partial<ExtendSchemaValues>,
|
||||
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
SchemaValues extends ExtendSchemaValues,
|
||||
DefaultValues extends Partial<SchemaValues>,
|
||||
>(
|
||||
extend: C | SchemableClass<
|
||||
ExtendSchemaT,
|
||||
ExtendSchemaUnknownKeys,
|
||||
ExtendSchemaCatchall,
|
||||
ExtendSchemaValues,
|
||||
ExtendDefaultValues
|
||||
>,
|
||||
|
||||
schemaApplier: (schema: C["schema"]) => z.ZodObject<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
SchemaValues,
|
||||
SchemaValues
|
||||
>,
|
||||
|
||||
defaultValuesApplier: (defaultValues: ExtendDefaultValues) => DefaultValues,
|
||||
) {
|
||||
type ClassKind<T, Arguments extends unknown[]> = (
|
||||
C extends Class<any>
|
||||
? Class<T, Arguments>
|
||||
: AbstractClass<T, Arguments>
|
||||
)
|
||||
|
||||
const schema = schemaApplier(extend.schema)
|
||||
const defaultValues = defaultValuesApplier(extend.defaultValues)
|
||||
|
||||
return class extends extend {
|
||||
static readonly schema = schema
|
||||
readonly schema = schema
|
||||
|
||||
static readonly defaultValues = defaultValues
|
||||
readonly defaultValues = defaultValues
|
||||
} as unknown as (
|
||||
ClassKind<
|
||||
Omit<C, "schema" | "defaultValues" | keyof ExtendSchemaValues> &
|
||||
{
|
||||
readonly schema: typeof schema,
|
||||
readonly defaultValues: typeof defaultValues,
|
||||
} &
|
||||
SchemaValues,
|
||||
|
||||
ConstructorParameters<C>
|
||||
> &
|
||||
|
||||
Omit<StaticMembers<C>, "schema" | "defaultValues"> &
|
||||
{
|
||||
readonly schema: typeof schema,
|
||||
readonly defaultValues: typeof defaultValues,
|
||||
}
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user