extendSchemable work
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Julien Valverdé
2024-01-09 03:06:52 +01:00
parent 1bdfc079b9
commit 6fe782208d
4 changed files with 91 additions and 57 deletions

38
src/SchemableClass.ts Normal file
View File

@@ -0,0 +1,38 @@
import { AbstractClass } from "type-fest"
import { z } from "zod"
export type SchemableClass<
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaValues extends {},
DefaultValues extends Partial<SchemaValues>,
> = (
AbstractClass<
{
readonly schema: z.ZodObject<
SchemaT,
SchemaUnknownKeys,
SchemaCatchall,
SchemaValues,
SchemaValues
>
readonly defaultValues: DefaultValues
} & SchemaValues,
any[]
> & {
readonly schema: z.ZodObject<
SchemaT,
SchemaUnknownKeys,
SchemaCatchall,
SchemaValues,
SchemaValues
>
readonly defaultValues: DefaultValues
}
)

View File

@@ -1,102 +1,73 @@
import { AbstractClass, Class } from "type-fest" import { AbstractClass } from "type-fest"
import { z } from "zod" import { z } from "zod"
import { SchemableClass } from "."
import { StaticMembers } from "./util" 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 extendSchemable< export function extendSchemable<
C extends SchemableClass< C extends SchemableClass<
ExtendSchemaT, ExtendSchemaT,
ExtendSchemaUnknownKeys, ExtendSchemaUnknownKeys,
ExtendSchemaCatchall, ExtendSchemaCatchall,
ExtendSchemaValues ExtendSchemaValues,
ExtendDefaultValues
>, >,
ExtendSchemaT extends z.ZodRawShape, ExtendSchemaT extends z.ZodRawShape,
ExtendSchemaUnknownKeys extends z.UnknownKeysParam, ExtendSchemaUnknownKeys extends z.UnknownKeysParam,
ExtendSchemaCatchall extends z.ZodTypeAny, ExtendSchemaCatchall extends z.ZodTypeAny,
ExtendSchemaValues extends {}, ExtendSchemaValues extends {},
ExtendDefaultValues extends Partial<ExtendSchemaValues>,
SchemaT extends z.ZodRawShape, SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam, SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny, SchemaCatchall extends z.ZodTypeAny,
SchemaValues extends ExtendSchemaValues, SchemaValues extends ExtendSchemaValues,
DefaultValues extends Partial<SchemaValues>,
>( >(
extend: C | SchemableClass< extend: C | SchemableClass<
ExtendSchemaT, ExtendSchemaT,
ExtendSchemaUnknownKeys, ExtendSchemaUnknownKeys,
ExtendSchemaCatchall, ExtendSchemaCatchall,
ExtendSchemaValues ExtendSchemaValues,
ExtendDefaultValues
>, >,
schema: z.ZodObject< schemaApplier: (schema: C["schema"]) => z.ZodObject<
SchemaT, SchemaT,
SchemaUnknownKeys, SchemaUnknownKeys,
SchemaCatchall, SchemaCatchall,
SchemaValues, SchemaValues,
SchemaValues SchemaValues
>, >,
defaultValuesApplier: (defaultValues: ExtendDefaultValues) => DefaultValues,
) { ) {
const schema = schemaApplier(extend.schema)
const defaultValues = defaultValuesApplier(extend.defaultValues)
return class extends extend { return class extends extend {
static readonly schema = schema static readonly schema = schema
readonly schema = schema readonly schema = schema
static readonly defaultValues = defaultValues
readonly defaultValues = defaultValues
} as unknown as ( } as unknown as (
Class< AbstractClass<
Omit< Omit<C, "schema" | "defaultValues" | keyof ExtendSchemaValues> &
Omit<C, "schema">, {
keyof ExtendSchemaValues readonly schema: typeof schema,
> & readonly defaultValues: typeof defaultValues,
{ readonly schema: typeof schema } & } &
SchemaValues, SchemaValues,
ConstructorParameters<C> ConstructorParameters<C>
> & > &
Omit<StaticMembers<C>, "schema"> & Omit<StaticMembers<C>, "schema" | "defaultValues"> &
{ readonly schema: typeof schema } {
readonly schema: typeof schema,
readonly defaultValues: typeof defaultValues,
}
) )
} }
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 = extendSchemable(Test1, Test2Schema)
Test2.schema
new Test2().prout

2
src/index.ts Normal file
View File

@@ -0,0 +1,2 @@
export * from "./SchemableClass"
export * from "./extendSchemable"

View File

@@ -0,0 +1,23 @@
import { z } from "zod"
import { extendSchemable } from "."
const Test1Schema = z.object({ prout: z.string() })
class Test1 {
static readonly schema = Test1Schema
readonly schema = Test1Schema
static readonly defaultValues = { prout: "heugneu" }
readonly defaultValues = { prout: "heugneu" }
prout: string = "heugneu"
}
const Test2 = extendSchemable(
Test1,
schema => schema.extend({ prout: z.literal("ruquier"), ruquier: z.number() }),
defaultValues => ({ prout: "ruquier" as const }),
)
Test2.defaultValues
new Test2().prout