This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
import { expression } from "@thilawyn/traitify-ts"
|
import { expression } from "@thilawyn/traitify-ts"
|
||||||
import { NoInfer } from "effect/Types"
|
import { NoInfer } from "effect/Types"
|
||||||
import { AbstractClass, Opaque } from "type-fest"
|
import { AbstractClass, Class } from "type-fest"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { DefinedDefaultValuesTag } from "."
|
|
||||||
import { InstantiableZodSchemaObject } from "./traits/InstantiableZodSchemaObject"
|
import { InstantiableZodSchemaObject } from "./traits/InstantiableZodSchemaObject"
|
||||||
|
import { StaticMembers } from "./util"
|
||||||
|
|
||||||
|
|
||||||
export function ZodSchemaClassOf<
|
export function ZodSchemaClassOf<
|
||||||
@@ -14,21 +14,33 @@ export function ZodSchemaClassOf<
|
|||||||
SchemaCatchall extends z.ZodTypeAny,
|
SchemaCatchall extends z.ZodTypeAny,
|
||||||
|
|
||||||
Values extends {},
|
Values extends {},
|
||||||
DefaultValues extends NoInfer<Partial<Values>>,
|
DefaultValues extends Partial<Values>,
|
||||||
>(
|
>(
|
||||||
of: Superclass,
|
of: Superclass,
|
||||||
|
|
||||||
{ schema, defaultValues }: {
|
{ schema, defaultValues }: {
|
||||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||||
defaultValues: DefaultValues
|
defaultValues: NoInfer<DefaultValues>
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const exp = expression
|
const exp = expression
|
||||||
.extends(of)
|
.extends(class extends (of as AbstractClass<{}, []>) {
|
||||||
|
static readonly schema = schema
|
||||||
|
static readonly defaultValues = defaultValues
|
||||||
|
|
||||||
|
constructor(values: Values) {
|
||||||
|
super()
|
||||||
|
Object.assign(this, values)
|
||||||
|
}
|
||||||
|
})
|
||||||
.expresses(InstantiableZodSchemaObject)
|
.expresses(InstantiableZodSchemaObject)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
return exp
|
return exp.extends as Class<
|
||||||
|
InstanceType<typeof exp.extends> & Values,
|
||||||
|
ConstructorParameters<typeof exp.extends>
|
||||||
|
> &
|
||||||
|
StaticMembers<typeof exp.extends>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -44,7 +56,7 @@ export function ZodSchemaClass<
|
|||||||
>(
|
>(
|
||||||
props: {
|
props: {
|
||||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||||
defaultValues: Opaque<DefaultValues, DefinedDefaultValuesTag>
|
defaultValues: NoInfer<DefaultValues>
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
return ZodSchemaClassOf(DefaultRoot, props)
|
return ZodSchemaClassOf(DefaultRoot, props)
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ class Test extends ZodSchemaClass({
|
|||||||
name: z.string(),
|
name: z.string(),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
defaultValues: defineDefaultValues({ id: -1n }),
|
defaultValues: { id: -1n },
|
||||||
}) {}
|
}) {}
|
||||||
|
|
||||||
Test.defaultValues
|
Test.defaultValues
|
||||||
|
|||||||
Reference in New Issue
Block a user