From 75f0bb12933a316ffee595ea4b1f1d44c53aa36e Mon Sep 17 00:00:00 2001 From: Antonio Ventilii <169057656+AntonioVentilii-DFINITY@users.noreply.github.com> Date: Thu, 31 Oct 2024 17:21:03 +0100 Subject: [PATCH] feat(frontend): Use Zod for Token type (#3252) # Motivation We ultimately use `Zod` for `Token` type. That requires a few more sub-interfaces to use `Zod` too (for example the ones related to OnRamper). --- src/frontend/src/lib/types/network.ts | 2 +- src/frontend/src/lib/types/token.ts | 39 ++++++++++++++++----------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/frontend/src/lib/types/network.ts b/src/frontend/src/lib/types/network.ts index 0a425afcc1..fe7b7f26f7 100644 --- a/src/frontend/src/lib/types/network.ts +++ b/src/frontend/src/lib/types/network.ts @@ -16,7 +16,7 @@ const NetworkAppMetadataSchema = z.object({ explorerUrl: UrlSchema }); -const NetworkSchema = z.object({ +export const NetworkSchema = z.object({ id: NetworkIdSchema, env: NetworkEnvironmentSchema, name: z.string(), diff --git a/src/frontend/src/lib/types/token.ts b/src/frontend/src/lib/types/token.ts index a936f58ed6..36e7402f2f 100644 --- a/src/frontend/src/lib/types/token.ts +++ b/src/frontend/src/lib/types/token.ts @@ -1,5 +1,5 @@ import type { OptionBalance } from '$lib/types/balance'; -import type { Network } from '$lib/types/network'; +import { NetworkSchema } from '$lib/types/network'; import type { OnramperId } from '$lib/types/onramper'; import type { AtLeastOne, Option, RequiredExcept } from '$lib/types/utils'; import { z } from 'zod'; @@ -31,32 +31,41 @@ const TokenAppearanceSchema = z.object({ oisyName: TokenOisyNameSchema.optional() }); +// TODO: use Zod to validate the OnramperId +const TokenBuySchema = z.object({ + onramperId: z.custom().optional() +}); + +const TokenBuyableSchema = z.object({ + buy: z.custom>().optional() +}); + +const TokenSchema = z + .object({ + id: TokenIdSchema, + network: NetworkSchema, + standard: TokenStandardSchema, + category: TokenCategorySchema + }) + .merge(TokenMetadataSchema) + .merge(TokenAppearanceSchema) + .merge(TokenBuyableSchema); + export type TokenId = z.infer; export type TokenStandard = z.infer; export type TokenCategory = z.infer; -export type Token = { - id: TokenId; - network: Network; - standard: TokenStandard; - category: TokenCategory; -} & TokenMetadata & - TokenAppearance & - TokenBuyable; +export type Token = z.infer; export type TokenMetadata = z.infer; export type TokenAppearance = z.infer; -export interface TokenBuyable { - buy?: AtLeastOne; -} +export type TokenBuyable = z.infer; -export interface TokenBuy { - onramperId?: OnramperId; -} +export type TokenBuy = z.infer; export interface TokenLinkedData { twinTokenSymbol?: string;