Skip to content

Commit

Permalink
feat(frontend): Use Zod for Token type (#3252)
Browse files Browse the repository at this point in the history
# 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).
  • Loading branch information
AntonioVentilii-DFINITY authored Oct 31, 2024
1 parent 13d66b9 commit 75f0bb1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/frontend/src/lib/types/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
39 changes: 24 additions & 15 deletions src/frontend/src/lib/types/token.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<OnramperId>().optional()
});

const TokenBuyableSchema = z.object({
buy: z.custom<AtLeastOne<TokenBuy>>().optional()
});

const TokenSchema = z
.object({
id: TokenIdSchema,
network: NetworkSchema,
standard: TokenStandardSchema,
category: TokenCategorySchema
})
.merge(TokenMetadataSchema)
.merge(TokenAppearanceSchema)
.merge(TokenBuyableSchema);

export type TokenId = z.infer<typeof TokenIdSchema>;

export type TokenStandard = z.infer<typeof TokenStandardSchema>;

export type TokenCategory = z.infer<typeof TokenCategorySchema>;

export type Token = {
id: TokenId;
network: Network;
standard: TokenStandard;
category: TokenCategory;
} & TokenMetadata &
TokenAppearance &
TokenBuyable;
export type Token = z.infer<typeof TokenSchema>;

export type TokenMetadata = z.infer<typeof TokenMetadataSchema>;

export type TokenAppearance = z.infer<typeof TokenAppearanceSchema>;

export interface TokenBuyable {
buy?: AtLeastOne<TokenBuy>;
}
export type TokenBuyable = z.infer<typeof TokenBuyableSchema>;

export interface TokenBuy {
onramperId?: OnramperId;
}
export type TokenBuy = z.infer<typeof TokenBuySchema>;

export interface TokenLinkedData {
twinTokenSymbol?: string;
Expand Down

0 comments on commit 75f0bb1

Please sign in to comment.