Skip to content

Commit

Permalink
feat(frontend): IcCanistersStrict type and validation (#3356)
Browse files Browse the repository at this point in the history
# Motivation

This might not suffise but, it's a first step, type wise, to defined the
"Index Canister" as optional.

# Notes

I add to include comments because we cannot just set the index as
optional. On the other side, I also need this new validation becase
otherwise I cannot split PR #3334 in smaller PRs.

# Changes

- Provide validation and types `IcCanistersStrictSchema`
- Add TODO for making the Index Canister optional typewise
  • Loading branch information
peterpeterparker authored Nov 5, 2024
1 parent ccc290c commit 474ddea
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
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

0 comments on commit 474ddea

Please sign in to comment.