All checks were successful
continuous-integration/drone/push Build is passing
Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://git.jvalver.de/Thilawyn/schemable-class/pulls/2
29 lines
735 B
TypeScript
29 lines
735 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)
|
|
)
|
|
}
|