Skip to content

Commit 4022c39

Browse files
committed
feat: schema, interface and mocks update for TokenInfo
Signed-off-by: rozekmichal <michal.rozek@blockydevs.com>
1 parent a609820 commit 4022c39

File tree

7 files changed

+69
-65
lines changed

7 files changed

+69
-65
lines changed

src/__tests__/mocks/mocks.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,7 @@ export const makeMirrorMock = (
435435
treasury_account_id: '0.0.1234',
436436
created_timestamp: '1234567890',
437437
deleted: false,
438-
default_freeze_status: false,
439-
default_kyc_status: false,
438+
freeze_default: false,
440439
pause_status: 'NOT_APPLICABLE',
441440
memo: '',
442441
});
@@ -451,8 +450,7 @@ export const makeMirrorMock = (
451450
treasury_account_id: '0.0.1234',
452451
created_timestamp: '1234567890',
453452
deleted: false,
454-
default_freeze_status: false,
455-
default_kyc_status: false,
453+
freeze_default: false,
456454
pause_status: 'NOT_APPLICABLE',
457455
memo: '',
458456
});

src/core/errors/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ export { NotFoundError } from './not-found-error';
1212
export { StateError } from './state-error';
1313
export { TransactionError } from './transaction-error';
1414
export { TransactionPrecheckError } from './transaction-precheck-error';
15-
export { ValidationError } from './validation-error';
15+
export {
16+
formatZodIssueLine,
17+
formatZodIssuesForMessage,
18+
ValidationError,
19+
} from './validation-error';

src/core/errors/validation-error.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
import type { ZodError } from 'zod';
22

3+
import { type $ZodIssue, toDotPath } from 'zod/v4/core';
4+
35
import { CliError } from './cli-error';
46

