Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(frontend): IcCanistersStrict type and validation #3356

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/frontend/src/icp/types/ic-token.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
IcAppMetadataSchema,
IcCanistersSchema,
IcCanistersStrictSchema,
IcCkInterfaceSchema,
IcCkLinkedAssetsSchema,
IcCkMetadataSchema,
Expand All @@ -19,6 +20,8 @@ export type IcAppMetadata = z.infer<typeof IcAppMetadataSchema>;

export type IcCanisters = z.infer<typeof IcCanistersSchema>;

export type IcCanistersStrict = z.infer<typeof IcCanistersStrictSchema>;

export type IcCkLinkedAssets = z.infer<typeof IcCkLinkedAssetsSchema>;

export type IcCkMetadata = z.infer<typeof IcCkMetadataSchema>;
Expand Down
5 changes: 5 additions & 0 deletions src/frontend/src/icp/validation/ic-token.validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export const IcAppMetadataSchema = z.object({

export const IcCanistersSchema = z.object({
ledgerCanisterId: CanisterIdTextSchema,
// TODO: Make canister .optional()
indexCanisterId: CanisterIdTextSchema
});

export const IcCanistersStrictSchema = IcCanistersSchema.extend({
indexCanisterId: CanisterIdTextSchema
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import {
IcAppMetadataSchema,
IcCanistersSchema,
IcCanistersStrictSchema,
IcCkInterfaceSchema,
IcCkLinkedAssetsSchema,
IcCkMetadataSchema,
Expand Down Expand Up @@ -98,6 +99,14 @@ describe('Schema Validation Tests', () => {
expect(IcCanistersSchema.parse(validData)).toEqual(validData);
});

// TODO: uncomment when Index canister becomes optional
// it('should validate with ledger canister only', () => {
// const validData = {
// ledgerCanisterId: mockCanisters.ledgerCanisterId
// };
// expect(IcCanistersSchema.parse(validData)).toEqual(validData);
// });

it('should fail with invalid ledger canister id', () => {
const invalidData = {
...validData,
Expand All @@ -120,12 +129,39 @@ describe('Schema Validation Tests', () => {
};
expect(() => IcCanistersSchema.parse(invalidData)).toThrow();
});
});

describe('IcCanistersStrictSchema', () => {
const validToken = {
...mockToken,
...mockFee,
...mockCanisters,
...mockApp
};

it('should validate with correct data', () => {
const validData = {
ledgerCanisterId: mockCanisters.ledgerCanisterId,
indexCanisterId: mockCanisters.ledgerCanisterId
};

expect(IcCanistersSchema.parse(validData)).toEqual(validData);
});

it('should validate a token with index canister correct data', () => {
expect(() => IcCanistersStrictSchema.parse(validToken)).not.toThrow();
});

it('should fail with missing index canister field', () => {
const invalidData = {
ledgerCanisterId: IC_CKBTC_LEDGER_CANISTER_ID
};
expect(() => IcCanistersSchema.parse(invalidData)).toThrow();
expect(() => IcCanistersStrictSchema.parse(invalidData)).toThrow();
});

it('should fail for token with missing index canister field', () => {
const { indexCanisterId: _, ...tokenWithoutIndexCanisterId } = validToken;
expect(() => IcCanistersStrictSchema.parse(tokenWithoutIndexCanisterId)).toThrow();
});
});

Expand Down Expand Up @@ -183,6 +219,12 @@ describe('Schema Validation Tests', () => {
expect(IcInterfaceSchema.parse(validData)).toEqual(validData);
});

// TODO: uncomment when Index canister becomes optional
// it('should validate without Index canister', () => {
// const { indexCanisterId: _, ...restValidData } = validData;
// expect(IcInterfaceSchema.parse(restValidData)).toEqual(restValidData);
// });

it('should fail with incorrect IcCanisters data', () => {
const invalidData = {
...validData,
Expand Down