This commit is contained in:
40
src/traits/JsonifiedZodSchemaObject.ts
Normal file
40
src/traits/JsonifiedZodSchemaObject.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { TraitClass, expression } from "@thilawyn/traitify-ts"
|
||||
import { Jsonifiable } from "type-fest"
|
||||
import { z } from "zod"
|
||||
import { ZodSchemaObject, ZodSchemaObjectTrait } from "./ZodSchemaObject"
|
||||
|
||||
|
||||
export const JsonifiedZodSchemaObject = <
|
||||
Of extends TraitClass<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>,
|
||||
T extends z.ZodRawShape,
|
||||
Catchall extends z.ZodTypeAny,
|
||||
Values extends object,
|
||||
PartialValues extends Partial<Values>,
|
||||
|
||||
JsonifyT extends z.ZodRawShape,
|
||||
JsonifyCatchall extends z.ZodTypeAny,
|
||||
DejsonifyT extends z.ZodRawShape,
|
||||
DejsonifyCatchall extends z.ZodTypeAny,
|
||||
JsonifiedValues extends Jsonifiable,
|
||||
>(
|
||||
of: Of | TraitClass<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>,
|
||||
|
||||
props: {
|
||||
jsonifySchema: (
|
||||
schema: typeof of.schema
|
||||
) => z.ZodObject<JsonifyT, "strip", JsonifyCatchall, JsonifiedValues, Values>,
|
||||
|
||||
dejsonifySchema: (
|
||||
schema: typeof of.schema
|
||||
) => z.ZodObject<DejsonifyT, "strip", DejsonifyCatchall, Values, JsonifiedValues>,
|
||||
},
|
||||
) => expression
|
||||
.expresses(ZodSchemaObject(of.schemaWithDefaults))
|
||||
.build()
|
||||
.subtrait()
|
||||
.implement(Super => class JsonifiedZodSchemaObjectImpl extends Super {
|
||||
static readonly of = of as Of
|
||||
static readonly jsonifySchema = props.jsonifySchema(of.schema)
|
||||
static readonly dejsonifySchema = props.dejsonifySchema(of.schema)
|
||||
})
|
||||
.build()
|
||||
Reference in New Issue
Block a user