Skip to content

Commit

Permalink
feat(frontend): CoingeckoCoinsId zod (#3329)
Browse files Browse the repository at this point in the history
# Motivation

Zod for `CoingeckoCoinsId`.

# Notes

I just convert the type I need for the PR / task I'm working on in #3326
and not all the Coingecko types.
  • Loading branch information
peterpeterparker authored Nov 5, 2024
1 parent 731006d commit 15de402
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/frontend/src/lib/types/coingecko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
// *refers to curl -l https://api.coingecko.com/api/v3/coins/list
import type { LedgerCanisterIdText } from '$icp/types/canister';
import type { EthAddress } from '$lib/types/address';
import type { CoingeckoCoinsIdSchema } from '$lib/validation/coingecko.validation';
import { z } from 'zod';

export type CoingeckoCoinsId = 'ethereum' | 'bitcoin' | 'internet-computer';
export type CoingeckoCoinsId = z.infer<typeof CoingeckoCoinsIdSchema>;

// We are interested only in the ERC20 <> USD on Ethereum and in the ICRC <> USD on Internet Computer, therefore not an exhaustive list.
// *refers to curl -l https://api.coingecko.com/api/v3/asset_platforms
Expand Down
3 changes: 3 additions & 0 deletions src/frontend/src/lib/validation/coingecko.validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { z } from 'zod';

export const CoingeckoCoinsIdSchema = z.enum(['ethereum', 'bitcoin', 'internet-computer']);
39 changes: 39 additions & 0 deletions src/frontend/src/tests/lib/validation/coingecko.validation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { CoingeckoCoinsIdSchema } from '$lib/validation/coingecko.validation';
import { describe, expect, it } from 'vitest';

describe('CoingeckoCoinsIdSchema', () => {
it('should pass validation for "ethereum"', () => {
const validData = 'ethereum';
expect(CoingeckoCoinsIdSchema.parse(validData)).toEqual(validData);
});

it('should pass validation for "bitcoin"', () => {
const validData = 'bitcoin';
expect(CoingeckoCoinsIdSchema.parse(validData)).toEqual(validData);
});

it('should pass validation for "internet-computer"', () => {
const validData = 'internet-computer';
expect(CoingeckoCoinsIdSchema.parse(validData)).toEqual(validData);
});

it('should fail validation for an unsupported coin ID', () => {
const invalidData = 'dogecoin';
expect(() => CoingeckoCoinsIdSchema.parse(invalidData)).toThrow();
});

it('should fail validation for a number instead of a string', () => {
const invalidData = 123;
expect(() => CoingeckoCoinsIdSchema.parse(invalidData)).toThrow();
});

it('should fail validation for null', () => {
const invalidData = null;
expect(() => CoingeckoCoinsIdSchema.parse(invalidData)).toThrow();
});

it('should fail validation for undefined', () => {
const invalidData = undefined;
expect(() => CoingeckoCoinsIdSchema.parse(invalidData)).toThrow();
});
});

0 comments on commit 15de402

Please sign in to comment.