123 lines
4.0 KiB
TypeScript
123 lines
4.0 KiB
TypeScript
import { trait } from "@thilawyn/traitify-ts"
|
|
import { Effect } from "effect"
|
|
import { JsonifiableObject } from "type-fest/source/jsonifiable"
|
|
import { z } from "zod"
|
|
import { JsonifiableZodSchemaClass } from "../shapes/JsonifiableZodSchemaClass"
|
|
import { parseZodTypeEffect } from "../util"
|
|
|
|
|
|
export const DejsonifiableZodSchemaObject = trait
|
|
.implement(Super => class DejsonifiableZodSchemaObject extends Super {
|
|
static dejsonify<
|
|
Instance extends Values,
|
|
|
|
JsonifySchemaT extends z.ZodRawShape,
|
|
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
|
JsonifySchemaCatchall extends z.ZodTypeAny,
|
|
|
|
DejsonifySchemaT extends z.ZodRawShape,
|
|
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
|
DejsonifySchemaCatchall extends z.ZodTypeAny,
|
|
|
|
JsonifiedValues extends JsonifiableObject,
|
|
Values extends object,
|
|
>(
|
|
this: JsonifiableZodSchemaClass<
|
|
Instance,
|
|
|
|
JsonifySchemaT,
|
|
JsonifySchemaUnknownKeys,
|
|
JsonifySchemaCatchall,
|
|
|
|
DejsonifySchemaT,
|
|
DejsonifySchemaUnknownKeys,
|
|
DejsonifySchemaCatchall,
|
|
|
|
JsonifiedValues,
|
|
Values
|
|
>,
|
|
|
|
values: JsonifiedValues,
|
|
params?: Partial<z.ParseParams>,
|
|
) {
|
|
return new this(
|
|
this.dejsonifySchema.parse(values, params)
|
|
)
|
|
}
|
|
|
|
static async dejsonifyPromise<
|
|
Instance extends Values,
|
|
|
|
JsonifySchemaT extends z.ZodRawShape,
|
|
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
|
JsonifySchemaCatchall extends z.ZodTypeAny,
|
|
|
|
DejsonifySchemaT extends z.ZodRawShape,
|
|
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
|
DejsonifySchemaCatchall extends z.ZodTypeAny,
|
|
|
|
JsonifiedValues extends JsonifiableObject,
|
|
Values extends object,
|
|
>(
|
|
this: JsonifiableZodSchemaClass<
|
|
Instance,
|
|
|
|
JsonifySchemaT,
|
|
JsonifySchemaUnknownKeys,
|
|
JsonifySchemaCatchall,
|
|
|
|
DejsonifySchemaT,
|
|
DejsonifySchemaUnknownKeys,
|
|
DejsonifySchemaCatchall,
|
|
|
|
JsonifiedValues,
|
|
Values
|
|
>,
|
|
|
|
values: JsonifiedValues,
|
|
params?: Partial<z.ParseParams>,
|
|
) {
|
|
return new this(
|
|
await this.dejsonifySchema.parseAsync(values, params)
|
|
)
|
|
}
|
|
|
|
static dejsonifyEffect<
|
|
Instance extends Values,
|
|
|
|
JsonifySchemaT extends z.ZodRawShape,
|
|
JsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
|
JsonifySchemaCatchall extends z.ZodTypeAny,
|
|
|
|
DejsonifySchemaT extends z.ZodRawShape,
|
|
DejsonifySchemaUnknownKeys extends z.UnknownKeysParam,
|
|
DejsonifySchemaCatchall extends z.ZodTypeAny,
|
|
|
|
JsonifiedValues extends JsonifiableObject,
|
|
Values extends object,
|
|
>(
|
|
this: JsonifiableZodSchemaClass<
|
|
Instance,
|
|
|
|
JsonifySchemaT,
|
|
JsonifySchemaUnknownKeys,
|
|
JsonifySchemaCatchall,
|
|
|
|
DejsonifySchemaT,
|
|
DejsonifySchemaUnknownKeys,
|
|
DejsonifySchemaCatchall,
|
|
|
|
JsonifiedValues,
|
|
Values
|
|
>,
|
|
|
|
values: JsonifiedValues,
|
|
params?: Partial<z.ParseParams>,
|
|
) {
|
|
return parseZodTypeEffect(this.dejsonifySchema, values, params).pipe(
|
|
Effect.map(values => new this(values)),
|
|
)
|
|
}
|
|
})
|
|
.build()
|