Skip to content

Commit 15ec3c7

Browse files
committed
Add validation to buildGlobalParameterDefinition
1 parent 2eb7f44 commit 15ec3c7

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

v-next/core/src/internal/global-parameters.ts

+39-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
import type {
2-
ParameterType,
3-
ParameterTypeToValueType,
4-
} from "../types/common.js";
1+
import type { ParameterTypeToValueType } from "../types/common.js";
52
import type {
63
GlobalArguments,
74
GlobalParameter,
85
GlobalParameterMap,
96
} from "../types/global-parameters.js";
107
import type { HardhatPlugin } from "../types/plugins.js";
118

9+
import { HardhatError } from "@nomicfoundation/hardhat-errors";
10+
11+
import { ParameterType } from "../types/common.js";
12+
13+
import {
14+
RESERVED_PARAMETER_NAMES,
15+
isParameterValueValid,
16+
isValidParamNameCasing,
17+
} from "./parameters.js";
18+
1219
/**
1320
* Builds a map of the global parameters, validating them.
1421
*
@@ -62,15 +69,36 @@ export function buildGlobalParameterDefinition<T extends ParameterType>({
6269
parameterType?: T;
6370
defaultValue: ParameterTypeToValueType<T>;
6471
}): GlobalParameter {
65-
// TODO: Validate name casing
66-
// TODO: Validate default value matches with type
67-
// TODO: Validate that the name is not one of the reserved ones in parameters.ts
72+
const type = parameterType ?? ParameterType.STRING;
73+
74+
if (!isValidParamNameCasing(name)) {
75+
throw new HardhatError(HardhatError.ERRORS.ARGUMENTS.INVALID_NAME, {
76+
name,
77+
});
78+
}
79+
80+
if (RESERVED_PARAMETER_NAMES.has(name)) {
81+
throw new HardhatError(HardhatError.ERRORS.ARGUMENTS.RESERVED_NAME, {
82+
name,
83+
});
84+
}
85+
86+
if (!isParameterValueValid(type, defaultValue)) {
87+
throw new HardhatError(
88+
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE,
89+
{
90+
value: defaultValue,
91+
name: "defaultValue",
92+
type: parameterType,
93+
},
94+
);
95+
}
6896

6997
return {
70-
name: options.name,
71-
description: options.description,
72-
parameterType: options.parameterType,
73-
defaultValue: options.defaultValue,
98+
name,
99+
description,
100+
parameterType: type,
101+
defaultValue,
74102
};
75103
}
76104

0 commit comments

Comments
 (0)