diff --git a/src/schema/decimal.ts b/src/schema/decimal.ts index cb16e42..5f946b7 100644 --- a/src/schema/decimal.ts +++ b/src/schema/decimal.ts @@ -2,4 +2,35 @@ import Decimal from "decimal.js" import { z } from "zod" -export const decimal = z.custom(v => Decimal.isDecimal(v)) +type JsonifiedDecimalBrand = "@thilawyn/zod-schema-class/JsonifiedDecimal" + + +export function decimal() { + return z.custom(v => Decimal.isDecimal(v)) +} + +export module decimal { + export function jsonifyDecimalSchema>(schema: S) { + return schema + .transform(v => v.toJSON()) + .brand() + } + + export function dejsonifyDecimalSchema>(schema: S) { + return z + .custom>() + .pipe( + z + .string() + .transform(v => { + try { + return new Decimal(v) + } + catch (e) { + return v + } + }) + ) + .pipe(schema) + } +} diff --git a/src/schema/jsonified/decimal.ts b/src/schema/jsonified/decimal.ts deleted file mode 100644 index e638a80..0000000 --- a/src/schema/jsonified/decimal.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { Decimal } from "decimal.js" -import { z } from "zod" - - -export type JsonifiedDecimalBrand = "@thilawyn/zod-schema-class/JsonifiedDecimal" - - -export function jsonifyDecimalSchema>(schema: S) { - return schema - .transform(v => v.toJSON()) - .brand() -} - -export function dejsonifyDecimalSchema>(schema: S) { - return z - .custom>() - .pipe( - z - .string() - .transform(v => { - try { - return new Decimal(v) - } - catch (e) { - return v - } - }) - ) - .pipe(schema) -} diff --git a/src/schema/jsonified/index.ts b/src/schema/jsonified/index.ts index 6bff7ae..d307697 100644 --- a/src/schema/jsonified/index.ts +++ b/src/schema/jsonified/index.ts @@ -1,16 +1,13 @@ import { dejsonifyBigIntSchema, jsonifyBigIntSchema } from "./bigint" import { dejsonifyDateSchema, jsonifyDateSchema } from "./date" -import { dejsonifyDecimalSchema, jsonifyDecimalSchema } from "./decimal" export const jsonify = { bigint: jsonifyBigIntSchema, date: jsonifyDateSchema, - decimal: jsonifyDecimalSchema, } as const export const dejsonify = { bigint: dejsonifyBigIntSchema, date: dejsonifyDateSchema, - decimal: dejsonifyDecimalSchema, } as const