0.1.2 #3
@@ -7,7 +7,7 @@ import { Extend, StaticMembers } from "./util"
|
||||
|
||||
|
||||
export function ZodSchemaClassOf<
|
||||
Super extends AbstractClass<object, []>,
|
||||
Superclass extends AbstractClass<object, []>,
|
||||
|
||||
SchemaT extends z.ZodRawShape,
|
||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||
@@ -16,15 +16,14 @@ export function ZodSchemaClassOf<
|
||||
Values extends object,
|
||||
DefaultValues extends Partial<Values>,
|
||||
>(
|
||||
of: Super,
|
||||
of: Superclass,
|
||||
|
||||
{ schema, defaultValues }: {
|
||||
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, Values, Values>
|
||||
defaultValues: DefaultValues
|
||||
},
|
||||
) {
|
||||
const exp = expression
|
||||
.extends(class extends (of as AbstractClass<object, []>) {
|
||||
class Schemas extends (of as AbstractClass<object, []>) {
|
||||
static readonly schema = schema
|
||||
static readonly defaultValues = defaultValues
|
||||
|
||||
@@ -32,26 +31,28 @@ export function ZodSchemaClassOf<
|
||||
super()
|
||||
Object.assign(this, values)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return expression
|
||||
.extends(Schemas as unknown as (
|
||||
AbstractClass<
|
||||
InstanceType<typeof of> &
|
||||
Extend<[
|
||||
InstanceType<typeof Schemas>,
|
||||
Values,
|
||||
]>,
|
||||
|
||||
ConstructorParameters<typeof Schemas>
|
||||
> &
|
||||
Extend<[
|
||||
StaticMembers<typeof of>,
|
||||
StaticMembers<typeof Schemas>,
|
||||
]>
|
||||
))
|
||||
.expresses(
|
||||
InstantiableZodSchemaObject,
|
||||
ExtendableZodSchemaObject,
|
||||
)
|
||||
.build()
|
||||
|
||||
return exp.extends as AbstractClass<
|
||||
InstanceType<Super> &
|
||||
Extend<[
|
||||
InstanceType<typeof exp.extends>,
|
||||
Values,
|
||||
]>,
|
||||
|
||||
ConstructorParameters<typeof exp.extends>
|
||||
> &
|
||||
Extend<[
|
||||
StaticMembers<Super>,
|
||||
StaticMembers<typeof exp.extends>,
|
||||
]>
|
||||
}
|
||||
|
||||
|
||||
|
||||
35
src/tests.ts
35
src/tests.ts
@@ -1,28 +1,19 @@
|
||||
import { expression } from "@thilawyn/traitify-ts"
|
||||
import { AbstractClass } from "type-fest"
|
||||
import { Implements } from "@thilawyn/traitify-ts"
|
||||
import { z } from "zod"
|
||||
import { ZodSchemaClass } from "./ZodSchemaClass"
|
||||
import { ExtendableZodSchemaObject } from "./lib"
|
||||
import { InstantiableZodSchemaObject } from "./traits/InstantiableZodSchemaObject"
|
||||
|
||||
|
||||
class Test extends ZodSchemaClass({
|
||||
const TestExp = ZodSchemaClass({
|
||||
schema: z.object({
|
||||
id: z.bigint(),
|
||||
name: z.string(),
|
||||
}),
|
||||
|
||||
defaultValues: { id: -1n },
|
||||
}) {}
|
||||
}).build()
|
||||
|
||||
const Test2 = ZodSchemaClass({
|
||||
schema: z.object({
|
||||
id: z.bigint(),
|
||||
name: z.string(),
|
||||
}),
|
||||
|
||||
defaultValues: { id: -1n },
|
||||
})
|
||||
@TestExp.implementsStatic
|
||||
class Test extends TestExp.extends implements Implements<typeof TestExp> {}
|
||||
|
||||
Test.defaultValues
|
||||
const inst = Test.create({ id: 1n, name: "" })
|
||||
@@ -45,19 +36,3 @@ console.log(subInst)
|
||||
// class ChildTest extends Test {}
|
||||
|
||||
// ChildTest.instantiate({ name: "" })
|
||||
|
||||
|
||||
class Gneugneu {
|
||||
|
||||
}
|
||||
|
||||
export function getExpression<C extends AbstractClass<object>>(class_: C) {
|
||||
return expression
|
||||
.extends(class_)
|
||||
.expresses(
|
||||
InstantiableZodSchemaObject,
|
||||
ExtendableZodSchemaObject,
|
||||
)
|
||||
}
|
||||
|
||||
const exp = getExpression(Gneugneu).build()
|
||||
|
||||
Reference in New Issue
Block a user