From 768838285c25cbb1b171f645e8efb37a3c14273a Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 30 Oct 2023 15:27:25 -0300 Subject: local error impl: errors shown fixed position that are wiped when moved from the view --- packages/taler-util/src/amounts.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'packages/taler-util/src/amounts.ts') diff --git a/packages/taler-util/src/amounts.ts b/packages/taler-util/src/amounts.ts index 082a8168e..5c6444b00 100644 --- a/packages/taler-util/src/amounts.ts +++ b/packages/taler-util/src/amounts.ts @@ -26,6 +26,9 @@ import { codecForString, codecForNumber, Codec, + Context, + DecodingError, + renderContext, } from "./codec.js"; import { AmountString } from "./taler-types.js"; @@ -74,7 +77,23 @@ export const codecForAmountJson = (): Codec => .property("fraction", codecForNumber()) .build("AmountJson"); -export const codecForAmountString = (): Codec => codecForString() as Codec; +export function codecForAmountString(): Codec { + return { + decode(x: any, c?: Context): AmountString { + if (typeof x !== "string") { + throw new DecodingError( + `expected string at ${renderContext(c)} but got ${typeof x}`, + ); + } + if (Amounts.parse(x) === undefined) { + throw new DecodingError( + `invalid amount at ${renderContext(c)} got "${x}"`, + ); + } + return x as AmountString; + }, + }; +} /** * Result of a possibly overflowing operation. -- cgit v1.2.3