|
| 1 | +import { type Currency, currencies, getCurrencyFromCode } from "@thesis-co/cent" |
| 2 | +import { z } from "zod" |
| 3 | +import { zNonNegativeBigIntString } from "./common" |
| 4 | + |
| 5 | +/** |
| 6 | + * Schema that validates a currency code string and transforms to Currency object |
| 7 | + */ |
| 8 | +export const zCurrencyCode = z.string().transform((code, ctx) => { |
| 9 | + try { |
| 10 | + return getCurrencyFromCode(code) |
| 11 | + } catch { |
| 12 | + ctx.addIssue({ |
| 13 | + code: z.ZodIssueCode.custom, |
| 14 | + message: `Unknown currency code: ${code}`, |
| 15 | + }) |
| 16 | + return z.NEVER |
| 17 | + } |
| 18 | +}) |
| 19 | + |
| 20 | +/** |
| 21 | + * Schema for full Currency object representation (validation only, no transform) |
| 22 | + */ |
| 23 | +export const zCurrencyObject = z.object({ |
| 24 | + name: z.string(), |
| 25 | + code: z.string(), |
| 26 | + decimals: z.union([z.bigint(), zNonNegativeBigIntString]), |
| 27 | + symbol: z.string(), |
| 28 | + fractionalUnit: z |
| 29 | + .union([ |
| 30 | + z.string(), |
| 31 | + z.array(z.string()), |
| 32 | + z.record(z.string(), z.union([z.string(), z.array(z.string())])), |
| 33 | + ]) |
| 34 | + .optional(), |
| 35 | + iso4217Support: z.boolean().optional(), |
| 36 | +}) |
| 37 | + |
| 38 | +/** |
| 39 | + * Options for zCurrency schema |
| 40 | + */ |
| 41 | +export interface ZCurrencyOptions { |
| 42 | + /** Only allow these currency codes */ |
| 43 | + allowed?: string[] |
| 44 | + /** Deny these currency codes */ |
| 45 | + denied?: string[] |
| 46 | + /** Filter by currency type */ |
| 47 | + type?: "fiat" | "crypto" | "all" |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Create a currency validation schema |
| 52 | + * |
| 53 | + * @example |
| 54 | + * ```ts |
| 55 | + * // Any valid currency |
| 56 | + * const schema = zCurrency() |
| 57 | + * schema.parse("USD") // Returns USD Currency object |
| 58 | + * |
| 59 | + * // Only specific currencies |
| 60 | + * const usdEurSchema = zCurrency({ allowed: ["USD", "EUR"] }) |
| 61 | + * |
| 62 | + * // Only fiat currencies |
| 63 | + * const fiatSchema = zCurrency({ type: "fiat" }) |
| 64 | + * ``` |
| 65 | + */ |
| 66 | +export function zCurrency(options?: ZCurrencyOptions) { |
| 67 | + return z.string().transform((code, ctx) => { |
| 68 | + const upperCode = code.toUpperCase() |
| 69 | + |
| 70 | + // Check allowlist |
| 71 | + if (options?.allowed && !options.allowed.includes(upperCode)) { |
| 72 | + ctx.addIssue({ |
| 73 | + code: z.ZodIssueCode.custom, |
| 74 | + message: `Currency ${upperCode} is not in allowed list: ${options.allowed.join(", ")}`, |
| 75 | + }) |
| 76 | + return z.NEVER |
| 77 | + } |
| 78 | + |
| 79 | + // Check denylist |
| 80 | + if (options?.denied?.includes(upperCode)) { |
| 81 | + ctx.addIssue({ |
| 82 | + code: z.ZodIssueCode.custom, |
| 83 | + message: `Currency ${upperCode} is not allowed`, |
| 84 | + }) |
| 85 | + return z.NEVER |
| 86 | + } |
| 87 | + |
| 88 | + // Get the currency |
| 89 | + let currency: Currency |
| 90 | + try { |
| 91 | + currency = getCurrencyFromCode(upperCode) |
| 92 | + } catch { |
| 93 | + ctx.addIssue({ |
| 94 | + code: z.ZodIssueCode.custom, |
| 95 | + message: `Unknown currency code: ${code}`, |
| 96 | + }) |
| 97 | + return z.NEVER |
| 98 | + } |
| 99 | + |
| 100 | + // Check type filter |
| 101 | + if (options?.type && options.type !== "all") { |
| 102 | + const isFiat = currency.iso4217Support === true |
| 103 | + if (options.type === "fiat" && !isFiat) { |
| 104 | + ctx.addIssue({ |
| 105 | + code: z.ZodIssueCode.custom, |
| 106 | + message: `Currency ${upperCode} is not a fiat currency`, |
| 107 | + }) |
| 108 | + return z.NEVER |
| 109 | + } |
| 110 | + if (options.type === "crypto" && isFiat) { |
| 111 | + ctx.addIssue({ |
| 112 | + code: z.ZodIssueCode.custom, |
| 113 | + message: `Currency ${upperCode} is not a cryptocurrency`, |
| 114 | + }) |
| 115 | + return z.NEVER |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return currency |
| 120 | + }) |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * Get all valid currency codes |
| 125 | + */ |
| 126 | +export function getValidCurrencyCodes(): string[] { |
| 127 | + return Object.keys(currencies) |
| 128 | +} |
0 commit comments