Skip to content

Commit

Permalink
refactor(frontend): add SVG schema to network schema (#3875)
Browse files Browse the repository at this point in the history
# Motivation

As suggested in the past, we should refine the schema of network type to
be more specific for the icons.
  • Loading branch information
AntonioVentilii authored Dec 6, 2024
1 parent 0436dca commit b390208
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/frontend/src/lib/schema/network.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ export const NetworkAppMetadataSchema = z.object({
explorerUrl: UrlSchema
});

const IconSchema = z
.string()
.refine((value) => value.endsWith('.svg'), { message: 'Must be an SVG file' });

export const NetworkSchema = z.object({
id: NetworkIdSchema,
env: NetworkEnvironmentSchema,
name: z.string(),
icon: z.string().optional(),
iconBW: z.string().optional(),
icon: IconSchema.optional(),
iconBW: IconSchema.optional(),
buy: z.custom<AtLeastOne<NetworkBuy>>().optional()
});
20 changes: 18 additions & 2 deletions src/frontend/src/tests/lib/schema/network.schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ describe('network.schema', () => {

const validNetwork = {
...validNetworkWithRequiredFields,
icon: 'https://example.com/icon.png',
iconBW: 'https://example.com/icon-bw.png',
icon: 'https://example.com/icon.svg',
iconBW: 'https://example.com/icon-bw.svg',
buy: { onramperId: 'icp' }
};

Expand Down Expand Up @@ -115,5 +115,21 @@ describe('network.schema', () => {
const { name: _, ...invalidNetwork } = validNetwork;
expect(() => NetworkSchema.parse(invalidNetwork)).toThrow();
});

it('should fail validation when icon is not a valid SVG string', () => {
const invalidNetwork = {
...validNetwork,
icon: 'https://example.com/invalid-icon.png'
};
expect(() => NetworkSchema.parse(invalidNetwork)).toThrow();
});

it('should fail validation when iconBW is not a valid SVG string', () => {
const invalidNetwork = {
...validNetwork,
iconBW: 'https://example.com/invalid-icon-bw.png'
};
expect(() => NetworkSchema.parse(invalidNetwork)).toThrow();
});
});
});

0 comments on commit b390208

Please sign in to comment.