Option jsonification work
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Julien Valverdé
2024-03-21 14:38:12 +01:00
parent a5c5b62c9f
commit 7bc302d882
4 changed files with 38 additions and 52 deletions

View File

@@ -1,19 +1,19 @@
import { Decimal } from "decimal.js"
import { identity } from "lodash-es"
import { Opaque } from "type-fest"
import { z } from "zod"
export type JsonifiedDecimal = Opaque<string, "@thilawyn/zod-schema-class/JsonifiedDecimal">
export type JsonifiedDecimalBrand = "@thilawyn/zod-schema-class/JsonifiedDecimal"
export function jsonifyDecimalSchema<S extends z.ZodType<Decimal>>(schema: S) {
return schema.transform(v => v.toJSON() as JsonifiedDecimal)
return schema
.transform(v => v.toJSON())
.brand<JsonifiedDecimalBrand>()
}
export function dejsonifyDecimalSchema<S extends z.ZodType<Decimal>>(schema: S) {
return z
.custom<JsonifiedDecimal>(identity)
.custom<string & z.BRAND<JsonifiedDecimalBrand>>()
.pipe(
z
.string()