Skip to content

Commit 6822911

Browse files
committed
refactor(config): validate Chain() parameters
1 parent 7d5ec63 commit 6822911

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

packages/config/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ export class Chain implements ChainJSON {
3939
public rpcEndpoints: RPCEndpoint[],
4040
public contracts: Contracts,
4141
) {
42+
if (name === "") {
43+
throw new Error("Chain name is required")
44+
}
4245
this.name = name
46+
if (id < 0) {
47+
throw new Error("Chain ID cannot be negative")
48+
}
4349
this.id = id
4450
this.rpcEndpoints = new Array<RPCEndpoint>()
4551
for (const rpcEndpoint of rpcEndpoints) {

packages/config/test/index.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,63 @@ describe("Load configuration from JSON file", () => {
4242
const chain = new config.Chain("GNOSIS", 123, new Array<config.RPCEndpoint>(), new config.Contracts())
4343
assert.equal(chain.toString(), "gnosis")
4444
})
45+
describe("Chain constructor", () => {
46+
const testCases: {
47+
name: string;
48+
chain: {
49+
name: string;
50+
id: number;
51+
rpcEndpoints: config.RPCEndpoint[];
52+
contracts: config.Contracts;
53+
};
54+
expectedErrorMessage: string;
55+
}[] = [
56+
{
57+
name: "name cannot be empty",
58+
chain: {
59+
name: "",
60+
id: 0,
61+
rpcEndpoints: new Array<config.RPCEndpoint>(),
62+
contracts: new config.Contracts(),
63+
},
64+
expectedErrorMessage: "Chain name is required",
65+
},
66+
{
67+
name: "id cannot be negative",
68+
chain: {
69+
name: "ethereum",
70+
id: -1,
71+
rpcEndpoints: new Array<config.RPCEndpoint>(),
72+
contracts: new config.Contracts(),
73+
},
74+
expectedErrorMessage: "Chain ID cannot be negative",
75+
},
76+
{
77+
name: "all good",
78+
chain: {
79+
name: "ethereum",
80+
id: 1,
81+
rpcEndpoints: new Array<config.RPCEndpoint>(),
82+
contracts: new config.Contracts(),
83+
},
84+
expectedErrorMessage: "",
85+
},
86+
]
87+
testCases.forEach((tc) => {
88+
it(tc.name, async () => {
89+
try {
90+
new config.Chain(tc.chain.name, tc.chain.id, tc.chain.rpcEndpoints, tc.chain.contracts)
91+
if (tc.expectedErrorMessage !== "") {
92+
assert.fail("expecting error")
93+
}
94+
} catch (err: any) {
95+
if (tc.expectedErrorMessage !== "") {
96+
assert.equal(err.message, tc.expectedErrorMessage)
97+
} else {
98+
assert.fail(`unexpected error: "${err}"`)
99+
}
100+
}
101+
})
102+
})
103+
})
45104
})

0 commit comments

Comments
 (0)