0.1.3 #4
42
src/JsonifiedZodSchemaClass.ts
Normal file
42
src/JsonifiedZodSchemaClass.ts
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import { TraitClass, expression } from "@thilawyn/traitify-ts"
|
||||||
|
import { Class, Jsonifiable } from "type-fest"
|
||||||
|
import { z } from "zod"
|
||||||
|
import { ZodSchemaObjectTrait } from "./lib"
|
||||||
|
import { JsonifiedZodSchemaObject } from "./traits/JsonifiedZodSchemaObject"
|
||||||
|
|
||||||
|
|
||||||
|
export function JsonifiedZodSchemaClass<
|
||||||
|
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 object & 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>,
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
return expression
|
||||||
|
.extends(
|
||||||
|
class JsonifiedZodSchemaObjectConstructor {
|
||||||
|
constructor(values: JsonifiedValues) {
|
||||||
|
Object.assign(this, values)
|
||||||
|
}
|
||||||
|
} as Class<JsonifiedValues, [values: JsonifiedValues]>
|
||||||
|
)
|
||||||
|
.expresses(JsonifiedZodSchemaObject(of, props))
|
||||||
|
}
|
||||||
24
src/tests.ts
24
src/tests.ts
@@ -1,8 +1,8 @@
|
|||||||
import { Implements, expression } from "@thilawyn/traitify-ts"
|
import { Implements } from "@thilawyn/traitify-ts"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
import { JsonifiedZodSchemaClass } from "./JsonifiedZodSchemaClass"
|
||||||
import { ZodSchemaClass } from "./ZodSchemaClass"
|
import { ZodSchemaClass } from "./ZodSchemaClass"
|
||||||
import { dejsonify, jsonify } from "./schema/jsonifiable"
|
import { dejsonify, jsonify } from "./schema/jsonifiable"
|
||||||
import { JsonifiedZodSchemaObject } from "./traits/JsonifiedZodSchemaObject"
|
|
||||||
import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject"
|
import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject"
|
||||||
|
|
||||||
|
|
||||||
@@ -28,21 +28,21 @@ const inst = User.create({ id: 1n, name: "User" })
|
|||||||
const instEffect = User.createEffect({ id: 1n, name: "User" })
|
const instEffect = User.createEffect({ id: 1n, name: "User" })
|
||||||
|
|
||||||
|
|
||||||
const jsonifiedUserExp = expression.expresses(
|
const jsonifiedUserExp = JsonifiedZodSchemaClass(User, {
|
||||||
JsonifiedZodSchemaObject(User, {
|
jsonifySchema: s => s.extend({
|
||||||
jsonifySchema: s => s.extend({
|
id: jsonify.bigint(s.shape.id)
|
||||||
id: jsonify.bigint(s.shape.id)
|
}),
|
||||||
}),
|
|
||||||
|
|
||||||
dejsonifySchema: s => s.extend({
|
dejsonifySchema: s => s.extend({
|
||||||
id: dejsonify.bigint(s.shape.id)
|
id: dejsonify.bigint(s.shape.id)
|
||||||
}),
|
}),
|
||||||
})
|
}).build()
|
||||||
).build()
|
|
||||||
|
|
||||||
@jsonifiedUserExp.staticImplements
|
@jsonifiedUserExp.staticImplements
|
||||||
class JsonifiedUser extends jsonifiedUserExp.extends implements Implements<typeof jsonifiedUserExp> {}
|
class JsonifiedUser extends jsonifiedUserExp.extends implements Implements<typeof jsonifiedUserExp> {}
|
||||||
|
|
||||||
|
const jsonifiedUser = JsonifiedUser.jsonify(inst)
|
||||||
|
|
||||||
|
|
||||||
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")
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ export const JsonifiedZodSchemaObject = <
|
|||||||
JsonifyCatchall extends z.ZodTypeAny,
|
JsonifyCatchall extends z.ZodTypeAny,
|
||||||
DejsonifyT extends z.ZodRawShape,
|
DejsonifyT extends z.ZodRawShape,
|
||||||
DejsonifyCatchall extends z.ZodTypeAny,
|
DejsonifyCatchall extends z.ZodTypeAny,
|
||||||
JsonifiedValues extends Jsonifiable,
|
JsonifiedValues extends object & Jsonifiable,
|
||||||
>(
|
>(
|
||||||
of: Of | TraitClass<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>,
|
of: Of | TraitClass<ZodSchemaObjectTrait<T, Catchall, Values, PartialValues>>,
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user