0.1.3 #4

Merged
Thilawyn merged 74 commits from next into master 2024-03-24 22:24:25 +01:00
Showing only changes of commit 1bb719c0b4 - Show all commits

View File

@@ -1,7 +1,8 @@
import { TraitClass, expression } from "@thilawyn/traitify-ts" import { ImplStatic, TraitClass, trait } from "@thilawyn/traitify-ts"
import { Jsonifiable } from "type-fest" import { Class, Jsonifiable } from "type-fest"
import { z } from "zod" import { z } from "zod"
import { ZodSchemaObject, ZodSchemaObjectTrait } from "./ZodSchemaObject" import { parseZodSchemaEffect } from "../util"
import { ZodSchemaObjectTrait } from "./ZodSchemaObject"
export const JsonifiedZodSchemaObject = < export const JsonifiedZodSchemaObject = <
@@ -28,13 +29,74 @@ export const JsonifiedZodSchemaObject = <
schema: typeof of.schema schema: typeof of.schema
) => z.ZodObject<DejsonifyT, "strip", DejsonifyCatchall, Values, JsonifiedValues>, ) => z.ZodObject<DejsonifyT, "strip", DejsonifyCatchall, Values, JsonifiedValues>,
}, },
) => expression ) => trait
.expresses(ZodSchemaObject(of.schemaWithDefaults)) .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)
static pipeSchemaIntoInstance<
Instance extends JsonifiedValues,
SchemaT extends z.ZodRawShape,
SchemaUnknownKeys extends z.UnknownKeysParam,
SchemaCatchall extends z.ZodTypeAny,
SchemaOutput extends JsonifiedValues,
SchemaInput,
>(
this: Class<Instance, [values: JsonifiedValues]>,
schema: z.ZodObject<SchemaT, SchemaUnknownKeys, SchemaCatchall, SchemaOutput, SchemaInput>,
) {
return schema.transform(values => new this(values))
}
static jsonify<
Instance extends JsonifiedValues
>(
this: (
Class<Instance, [values: JsonifiedValues]> &
ImplStatic<typeof JsonifiedZodSchemaObjectImpl>
),
values: Values,
params?: Partial<z.ParseParams>,
) {
return this
.pipeSchemaIntoInstance(this.jsonifySchema)
.parse(values, params)
}
static jsonifyPromise<
Instance extends JsonifiedValues
>(
this: (
Class<Instance, [values: JsonifiedValues]> &
ImplStatic<typeof JsonifiedZodSchemaObjectImpl>
),
values: Values,
params?: Partial<z.ParseParams>,
) {
return this
.pipeSchemaIntoInstance(this.jsonifySchema)
.parseAsync(values, params)
}
static jsonifyEffect<
Instance extends JsonifiedValues
>(
this: (
Class<Instance, [values: JsonifiedValues]> &
ImplStatic<typeof JsonifiedZodSchemaObjectImpl>
),
values: Values,
params?: Partial<z.ParseParams>,
) {
return parseZodSchemaEffect(
this.pipeSchemaIntoInstance(this.jsonifySchema),
values,
params,
)
}
})
.build() .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()