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