import { Decimal } from "decimal.js" import { Opaque } from "type-fest" import { z } from "zod" import { identity } from "../../../util" export type JsonifiedDecimal = Opaque export function jsonifyDecimalSchema< S extends z.ZodType >(schema: S) { return schema.transform(v => v.toJSON() as JsonifiedDecimal) } export function dejsonifyDecimalSchema< S extends z.ZodType >(schema: S) { return z .custom(identity) .pipe(z .string() .transform(v => { try { return new Decimal(v) } catch (e) { return v } }) .pipe(schema) ) }