ZodSchemaClassExtender.jsonifiable work
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julien Valverdé
2024-02-25 23:57:46 +01:00
parent f79143d821
commit 0b0af7b128

View File

@@ -1,10 +1,10 @@
import { expression } from "@thilawyn/traitify-ts"
import { AbstractClass, Simplify } from "type-fest"
import { JsonifiableObject } from "type-fest/source/jsonifiable"
import { z } from "zod"
import { JsonifiableZodSchemaAbstractClass } from "../shapes/JsonifiableZodSchemaClass"
import { ZodSchemaAbstractClass } from "../shapes/ZodSchemaClass"
import { Extend, Override, StaticMembers } from "../util"
import { JsonifiableZodSchemaAbstractClass, JsonifiableZodSchemaClass } from "../shapes/JsonifiableZodSchemaClass"
import { JsonifiableObject } from "type-fest/source/jsonifiable"
export class ZodSchemaClassExtender<Superclass extends AbstractClass<object>> {
@@ -66,22 +66,44 @@ export class ZodSchemaClassExtender<Superclass extends AbstractClass<object>> {
}
jsonifiable<
Super extends JsonifiableZodSchemaAbstractClass<
Instance,
Super extends (
ZodSchemaAbstractClass<SuperInstance, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues> &
JsonifiableZodSchemaAbstractClass<
SuperInstance,
JsonifySchemaT,
JsonifySchemaUnknownKeys,
JsonifySchemaCatchall,
SuperJsonifySchemaT,
SuperJsonifySchemaUnknownKeys,
SuperJsonifySchemaCatchall,
DejsonifySchemaT,
DejsonifySchemaUnknownKeys,
DejsonifySchemaCatchall,
SuperDejsonifySchemaT,
SuperDejsonifySchemaUnknownKeys,
SuperDejsonifySchemaCatchall,
JsonifiedValues,
Values
>,
Instance extends Values,
SuperJsonifiedValues,
SuperValues
>
),
SuperInstance extends SuperValues,
SuperSchemaT extends z.ZodRawShape,
SuperSchemaUnknownKeys extends z.UnknownKeysParam,
SuperSchemaCatchall extends z.ZodTypeAny,
SuperValues extends object,
SuperDefaultValues extends Partial<SuperValues>,
SuperJsonifySchemaT extends z.ZodRawShape,
SuperJsonifySchemaUnknownKeys extends z.UnknownKeysParam,
SuperJsonifySchemaCatchall extends z.ZodTypeAny,
SuperDejsonifySchemaT extends z.ZodRawShape,
SuperDejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
SuperDejsonifySchemaCatchall extends z.ZodTypeAny,
SuperJsonifiedValues extends JsonifiableObject,
/* New jsonifiable schemas */
JsonifySchemaT extends z.ZodRawShape,
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
JsonifySchemaCatchall extends z.ZodTypeAny,
@@ -90,56 +112,65 @@ export class ZodSchemaClassExtender<Superclass extends AbstractClass<object>> {
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
DejsonifySchemaCatchall extends z.ZodTypeAny,
JsonifiedValues extends JsonifiableObject,
Values extends object,
JsonifiedValues extends SuperJsonifiedValues,
Values extends SuperValues,
>(
this: ZodSchemaClassExtender<
Super | JsonifiableZodSchemaAbstractClass<
Instance,
Super | (
ZodSchemaAbstractClass<SuperInstance, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues> &
JsonifiableZodSchemaAbstractClass<
SuperInstance,
JsonifySchemaT,
JsonifySchemaUnknownKeys,
JsonifySchemaCatchall,
SuperJsonifySchemaT,
SuperJsonifySchemaUnknownKeys,
SuperJsonifySchemaCatchall,
DejsonifySchemaT,
DejsonifySchemaUnknownKeys,
DejsonifySchemaCatchall,
SuperDejsonifySchemaT,
SuperDejsonifySchemaUnknownKeys,
SuperDejsonifySchemaCatchall,
JsonifiedValues,
Values
SuperJsonifiedValues,
SuperValues
>
)
>,
props: {
jsonifySchema: (
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
schema: Super["schema"],
jsonifySchema: Super["jsonifySchema"],
) => z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>
dejsonifySchema: (
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
schema: Super["schema"],
dejsonifySchema: Super["dejsonifySchema"],
) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
},
) {
const schema = props.schema(this.superclass.schema)
const defaultValues = props.defaultValues(this.superclass.defaultValues)
const jsonifySchema = props.jsonifySchema(this.superclass.schema, this.superclass.jsonifySchema)
const dejsonifySchema = props.dejsonifySchema(this.superclass.schema, this.superclass.dejsonifySchema)
class Schemas extends (this.superclass as AbstractClass<object, any[]>) {
static readonly schema = schema
static readonly defaultValues = defaultValues
class JsonifiableSchemas extends (this.superclass as AbstractClass<object>) {
static readonly jsonifySchema = jsonifySchema
readonly jsonifySchema = jsonifySchema
static readonly dejsonifySchema = dejsonifySchema
readonly dejsonifySchema = dejsonifySchema
}
return new this.constructor(
Schemas as unknown as AbstractClass<
JsonifiableSchemas as unknown as AbstractClass<
Simplify<
Extend<[SuperInstance, Values]>
Override<[SuperInstance, JsonifiableSchemas]>
>,
[values: Values]
ConstructorParameters<
ZodSchemaAbstractClass<SuperInstance, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>
>
> &
Simplify<
Override<[
StaticMembers<Super>,
StaticMembers<typeof Schemas>,
StaticMembers<typeof JsonifiableSchemas>,
]>
>
)