diff options
author | Sebastian <sebasjm@gmail.com> | 2023-11-30 10:30:32 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-11-30 10:30:40 -0300 |
commit | d5a46a3bc4025d9c2abd2d73831bf033272f174d (patch) | |
tree | 981a114f8805baaf09c51c3878b5c1be9be30e67 | |
parent | eca75bbeb2c9a4ae7fcb58c1cfeb5957720a8b40 (diff) |
check conversion URL
-rw-r--r-- | packages/taler-util/src/codec.ts | 33 | ||||
-rw-r--r-- | packages/taler-util/src/taler-types.ts | 3 |
2 files changed, 35 insertions, 1 deletions
diff --git a/packages/taler-util/src/codec.ts b/packages/taler-util/src/codec.ts index 695f3c147..670111b75 100644 --- 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 index e32c5a99d..3b96c52fe 100644 --- 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()) |