This commit is contained in:
@@ -7,15 +7,20 @@ import { ZodSchemaAbstractClass } from "../shapes/ZodSchemaClass"
|
||||
import { Extend, Override, StaticMembers } from "../util"
|
||||
|
||||
|
||||
export class ZodSchemaClassExtender<Superclass extends AbstractClass<object>> {
|
||||
export class ZodSchemaClassExtender<
|
||||
Superclass extends AbstractClass<object>,
|
||||
Subclass extends AbstractClass<object>,
|
||||
> {
|
||||
declare ["constructor"]: typeof ZodSchemaClassExtender
|
||||
|
||||
constructor(readonly superclass: Superclass) {}
|
||||
constructor(
|
||||
readonly superclass: Superclass,
|
||||
readonly subclass: Subclass,
|
||||
) {}
|
||||
|
||||
|
||||
schema<
|
||||
Super extends ZodSchemaAbstractClass<SuperInstance, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>,
|
||||
SuperInstance extends SuperValues,
|
||||
Super extends ZodSchemaAbstractClass<any, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>,
|
||||
|
||||
SuperSchemaT extends z.ZodRawShape,
|
||||
SuperSchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
@@ -32,7 +37,8 @@ export class ZodSchemaClassExtender<Superclass extends AbstractClass<object>> {
|
||||
DefaultValues extends Partial<Values>,
|
||||
>(
|
||||
this: ZodSchemaClassExtender<
|
||||
Super | ZodSchemaAbstractClass<SuperInstance, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>
|
||||
Super | ZodSchemaAbstractClass<any, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>,
|
||||
any
|
||||
>,
|
||||
|
||||
props: {
|
||||
@@ -49,9 +55,11 @@ export class ZodSchemaClassExtender<Superclass extends AbstractClass<object>> {
|
||||
}
|
||||
|
||||
return new this.constructor(
|
||||
this.superclass,
|
||||
|
||||
Schemas as unknown as AbstractClass<
|
||||
Simplify<
|
||||
Extend<[SuperInstance, Values]>
|
||||
Extend<[InstanceType<Super>, Values]>
|
||||
>,
|
||||
|
||||
[values: Values]
|
||||
@@ -66,10 +74,9 @@ export class ZodSchemaClassExtender<Superclass extends AbstractClass<object>> {
|
||||
}
|
||||
|
||||
jsonifiable<
|
||||
Super extends (
|
||||
ZodSchemaAbstractClass<SuperInstance, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues> &
|
||||
JsonifiableZodSchemaAbstractClass<
|
||||
SuperInstance,
|
||||
/** Superclass jsonifiable schemas */
|
||||
Super extends JsonifiableZodSchemaAbstractClass<
|
||||
any,
|
||||
|
||||
SuperJsonifySchemaT,
|
||||
SuperJsonifySchemaUnknownKeys,
|
||||
@@ -81,17 +88,7 @@ export class ZodSchemaClassExtender<Superclass extends AbstractClass<object>> {
|
||||
|
||||
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,
|
||||
@@ -102,6 +99,17 @@ export class ZodSchemaClassExtender<Superclass extends AbstractClass<object>> {
|
||||
SuperDejsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
SuperJsonifiedValues extends JsonifiableObject,
|
||||
SuperValues extends object,
|
||||
|
||||
/** New schemas */
|
||||
Self extends ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>,
|
||||
|
||||
SelfSchemaT extends z.ZodRawShape,
|
||||
SelfSchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
SelfSchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
SelfValues extends object,
|
||||
SelfDefaultValues extends Partial<SelfValues>,
|
||||
|
||||
/* New jsonifiable schemas */
|
||||
JsonifySchemaT extends z.ZodRawShape,
|
||||
@@ -113,13 +121,11 @@ export class ZodSchemaClassExtender<Superclass extends AbstractClass<object>> {
|
||||
DejsonifySchemaCatchall extends z.ZodTypeAny,
|
||||
|
||||
JsonifiedValues extends SuperJsonifiedValues,
|
||||
Values extends SuperValues,
|
||||
Values extends SelfValues,
|
||||
>(
|
||||
this: ZodSchemaClassExtender<
|
||||
Super | (
|
||||
ZodSchemaAbstractClass<SuperInstance, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues> &
|
||||
JsonifiableZodSchemaAbstractClass<
|
||||
SuperInstance,
|
||||
Super | JsonifiableZodSchemaAbstractClass<
|
||||
any,
|
||||
|
||||
SuperJsonifySchemaT,
|
||||
SuperJsonifySchemaUnknownKeys,
|
||||
@@ -131,26 +137,27 @@ export class ZodSchemaClassExtender<Superclass extends AbstractClass<object>> {
|
||||
|
||||
SuperJsonifiedValues,
|
||||
SuperValues
|
||||
>
|
||||
)
|
||||
>,
|
||||
|
||||
Self | ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>
|
||||
>,
|
||||
|
||||
props: {
|
||||
jsonifySchema: (
|
||||
schema: Super["schema"],
|
||||
schema: Self["schema"],
|
||||
jsonifySchema: Super["jsonifySchema"],
|
||||
) => z.ZodObject<JsonifySchemaT, JsonifySchemaUnknownKeys, JsonifySchemaCatchall, JsonifiedValues, Values>
|
||||
|
||||
dejsonifySchema: (
|
||||
schema: Super["schema"],
|
||||
schema: Self["schema"],
|
||||
dejsonifySchema: Super["dejsonifySchema"],
|
||||
) => z.ZodObject<DejsonifySchemaT, DejsonifySchemaUnknownKeys, DejsonifySchemaCatchall, Values, JsonifiedValues>
|
||||
},
|
||||
) {
|
||||
const jsonifySchema = props.jsonifySchema(this.superclass.schema, this.superclass.jsonifySchema)
|
||||
const dejsonifySchema = props.dejsonifySchema(this.superclass.schema, this.superclass.dejsonifySchema)
|
||||
const jsonifySchema = props.jsonifySchema(this.subclass.schema, this.superclass.jsonifySchema)
|
||||
const dejsonifySchema = props.dejsonifySchema(this.subclass.schema, this.superclass.dejsonifySchema)
|
||||
|
||||
class JsonifiableSchemas extends (this.superclass as AbstractClass<object>) {
|
||||
class JsonifiableSchemas extends (this.subclass as AbstractClass<object>) {
|
||||
static readonly jsonifySchema = jsonifySchema
|
||||
readonly jsonifySchema = jsonifySchema
|
||||
static readonly dejsonifySchema = dejsonifySchema
|
||||
@@ -158,30 +165,32 @@ export class ZodSchemaClassExtender<Superclass extends AbstractClass<object>> {
|
||||
}
|
||||
|
||||
return new this.constructor(
|
||||
this.superclass,
|
||||
|
||||
JsonifiableSchemas as unknown as AbstractClass<
|
||||
Simplify<
|
||||
Override<[SuperInstance, JsonifiableSchemas]>
|
||||
Override<[InstanceType<Self>, JsonifiableSchemas]>
|
||||
>,
|
||||
|
||||
ConstructorParameters<
|
||||
ZodSchemaAbstractClass<SuperInstance, SuperSchemaT, SuperSchemaUnknownKeys, SuperSchemaCatchall, SuperValues, SuperDefaultValues>
|
||||
ZodSchemaAbstractClass<any, SelfSchemaT, SelfSchemaUnknownKeys, SelfSchemaCatchall, SelfValues, SelfDefaultValues>
|
||||
>
|
||||
> &
|
||||
Simplify<
|
||||
Override<[
|
||||
StaticMembers<Super>,
|
||||
StaticMembers<Self>,
|
||||
StaticMembers<typeof JsonifiableSchemas>,
|
||||
]>
|
||||
>
|
||||
>,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
toClass() {
|
||||
return this.superclass
|
||||
return this.subclass
|
||||
}
|
||||
|
||||
toExpressionBuilder() {
|
||||
return expression.extends(this.superclass)
|
||||
return expression.extends(this.subclass)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,9 @@ const AdminUserProto = User.extend()
|
||||
}),
|
||||
defaultValues: v => ({ ...v, name: "Admin" as const }),
|
||||
})
|
||||
.jsonifiable({
|
||||
|
||||
})
|
||||
|
||||
|
||||
class AdminUser extends AdminUserProto.toClass() {}
|
||||
|
||||
@@ -10,7 +10,7 @@ export const ExtendableZodSchemaObject = trait
|
||||
>(
|
||||
this: Self
|
||||
) {
|
||||
return new ZodSchemaClassExtender(this)
|
||||
return new ZodSchemaClassExtender(this, this)
|
||||
}
|
||||
})
|
||||
.build()
|
||||
|
||||
Reference in New Issue
Block a user