Files
zod-schema-class/src/legacy/jsonifiable/schema/bigint.ts
Julien Valverdé fc95a5d53a
All checks were successful
continuous-integration/drone/push Build is passing
Moved current version to legacy
2024-01-20 22:02:14 +01:00

29 lines
738 B
TypeScript

import { Opaque } from "type-fest"
import { z } from "zod"
import { identity } from "../../../util"
export type JsonifiedBigInt = Opaque<string, "@thilawyn/schemable-class/JsonifiedBigInt">
export function jsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
return schema.transform(v => v.toString() as JsonifiedBigInt)
}
export function dejsonifyBigIntSchema<S extends z.ZodBigInt>(schema: S) {
return z
.custom<JsonifiedBigInt>(identity)
.pipe(z
.string()
.transform(v => {
try {
return BigInt(v)
}
catch (e) {
return v
}
})
.pipe(schema)
)
}