This commit is contained in:
181
src/builders/ZodSchemaClassBuilder.ts
Normal file
181
src/builders/ZodSchemaClassBuilder.ts
Normal file
@@ -0,0 +1,181 @@
|
||||
import { Trait, TraitExpression, TraitExpressionBuilder } from "@thilawyn/traitify-ts"
|
||||
import { AbstractClass } from "type-fest"
|
||||
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
||||
import { z } from "zod"
|
||||
import { ZodSchemaAbstractClass } from "../shapes/ZodSchemaClass"
|
||||
import { DejsonifiableZodSchemaObject } from "../traits/DejsonifiableZodSchemaObject"
|
||||
import { ExtendableZodSchemaObject } from "../traits/ExtendableZodSchemaObject"
|
||||
import { InstantiableZodSchemaObject } from "../traits/InstantiableZodSchemaObject"
|
||||
import { JsonifiableZodSchemaObject } from "../traits/JsonifiableZodSchemaObject"
|
||||
import { StaticMembers } from "../util"
|
||||
|
||||
|
||||
export class ZodSchemaClassBuilder<
|
||||
Superclass extends AbstractClass<object>,
|
||||
const Traits extends readonly Trait<any, any, any, any>[],
|
||||
>
|
||||
extends TraitExpressionBuilder<Superclass, Traits> {
|
||||
declare ["constructor"]: typeof ZodSchemaClassBuilder
|
||||
|
||||
schema<
|
||||
Superclass extends AbstractClass<object, []> & { schema?: never, defaultValues?: never },
|
||||
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
Values extends object,
|
||||
DefaultValues extends Partial<Values>,
|
||||
>(
|
||||
this: ZodSchemaClassBuilder<Superclass, Traits>,
|
||||
|
||||
{ schema, defaultValues }: {
|
||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||
defaultValues: DefaultValues
|
||||
},
|
||||
) {
|
||||
class Schemas extends (this.expressionSuperclass as AbstractClass<object, []>) {
|
||||
static readonly schema = schema
|
||||
static readonly defaultValues = defaultValues
|
||||
|
||||
constructor(values: Values) {
|
||||
super()
|
||||
Object.assign(this, values)
|
||||
}
|
||||
}
|
||||
|
||||
return new this.constructor(
|
||||
Schemas as unknown as (
|
||||
AbstractClass<
|
||||
InstanceType<Superclass> & Values,
|
||||
ConstructorParameters<typeof Schemas>
|
||||
> &
|
||||
StaticMembers<Superclass> &
|
||||
StaticMembers<typeof Schemas>
|
||||
),
|
||||
|
||||
[
|
||||
...this.expressionTraits,
|
||||
InstantiableZodSchemaObject,
|
||||
ExtendableZodSchemaObject,
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
jsonifiable<
|
||||
Superclass extends ZodSchemaAbstractClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>
|
||||
& { jsonifySchema?: never, dejsonifySchema?: never },
|
||||
Instance extends Values,
|
||||
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
Values extends object,
|
||||
DefaultValues extends Partial<Values>,
|
||||
|
||||
JsonifySchemaT extends z.ZodRawShape,
|
||||
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
JsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
DejsonifySchemaT extends z.ZodRawShape,
|
||||
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
DejsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
JsonifiedValues extends JsonifiableObject,
|
||||
>(
|
||||
this: ZodSchemaClassBuilder<
|
||||
Superclass | ZodSchemaAbstractClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>,
|
||||
Traits
|
||||
>,
|
||||
|
||||
props: {
|
||||
jsonifySchema: (
|
||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||
) => z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>
|
||||
|
||||
dejsonifySchema: (
|
||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||
) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
|
||||
},
|
||||
) {
|
||||
const jsonifySchema = props.jsonifySchema(this.expressionSuperclass.schema)
|
||||
const dejsonifySchema = props.dejsonifySchema(this.expressionSuperclass.schema)
|
||||
|
||||
class JsonifiableSchemas extends (this.expressionSuperclass as AbstractClass<object>) {
|
||||
static readonly jsonifySchema = jsonifySchema
|
||||
readonly jsonifySchema = jsonifySchema
|
||||
static readonly dejsonifySchema = dejsonifySchema
|
||||
readonly dejsonifySchema = dejsonifySchema
|
||||
}
|
||||
|
||||
return new this.constructor(
|
||||
JsonifiableSchemas as unknown as (
|
||||
AbstractClass<
|
||||
InstanceType<Superclass> & JsonifiableSchemas,
|
||||
|
||||
// TODO: for some reason, ConstructorParameters<Superclass> does not work here. Maybe try to find a fix?
|
||||
ConstructorParameters<ZodSchemaAbstractClass<Instance, SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, DefaultValues>>
|
||||
> &
|
||||
StaticMembers<Superclass> &
|
||||
StaticMembers<typeof JsonifiableSchemas>
|
||||
),
|
||||
|
||||
[
|
||||
...this.expressionTraits,
|
||||
JsonifiableZodSchemaObject,
|
||||
DejsonifiableZodSchemaObject,
|
||||
],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export interface ZodSchemaClassBuilder<
|
||||
Superclass extends AbstractClass<object>,
|
||||
Traits extends readonly Trait<any, any, any, any>[],
|
||||
> {
|
||||
extends<
|
||||
Super extends AbstractClass<object>
|
||||
>(
|
||||
// \/ Ensures `extends` can only be called once at the beginning
|
||||
this: ZodSchemaClassBuilder<typeof TraitExpression.NullSuperclass, Traits>,
|
||||
superclass: Super,
|
||||
): ZodSchemaClassBuilder<Super, Traits>
|
||||
|
||||
expresses<
|
||||
const T extends readonly Trait<
|
||||
TraitExpression<
|
||||
typeof TraitExpression.NullSuperclass,
|
||||
readonly Trait<any, any, any, any>[]
|
||||
>,
|
||||
any,
|
||||
any,
|
||||
any
|
||||
>[]
|
||||
>(
|
||||
...traits: T
|
||||
): ZodSchemaClassBuilder<
|
||||
Superclass,
|
||||
TraitExpressionBuilder.ExpressesReturnTypeTraits<Traits, T>
|
||||
>
|
||||
|
||||
expressesFirst<
|
||||
const T extends readonly Trait<
|
||||
TraitExpression<
|
||||
typeof TraitExpression.NullSuperclass,
|
||||
readonly Trait<any, any, any, any>[]
|
||||
>,
|
||||
any,
|
||||
any,
|
||||
any
|
||||
>[]
|
||||
>(
|
||||
...traits: T
|
||||
): ZodSchemaClassBuilder<
|
||||
Superclass,
|
||||
TraitExpressionBuilder.ExpressesFirstReturnTypeTraits<Traits, T>
|
||||
>
|
||||
}
|
||||
|
||||
|
||||
export const zodSchemaClass = new ZodSchemaClassBuilder(TraitExpression.NullSuperclass, [])
|
||||
Reference in New Issue
Block a user