0.1.3 #4
@@ -1,12 +1,12 @@
|
|||||||
import { TraitClass, expression } from "@thilawyn/traitify-ts"
|
import { expression } from "@thilawyn/traitify-ts"
|
||||||
import { Class, Jsonifiable } from "type-fest"
|
import { Class, Jsonifiable } from "type-fest"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { ZodSchemaObjectTrait } from "./lib"
|
import { JsonifiedZodSchemaObject, OfClass, OfClassInstance } from "./traits/JsonifiedZodSchemaObject"
|
||||||
import { JsonifiedZodSchemaObject } from "./traits/JsonifiedZodSchemaObject"
|
|
||||||
|
|
||||||
|
|
||||||
export function JsonifiedZodSchemaClass<
|
export function JsonifiedZodSchemaClass<
|
||||||
Of extends TraitClass<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>,
|
Of extends OfClass<OfInstance, T, Catchall, Values, PartialValues>,
|
||||||
|
OfInstance extends OfClassInstance<T, Catchall, Values, PartialValues>,
|
||||||
T extends z.ZodRawShape,
|
T extends z.ZodRawShape,
|
||||||
Catchall extends z.ZodTypeAny,
|
Catchall extends z.ZodTypeAny,
|
||||||
Values extends object,
|
Values extends object,
|
||||||
@@ -18,7 +18,7 @@ export function JsonifiedZodSchemaClass<
|
|||||||
DejsonifyCatchall extends z.ZodTypeAny,
|
DejsonifyCatchall extends z.ZodTypeAny,
|
||||||
JsonifiedValues extends object & Jsonifiable,
|
JsonifiedValues extends object & Jsonifiable,
|
||||||
>(
|
>(
|
||||||
of: Of | TraitClass<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>,
|
of: Of | OfClass<OfInstance, T, Catchall, Values, PartialValues>,
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
jsonifySchema: (
|
jsonifySchema: (
|
||||||
@@ -38,5 +38,5 @@ export function JsonifiedZodSchemaClass<
|
|||||||
}
|
}
|
||||||
} as Class<JsonifiedValues, [values: JsonifiedValues]>
|
} as Class<JsonifiedValues, [values: JsonifiedValues]>
|
||||||
)
|
)
|
||||||
.expresses(JsonifiedZodSchemaObject(of, props))
|
.expresses(JsonifiedZodSchemaObject(of as Of, props))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ const jsonifiedUserExp = JsonifiedZodSchemaClass(User, {
|
|||||||
class JsonifiedUser extends jsonifiedUserExp.extends implements Implements<typeof jsonifiedUserExp> {}
|
class JsonifiedUser extends jsonifiedUserExp.extends implements Implements<typeof jsonifiedUserExp> {}
|
||||||
|
|
||||||
const jsonifiedUser = JsonifiedUser.jsonify(inst)
|
const jsonifiedUser = JsonifiedUser.jsonify(inst)
|
||||||
|
jsonifiedUser.dejsonify()
|
||||||
|
|
||||||
const adminUserExp = User.extend(s => s.extend({
|
const adminUserExp = User.extend(s => s.extend({
|
||||||
role: z.literal("Admin").default("Admin")
|
role: z.literal("Admin").default("Admin")
|
||||||
|
|||||||
@@ -1,12 +1,35 @@
|
|||||||
import { ImplStatic, TraitClass, trait } from "@thilawyn/traitify-ts"
|
import { ImplStatic, TraitInstance, TraitStaticMembers, trait } from "@thilawyn/traitify-ts"
|
||||||
import { Class, Jsonifiable } from "type-fest"
|
import { Class, Jsonifiable } from "type-fest"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { parseZodSchemaEffect } from "../util"
|
import { parseZodSchemaEffect } from "../util"
|
||||||
import { ZodSchemaObjectTrait } from "./ZodSchemaObject"
|
import { ZodSchemaObjectTrait } from "./ZodSchemaObject"
|
||||||
|
|
||||||
|
|
||||||
|
export type OfClass<
|
||||||
|
Instance extends OfClassInstance<T, Catchall, Values, PartialValues>,
|
||||||
|
|
||||||
|
T extends z.ZodRawShape,
|
||||||
|
Catchall extends z.ZodTypeAny,
|
||||||
|
Values extends object,
|
||||||
|
PartialValues extends Partial<Values>,
|
||||||
|
> = (
|
||||||
|
Class<Instance, [values: Values]> &
|
||||||
|
TraitStaticMembers<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>
|
||||||
|
)
|
||||||
|
|
||||||
|
export type OfClassInstance<
|
||||||
|
T extends z.ZodRawShape,
|
||||||
|
Catchall extends z.ZodTypeAny,
|
||||||
|
Values extends object,
|
||||||
|
PartialValues extends Partial<Values>,
|
||||||
|
> = (
|
||||||
|
Values & TraitInstance<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
export const JsonifiedZodSchemaObject = <
|
export const JsonifiedZodSchemaObject = <
|
||||||
Of extends TraitClass<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>,
|
Of extends OfClass<OfInstance, T, Catchall, Values, PartialValues>,
|
||||||
|
OfInstance extends OfClassInstance<T, Catchall, Values, PartialValues>,
|
||||||
T extends z.ZodRawShape,
|
T extends z.ZodRawShape,
|
||||||
Catchall extends z.ZodTypeAny,
|
Catchall extends z.ZodTypeAny,
|
||||||
Values extends object,
|
Values extends object,
|
||||||
@@ -18,7 +41,7 @@ export const JsonifiedZodSchemaObject = <
|
|||||||
DejsonifyCatchall extends z.ZodTypeAny,
|
DejsonifyCatchall extends z.ZodTypeAny,
|
||||||
JsonifiedValues extends object & Jsonifiable,
|
JsonifiedValues extends object & Jsonifiable,
|
||||||
>(
|
>(
|
||||||
of: Of | TraitClass<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>,
|
of: Of | OfClass<OfInstance, T, Catchall, Values, PartialValues>,
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
jsonifySchema: (
|
jsonifySchema: (
|
||||||
@@ -31,6 +54,8 @@ export const JsonifiedZodSchemaObject = <
|
|||||||
},
|
},
|
||||||
) => trait
|
) => trait
|
||||||
.implement(Super => class JsonifiedZodSchemaObjectImpl extends Super {
|
.implement(Super => class JsonifiedZodSchemaObjectImpl extends Super {
|
||||||
|
declare ["constructor"]: typeof JsonifiedZodSchemaObjectImpl
|
||||||
|
|
||||||
static readonly of = of as Of
|
static readonly of = of as Of
|
||||||
static readonly jsonifySchema = props.jsonifySchema(of.schema)
|
static readonly jsonifySchema = props.jsonifySchema(of.schema)
|
||||||
static readonly dejsonifySchema = props.dejsonifySchema(of.schema)
|
static readonly dejsonifySchema = props.dejsonifySchema(of.schema)
|
||||||
@@ -98,5 +123,22 @@ export const JsonifiedZodSchemaObject = <
|
|||||||
params,
|
params,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
dejsonify(params?: Partial<z.ParseParams>) {
|
||||||
|
return this.constructor.of.pipeSchemaIntoInstance(
|
||||||
|
this.constructor.dejsonifySchema
|
||||||
|
).parse(this, params)
|
||||||
|
}
|
||||||
|
|
||||||
|
dejsonifyPromise(params?: Partial<z.ParseParams>) {
|
||||||
|
return this.constructor.of.pipeSchemaIntoInstance(
|
||||||
|
this.constructor.dejsonifySchema
|
||||||
|
).parseAsync(this, params)
|
||||||
|
}
|
||||||
|
|
||||||
|
dejsonifyEffect(params?: Partial<z.ParseParams>) {
|
||||||
|
return parseZodSchemaEffect(this.constructor.dejsonifySchema, this, params)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.build()
|
.build()
|
||||||
|
|||||||
Reference in New Issue
Block a user