7+
export function formatZodIssueLine(issue: $ZodIssue): string {
8+
const path = issue.path.length === 0 ? '(root)' : toDotPath(issue.path);
9+
return `${path}: ${issue.message}`;
10+
}
11+
12+
export function formatZodIssuesForMessage(zodError: ZodError): string {
13+
return zodError.issues.map((i) => ` - ${formatZodIssueLine(i)}`).join('\n');
14+
}
15+
516
export class ValidationError extends CliError {
617
static readonly CODE = 'VALIDATION_ERROR';
718

@@ -21,11 +32,13 @@ export class ValidationError extends CliError {
2132
}
2233

2334
static fromZod(zodError: ZodError): ValidationError {
24-
const issues = zodError.issues.map((i) => i.message);
25-
const message = `Validation failed:\n${issues.map((i) => ` - ${i}`).join('\n')}`;
35+
const issueLines = zodError.issues.map((issue) =>
36+
formatZodIssueLine(issue),
37+
);
38+
const message = `Validation failed:\n${formatZodIssuesForMessage(zodError)}`;
2639

2740
return new ValidationError(message, {
28-
context: { issues },
41+
context: { issues: issueLines },
2942
cause: zodError,
3043
});
3144
}

src/core/services/mirrornode/__tests__/unit/mocks.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ export const createMockTokenInfo = (
116116
treasury_account_id: '0.0.1234',
117117
created_timestamp: '2024-01-01T12:00:00.000Z',
118118
deleted: false,
119-
default_freeze_status: false,
120-
default_kyc_status: false,
119+
freeze_default: false,
121120
pause_status: 'UNPAUSED',
122121
memo: '',
123122
...overrides,
@@ -139,8 +138,7 @@ export const createMockMirrorNodeTokenByIdJson = (
139138
treasury_account_id: '0.0.1234',
140139
created_timestamp: '2024-01-01T12:00:00.000Z',
141140
deleted: false,
142-
default_freeze_status: false,
143-
default_kyc_status: false,
141+
freeze_default: false,
144142
pause_status: 'UNPAUSED',
145143
memo: '',
146144
...overrides,

src/core/services/mirrornode/schema.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import type { TokenInfo } from './types';
22

33
import { z } from 'zod';
44

5-
const optionalMirrorKey = z
6-
.object({
7-
_type: z.string(),
8-
key: z.string(),
9-
})
10-
.optional();
5+
const mirrorKeyObject = z.object({
6+
_type: z.string(),
7+
key: z.string(),
8+
});
9+
10+
const optionalKeyRef = z.union([mirrorKeyObject, z.null()]).optional();
1111

1212
export const TokenInfoSchema: z.ZodType<TokenInfo> = z.object({
1313
token_id: z.string(),
@@ -18,17 +18,16 @@ export const TokenInfoSchema: z.ZodType<TokenInfo> = z.object({
1818
max_supply: z.string(),
1919
type: z.string(),
2020
treasury_account_id: z.string(),
21-
admin_key: optionalMirrorKey,
22-
kyc_key: optionalMirrorKey,
23-
freeze_key: optionalMirrorKey,
24-
wipe_key: optionalMirrorKey,
25-
supply_key: optionalMirrorKey,
26-
fee_schedule_key: optionalMirrorKey,
27-
pause_key: optionalMirrorKey,
21+
admin_key: optionalKeyRef,
22+
kyc_key: optionalKeyRef,
23+
freeze_key: optionalKeyRef,
24+
wipe_key: optionalKeyRef,
25+
supply_key: optionalKeyRef,
26+
fee_schedule_key: optionalKeyRef,
27+
pause_key: optionalKeyRef,
2828
created_timestamp: z.string(),
29-
deleted: z.boolean(),
30-
default_freeze_status: z.boolean(),
31-
default_kyc_status: z.boolean(),
29+
deleted: z.boolean().nullable().optional(),
30+
freeze_default: z.boolean().optional(),
3231
pause_status: z.string(),
3332
memo: z.string(),
3433
});

src/core/services/mirrornode/types.ts

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ export interface TokenBalanceInfo {
6060
decimals: number;
6161
}
6262

63-
// Token Info
63+
export type MirrorNodeTokenKey = {
64+
_type: string;
65+
key: string;
66+
};
67+
6468
export interface TokenInfo {
6569
token_id: string;
6670
symbol: string;
@@ -70,38 +74,16 @@ export interface TokenInfo {
7074
max_supply: string;
7175
type: string;
7276
treasury_account_id: string;
73-
admin_key?: {
74-
_type: string;
75-
key: string;
76-
};
77-
kyc_key?: {
78-
_type: string;
79-
key: string;
80-
};
81-
freeze_key?: {
82-
_type: string;
83-
key: string;
84-
};
85-
wipe_key?: {
86-
_type: string;
87-
key: string;
88-
};
89-
supply_key?: {
90-
_type: string;
91-
key: string;
92-
};
93-
fee_schedule_key?: {
94-
_type: string;
95-
key: string;
96-
};
97-
pause_key?: {
98-
_type: string;
99-
key: string;
100-
};
77+
admin_key?: MirrorNodeTokenKey | null;
78+
kyc_key?: MirrorNodeTokenKey | null;
79+
freeze_key?: MirrorNodeTokenKey | null;
80+
wipe_key?: MirrorNodeTokenKey | null;
81+
supply_key?: MirrorNodeTokenKey | null;
82+
fee_schedule_key?: MirrorNodeTokenKey | null;
83+
pause_key?: MirrorNodeTokenKey | null;
10184
created_timestamp: string;
102-
deleted: boolean;
103-
default_freeze_status: boolean;
104-
default_kyc_status: boolean;
85+
deleted?: boolean | null;
86+
freeze_default?: boolean;
10587
pause_status: string;
10688
memo: string;
10789
}
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { z } from 'zod';
22

3-
import { ValidationError } from '@/core/errors';
3+
import {
4+
formatZodIssueLine,
5+
formatZodIssuesForMessage,
6+
ValidationError,
7+
} from '@/core/errors';
48

59
export function parseWithSchema<T>(
610
schema: z.ZodType<T>,
@@ -9,10 +13,16 @@ export function parseWithSchema<T>(
913
): T {
1014
const result = schema.safeParse(data);
1115
if (!result.success) {
12-
throw new ValidationError(`Invalid response (${context})`, {
13-
cause: result.error,
14-
context: { issues: result.error.issues },
15-
});
16+
const issueLines = result.error.issues.map((issue) =>
17+
formatZodIssueLine(issue),
18+
);
19+
throw new ValidationError(
20+
`Invalid response (${context}):\n${formatZodIssuesForMessage(result.error)}`,
21+
{
22+
cause: result.error,
23+
context: { issues: issueLines },
24+
},
25+
);
1626
}
1727
return result.data;
1828
}

0 commit comments

Comments
 (0)