ZodSchemaClassOf refactoring
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -7,7 +7,7 @@ import { Extend, StaticMembers } from "./util"
|
|||||||
|
|
||||||
|
|
||||||
export function ZodSchemaClassOf<
|
export function ZodSchemaClassOf<
|
||||||
Super extends AbstractClass<object, []>,
|
Superclass extends AbstractClass<object, []>,
|
||||||
|
|
||||||
SchemaT extends z.ZodRawShape,
|
SchemaT extends z.ZodRawShape,
|
||||||
SchemaUnknownKeys extends z.UnknownKeysParam,
|
SchemaUnknownKeys extends z.UnknownKeysParam,
|
||||||
@@ -16,42 +16,43 @@ export function ZodSchemaClassOf<
|
|||||||
Values extends object,
|
Values extends object,
|
||||||
DefaultValues extends Partial<Values>,
|
DefaultValues extends Partial<Values>,
|
||||||
>(
|
>(
|
||||||
of: Super,
|
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: DefaultValues
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const exp = expression
|
class Schemas extends (of as AbstractClass<object, []>) {
|
||||||
.extends(class extends (of as AbstractClass<object, []>) {
|
static readonly schema = schema
|
||||||
static readonly schema = schema
|
static readonly defaultValues = defaultValues
|
||||||
static readonly defaultValues = defaultValues
|
|
||||||
|
|
||||||
constructor(values: Values) {
|
constructor(values: Values) {
|
||||||
super()
|
super()
|
||||||
Object.assign(this, values)
|
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(
|
.expresses(
|
||||||
InstantiableZodSchemaObject,
|
InstantiableZodSchemaObject,
|
||||||
ExtendableZodSchemaObject,
|
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 { Implements } from "@thilawyn/traitify-ts"
|
||||||
import { AbstractClass } from "type-fest"
|
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { ZodSchemaClass } from "./ZodSchemaClass"
|
import { ZodSchemaClass } from "./ZodSchemaClass"
|
||||||
import { ExtendableZodSchemaObject } from "./lib"
|
|
||||||
import { InstantiableZodSchemaObject } from "./traits/InstantiableZodSchemaObject"
|
|
||||||
|
|
||||||
|
|
||||||
class Test extends ZodSchemaClass({
|
const TestExp = ZodSchemaClass({
|
||||||
schema: z.object({
|
schema: z.object({
|
||||||
id: z.bigint(),
|
id: z.bigint(),
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
defaultValues: { id: -1n },
|
defaultValues: { id: -1n },
|
||||||
}) {}
|
}).build()
|
||||||
|
|
||||||
const Test2 = ZodSchemaClass({
|
@TestExp.implementsStatic
|
||||||
schema: z.object({
|
class Test extends TestExp.extends implements Implements<typeof TestExp> {}
|
||||||
id: z.bigint(),
|
|
||||||
name: z.string(),
|
|
||||||
}),
|
|
||||||
|
|
||||||
defaultValues: { id: -1n },
|
|
||||||
})
|
|
||||||
|
|
||||||
Test.defaultValues
|
Test.defaultValues
|
||||||
const inst = Test.create({ id: 1n, name: "" })
|
const inst = Test.create({ id: 1n, name: "" })
|
||||||
@@ -45,19 +36,3 @@ console.log(subInst)
|
|||||||
// class ChildTest extends Test {}
|
// class ChildTest extends Test {}
|
||||||
|
|
||||||
// ChildTest.instantiate({ name: "" })
|
// 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