JsonifiedZodSchemaClass
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Julien Valverdé
2024-03-18 11:34:50 +01:00
parent 1bb719c0b4
commit c16bfe24fd
3 changed files with 55 additions and 13 deletions

View 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))
}

View File

@@ -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,8 +28,7 @@ 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)
}), }),
@@ -37,12 +36,13 @@ const jsonifiedUserExp = expression.expresses(
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")

View File

@@ -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>>,