Skip to content

Commit c42b5c5

Browse files
kshyun28alcuadrado
authored andcommitted
Add config validation for empty network url
1 parent 221f640 commit c42b5c5

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

packages/hardhat-core/src/internal/core/config/config-validation.ts

+22
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ function getMessage(e: ValidationError): string {
5858
);
5959
}
6060

61+
function getEmptyErrorMessage(path: string, value: any, expectedType: string) {
62+
return `Empty value ${stringify(
63+
value
64+
)} for ${path} - Expected a non-empty value of type ${expectedType}.`;
65+
}
66+
6167
function getErrorMessage(path: string, value: any, expectedType: string) {
6268
return `Invalid value ${stringify(
6369
value
@@ -148,6 +154,14 @@ function isDecimalString(v: unknown): v is string {
148154
return v.match(DEC_STRING_REGEX) !== null;
149155
}
150156

157+
function isEmptyString(v: unknown): v is string {
158+
if (typeof v !== "string") {
159+
return false;
160+
}
161+
162+
return v.trim().length === 0;
163+
}
164+
151165
export const hexString = new t.Type<string>(
152166
"hex string",
153167
isHexString,
@@ -535,6 +549,14 @@ export function getValidationErrors(config: any): string[] {
535549
"string"
536550
)
537551
);
552+
} else if (isEmptyString(netConfig.url)) {
553+
errors.push(
554+
getEmptyErrorMessage(
555+
`HardhatConfig.networks.${networkName}.url`,
556+
netConfig.url,
557+
"string"
558+
)
559+
);
538560
}
539561
}
540562

packages/hardhat-core/test/internal/core/config/config-validation.ts

+14
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,20 @@ describe("Config validation", function () {
11711171
);
11721172
});
11731173

1174+
it("Should fail if an empty url is set for custom networks", function () {
1175+
// Empty string
1176+
expectHardhatError(
1177+
() => validateConfig({ networks: { custom: { url: "" } } }),
1178+
ERRORS.GENERAL.INVALID_CONFIG
1179+
);
1180+
1181+
// Empty string with at least 1 whitespace
1182+
expectHardhatError(
1183+
() => validateConfig({ networks: { custom: { url: " " } } }),
1184+
ERRORS.GENERAL.INVALID_CONFIG
1185+
);
1186+
});
1187+
11741188
it("Shouldn't fail if no url is set for localhost network", function () {
11751189
const errors = getValidationErrors({ networks: { localhost: {} } });
11761190
assert.isEmpty(errors);

0 commit comments

Comments
 (0)