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