0.1.1 #2
38
src/SchemableClass.ts
Normal file
38
src/SchemableClass.ts
Normal 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
|
||||
}
|
||||
)
|
||||
@@ -1,102 +1,73 @@
|
||||
import { AbstractClass, Class } from "type-fest"
|
||||
import { AbstractClass } from "type-fest"
|
||||
import { z } from "zod"
|
||||
import { SchemableClass } from "."
|
||||
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<
|
||||
C extends SchemableClass<
|
||||
ExtendSchemaT,
|
||||
ExtendSchemaUnknownKeys,
|
||||
ExtendSchemaCatchall,
|
||||
ExtendSchemaValues
|
||||
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
|
||||
ExtendSchemaValues,
|
||||
ExtendDefaultValues
|
||||
>,
|
||||
|
||||
schema: z.ZodObject<
|
||||
schemaApplier: (schema: C["schema"]) => z.ZodObject<
|
||||
SchemaT,
|
||||
SchemaUnknownKeys,
|
||||
SchemaCatchall,
|
||||
SchemaValues,
|
||||
SchemaValues
|
||||
>,
|
||||
|
||||
defaultValuesApplier: (defaultValues: ExtendDefaultValues) => DefaultValues,
|
||||
) {
|
||||
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 (
|
||||
Class<
|
||||
Omit<
|
||||
Omit<C, "schema">,
|
||||
keyof ExtendSchemaValues
|
||||
> &
|
||||
{ readonly schema: typeof schema } &
|
||||
AbstractClass<
|
||||
Omit<C, "schema" | "defaultValues" | keyof ExtendSchemaValues> &
|
||||
{
|
||||
readonly schema: typeof schema,
|
||||
readonly defaultValues: typeof defaultValues,
|
||||
} &
|
||||
SchemaValues,
|
||||
|
||||
ConstructorParameters<C>
|
||||
> &
|
||||
|
||||
Omit<StaticMembers<C>, "schema"> &
|
||||
{ readonly schema: typeof schema }
|
||||
Omit<StaticMembers<C>, "schema" | "defaultValues"> &
|
||||
{
|
||||
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
2
src/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./SchemableClass"
|
||||
export * from "./extendSchemable"
|
||||
23
src/tests.ts
23
src/tests.ts
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user