taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit d5a46a3bc4025d9c2abd2d73831bf033272f174d
parent eca75bbeb2c9a4ae7fcb58c1cfeb5957720a8b40
Author: Sebastian <sebasjm@gmail.com>
Date:   Thu, 30 Nov 2023 10:30:32 -0300

check conversion URL

Diffstat:
Mpackages/taler-util/src/codec.ts | 33+++++++++++++++++++++++++++++++++
Mpackages/taler-util/src/taler-types.ts | 3++-
2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/packages/taler-util/src/codec.ts b/packages/taler-util/src/codec.ts @@ -322,6 +322,39 @@ export function codecForString(): Codec<string> { } /** + * Return a codec for a value that must be a string. + */ +export function codecForStringURL(shouldEndWithSlash?: boolean): Codec<string> { + return { + decode(x: any, c?: Context): string { + if (typeof x !== "string") { + throw new DecodingError( + `expected string at ${renderContext(c)} but got ${typeof x}`, + ); + } + if (shouldEndWithSlash && !x.endsWith("/")) { + throw new DecodingError( + `expected URL string that ends with slash at ${renderContext(c)} but got ${x}`, + ); + } + try { + const url = new URL(x) + return x; + } catch(e) { + if (e instanceof Error) { + throw new DecodingError(e.message) + } else { + throw new DecodingError( + `expected an URL string at ${renderContext(c)} but got "${x}"`, + ); + } + + } + }, + }; +} + +/** * Codec that allows any value. */ export function codecForAny(): Codec<any> { diff --git a/packages/taler-util/src/taler-types.ts b/packages/taler-util/src/taler-types.ts @@ -38,6 +38,7 @@ import { codecForMap, codecForNumber, codecForString, + codecForStringURL, codecOptional, } from "./codec.js"; import { strcmp } from "./helpers.js"; @@ -2372,7 +2373,7 @@ export interface ExchangeWireAccount { export const codecForExchangeWireAccount = (): Codec<ExchangeWireAccount> => buildCodecForObject<ExchangeWireAccount>() - .property("conversion_url", codecOptional(codecForString())) + .property("conversion_url", codecOptional(codecForStringURL())) .property("credit_restrictions", codecForList(codecForAny())) .property("debit_restrictions", codecForList(codecForAny())) .property("master_sig", codecForString())