extendWithZodSchema tests
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:
102
src/extendWithZodSchema.ts
Normal file
102
src/extendWithZodSchema.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
import { AbstractClass, Class } from "type-fest"
|
||||
import { z } from "zod"
|
||||
import { StaticMembers } from "./util"
|
||||
|
||||
|
||||
type SchemableClass<
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
SchemaValues extends {},
|
||||
> = (
|
||||
AbstractClass<
|
||||
{
|
||||
readonly schema: z.ZodObject<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
SchemaValues,
|
||||
SchemaValues
|
||||
>
|
||||
} & SchemaValues,
|
||||
|
||||
any[]
|
||||
> & {
|
||||
readonly schema: z.ZodObject<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
SchemaValues,
|
||||
SchemaValues
|
||||
>
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
export function extendWithZodSchema<
|
||||
C extends SchemableClass<
|
||||
ExtendSchemaT,
|
||||
ExtendSchemaUnknownKeys,
|
||||
ExtendSchemaCatchall,
|
||||
ExtendSchemaValues
|
||||
>,
|
||||
|
||||
ExtendSchemaT extends z.ZodRawShape,
|
||||
ExtendSchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
ExtendSchemaCatchall extends z.ZodTypeAny,
|
||||
ExtendSchemaValues extends {},
|
||||
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
SchemaValues extends ExtendSchemaValues,
|
||||
>(
|
||||
extend: C | SchemableClass<
|
||||
ExtendSchemaT,
|
||||
ExtendSchemaUnknownKeys,
|
||||
ExtendSchemaCatchall,
|
||||
ExtendSchemaValues
|
||||
>,
|
||||
|
||||
schema: z.ZodObject<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
SchemaValues,
|
||||
SchemaValues
|
||||
>,
|
||||
) {
|
||||
|
||||
const class_ = class extends extend {
|
||||
static readonly schema = schema
|
||||
readonly schema = schema
|
||||
}
|
||||
|
||||
return class_ as unknown as (
|
||||
Class<
|
||||
Omit<C, "schema"> &
|
||||
{ readonly schema: typeof schema } &
|
||||
SchemaValues,
|
||||
|
||||
ConstructorParameters<C>
|
||||
> &
|
||||
Omit<StaticMembers<C>, "schema"> &
|
||||
{ readonly schema: typeof schema }
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
|
||||
const Test1Schema = z.object({ prout: z.string() })
|
||||
class Test1 {
|
||||
static readonly schema = Test1Schema
|
||||
readonly schema = Test1Schema
|
||||
|
||||
prout: string = "heugneu"
|
||||
}
|
||||
|
||||
const Test2Schema = Test1.schema.extend({ prout: z.literal("ruquier"), ruquier: z.number() })
|
||||
const Test2 = extendWithZodSchema(Test1, Test2Schema)
|
||||
|
||||
Test2.schema
|
||||
new Test2().schema
|
||||
Reference in New Issue
Block a user