0.1.3 #4
@@ -1,15 +1,9 @@
|
|||||||
import { expression } from "@thilawyn/traitify-ts"
|
import { expression } from "@thilawyn/traitify-ts"
|
||||||
|
import { Class } from "type-fest"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { ZodSchemaObject } from "./lib"
|
import { ZodSchemaObject } from "./lib"
|
||||||
|
|
||||||
|
|
||||||
class ZodSchemaObjectConstructor<Values> {
|
|
||||||
constructor(values: Values) {
|
|
||||||
Object.assign(this, values)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export function ZodSchemaClass<
|
export function ZodSchemaClass<
|
||||||
T extends z.ZodRawShape,
|
T extends z.ZodRawShape,
|
||||||
Catchall extends z.ZodTypeAny,
|
Catchall extends z.ZodTypeAny,
|
||||||
@@ -19,6 +13,12 @@ export function ZodSchemaClass<
|
|||||||
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>
|
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>
|
||||||
) {
|
) {
|
||||||
return expression
|
return expression
|
||||||
.extends(ZodSchemaObjectConstructor<Values>)
|
.extends(
|
||||||
|
class ZodSchemaObjectConstructor {
|
||||||
|
constructor(values: Values) {
|
||||||
|
Object.assign(this, values)
|
||||||
|
}
|
||||||
|
} as Class<Values, [values: Values]>
|
||||||
|
)
|
||||||
.expresses(ZodSchemaObject(schemaWithDefaults))
|
.expresses(ZodSchemaObject(schemaWithDefaults))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ export const ExtendableZodSchemaObject = <
|
|||||||
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>,
|
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>,
|
||||||
) => expression
|
) => expression
|
||||||
.expresses(ZodSchemaObject(schemaWithDefaults))
|
.expresses(ZodSchemaObject(schemaWithDefaults))
|
||||||
.buildAnyway()
|
.build()
|
||||||
.subtrait()
|
.subtrait()
|
||||||
.implement(Super => class ExtendableZodSchemaObject extends Super {
|
.implement(Super => class ExtendableZodSchemaObjectImpl extends Super {
|
||||||
})
|
})
|
||||||
.build()
|
.build()
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { ImplStatic, trait } from "@thilawyn/traitify-ts"
|
import { ImplStatic, trait } from "@thilawyn/traitify-ts"
|
||||||
import { Class, HasRequiredKeys } from "type-fest"
|
import { Class, HasRequiredKeys } from "type-fest"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { StaticMembers, parseZodSchemaEffect, stripZodObjectDefaults } from "../util"
|
import { parseZodSchemaEffect, stripZodObjectDefaults } from "../util"
|
||||||
|
|
||||||
|
|
||||||
type CreateArgs<Input extends object> = (
|
type CreateArgs<Input extends object> = (
|
||||||
@@ -19,8 +19,7 @@ export const ZodSchemaObject = <
|
|||||||
>(
|
>(
|
||||||
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>,
|
schemaWithDefaults: z.ZodObject<T, "strip", Catchall, Values, PartialValues>,
|
||||||
) => trait
|
) => trait
|
||||||
.implement(Super => {
|
.implement(Super => class ZodSchemaObjectImpl extends Super {
|
||||||
class ZodSchemaObject extends Super {
|
|
||||||
static readonly schema = stripZodObjectDefaults(schemaWithDefaults)
|
static readonly schema = stripZodObjectDefaults(schemaWithDefaults)
|
||||||
static readonly schemaWithDefaults = schemaWithDefaults
|
static readonly schemaWithDefaults = schemaWithDefaults
|
||||||
|
|
||||||
@@ -45,7 +44,7 @@ export const ZodSchemaObject = <
|
|||||||
>(
|
>(
|
||||||
this: (
|
this: (
|
||||||
Class<Instance, [values: Values]> &
|
Class<Instance, [values: Values]> &
|
||||||
ImplStatic<typeof ZodSchemaObject>
|
ImplStatic<typeof ZodSchemaObjectImpl>
|
||||||
),
|
),
|
||||||
...[values, params]: CreateArgs<PartialValues>
|
...[values, params]: CreateArgs<PartialValues>
|
||||||
) {
|
) {
|
||||||
@@ -59,7 +58,7 @@ export const ZodSchemaObject = <
|
|||||||
>(
|
>(
|
||||||
this: (
|
this: (
|
||||||
Class<Instance, [values: Values]> &
|
Class<Instance, [values: Values]> &
|
||||||
ImplStatic<typeof ZodSchemaObject>
|
ImplStatic<typeof ZodSchemaObjectImpl>
|
||||||
),
|
),
|
||||||
...[values, params]: CreateArgs<PartialValues>
|
...[values, params]: CreateArgs<PartialValues>
|
||||||
) {
|
) {
|
||||||
@@ -73,7 +72,7 @@ export const ZodSchemaObject = <
|
|||||||
>(
|
>(
|
||||||
this: (
|
this: (
|
||||||
Class<Instance, [values: Values]> &
|
Class<Instance, [values: Values]> &
|
||||||
ImplStatic<typeof ZodSchemaObject>
|
ImplStatic<typeof ZodSchemaObjectImpl>
|
||||||
),
|
),
|
||||||
...[values, params]: CreateArgs<PartialValues>
|
...[values, params]: CreateArgs<PartialValues>
|
||||||
) {
|
) {
|
||||||
@@ -103,9 +102,6 @@ export const ZodSchemaObject = <
|
|||||||
// .extends(this)
|
// .extends(this)
|
||||||
// .expresses(ZodSchemaObject(schemaWithDefaults(this.schemaWithDefaults)))
|
// .expresses(ZodSchemaObject(schemaWithDefaults(this.schemaWithDefaults)))
|
||||||
// }
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
return ZodSchemaObject as Class<ZodSchemaObject & Values> & StaticMembers<typeof ZodSchemaObject>
|
|
||||||
})
|
})
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user