|
1 |
| -import type { |
2 |
| - ParameterType, |
3 |
| - ParameterTypeToValueType, |
4 |
| -} from "../types/common.js"; |
| 1 | +import type { ParameterTypeToValueType } from "../types/common.js"; |
5 | 2 | import type {
|
6 | 3 | GlobalArguments,
|
7 | 4 | GlobalParameter,
|
8 | 5 | GlobalParameterMap,
|
9 | 6 | } from "../types/global-parameters.js";
|
10 | 7 | import type { HardhatPlugin } from "../types/plugins.js";
|
11 | 8 |
|
| 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 | + |
12 | 19 | /**
|
13 | 20 | * Builds a map of the global parameters, validating them.
|
14 | 21 | *
|
@@ -62,15 +69,36 @@ export function buildGlobalParameterDefinition<T extends ParameterType>({
|
62 | 69 | parameterType?: T;
|
63 | 70 | defaultValue: ParameterTypeToValueType<T>;
|
64 | 71 | }): 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 | + } |
68 | 96 |
|
69 | 97 | 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, |
74 | 102 | };
|
75 | 103 | }
|
76 | 104 |
|
|
0 commit comments