Skip to content

Commit f9c8f83

Browse files
authored
fix(sdk): replace z.coerce.bigint with pipe-based coercion for JSON schema compat (#8595)
1 parent 313848c commit f9c8f83

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

.changeset/bright-lizards-glow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hyperlane-xyz/sdk': patch
3+
---
4+
5+
Replaced z.coerce.bigint().positive() with pipe-based coercion in TokenMetadataSchema scale field to fix zod-to-json-schema compatibility in the registry build.

typescript/sdk/src/token/types.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,33 @@ export const contractVersionMatchesDependency = (version: string) => {
3333

3434
export const VERSION_ERROR_MESSAGE = `Contract version must match the @hyperlane-xyz/core dependency version (${CONTRACTS_PACKAGE_VERSION})`;
3535

36+
/**
37+
* Coerces bigint or string to bigint with positivity check.
38+
* Needed because bigints are serialised as strings in JSON/YAML
39+
* and must be converted back on parse. Uses .refine() instead of
40+
* .positive() to avoid bigint values in zod-to-json-schema output.
41+
*/
42+
const PositiveBigIntFromString = z
43+
.union([
44+
z.bigint(),
45+
z
46+
.string()
47+
.refine(
48+
(s) => {
49+
try {
50+
BigInt(s);
51+
return true;
52+
} catch {
53+
return false;
54+
}
55+
},
56+
{ message: 'Must be a bigint-compatible string' },
57+
)
58+
.transform((s) => BigInt(s)),
59+
])
60+
.pipe(z.bigint())
61+
.refine((n) => n > 0n, { message: 'Must be positive' });
62+
3663
export const TokenMetadataSchema = z.object({
3764
name: z.string(),
3865
symbol: z.string(),
@@ -45,8 +72,8 @@ export const TokenMetadataSchema = z.object({
4572
denominator: z.number().int().gt(0),
4673
}),
4774
z.object({
48-
numerator: z.coerce.bigint().positive(),
49-
denominator: z.coerce.bigint().positive(),
75+
numerator: PositiveBigIntFromString,
76+
denominator: PositiveBigIntFromString,
5077
}),
5178
])
5279
.optional(),

0 commit comments

Comments
 (0)