0.1.3 #4

Merged
Thilawyn merged 74 commits from next into master 2024-03-24 22:24:25 +01:00
2 changed files with 59 additions and 1 deletions
Showing only changes of commit c672e1f2bb - Show all commits

View File

@@ -1,6 +1,8 @@
import { Implements } from "@thilawyn/traitify-ts" import { Implements, expression } from "@thilawyn/traitify-ts"
import { z } from "zod" import { z } from "zod"
import { ZodSchemaClass } from "./ZodSchemaClass" import { ZodSchemaClass } from "./ZodSchemaClass"
import { dejsonify, jsonify } from "./schema/jsonifiable"
import { JsonifiedZodSchemaObject } from "./traits/JsonifiedZodSchemaObject"
import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject" import { MobXObservableZodSchemaObject } from "./traits/MobXObservableZodSchemaObject"
@@ -27,6 +29,22 @@ 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(
JsonifiedZodSchemaObject(User, {
jsonifySchema: s => s.extend({
id: jsonify.bigint(s.shape.id)
}),
dejsonifySchema: s => s.extend({
id: dejsonify.bigint(s.shape.id)
}),
})
).build()
@jsonifiedUserExp.staticImplements
class JsonifiedUser extends jsonifiedUserExp.extends implements Implements<typeof jsonifiedUserExp> {}
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")
})).build() })).build()

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