Skip to content

Commit 7fe81be

Browse files
fix errors related to typeChecking and verification functions usage
1 parent 8c6f499 commit 7fe81be

File tree

4 files changed

+36
-46
lines changed

4 files changed

+36
-46
lines changed

packages/cactus-plugin-satp-hermes/src/main/typescript/services/utils.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ export function getEnumValueByKey<T extends object>(
1515

1616
export interface chainConfigElement<T> {
1717
configElement: string;
18-
configElementType?: T;
19-
configElementTypeguard?: (value: unknown, log: Logger) => boolean;
18+
configElementType?: T | string;
19+
configElementTypeguard?:
20+
| ((value: unknown, log: Logger) => boolean)
21+
| ((value: unknown) => boolean);
2022
configSubElementType?: T;
21-
configSubElementFunctionTypeguard?: (value: unknown, log: Logger) => boolean;
23+
configSubElementFunctionTypeguard?:
24+
| ((value: unknown, log: Logger) => boolean)
25+
| ((value: unknown) => boolean);
2226
}
2327

2428
export function identifyAndCheckConfigFormat<T>(
@@ -81,9 +85,10 @@ export function checkConfigElementFormat<T>(
8185
);
8286
return false;
8387
}
88+
8489
if (
8590
ccElement.configElementType &&
86-
typeof obj[ccElement.configElement] !== typeof ccElement.configElementType
91+
typeof obj[ccElement.configElement] !== ccElement.configElementType
8792
) {
8893
log.error(
8994
`${fnTag}: ${ccElement.configElement} present but not of type ${ccElement.configElementType}`,
@@ -98,19 +103,7 @@ export function checkConfigElementFormat<T>(
98103
);
99104
return false;
100105
} else {
101-
if (
102-
ccElement.configElementType === Array &&
103-
!ccElement.configSubElementType &&
104-
!ccElement.configSubElementFunctionTypeguard
105-
) {
106-
log.error(
107-
`${fnTag}: ${ccElement.configElement} is an array but no type check is provided for its elements`,
108-
);
109-
return false;
110-
} else if (
111-
ccElement.configElementType === Array &&
112-
ccElement.configSubElementType
113-
) {
106+
if (ccElement.configSubElementType) {
114107
obj[ccElement.configElement].forEach((subEl: unknown) => {
115108
if (
116109
typeof subEl !== typeof ccElement.configSubElementType ||
@@ -122,10 +115,7 @@ export function checkConfigElementFormat<T>(
122115
return false;
123116
}
124117
});
125-
} else if (
126-
ccElement.configElementType === Array &&
127-
ccElement.configSubElementFunctionTypeguard
128-
) {
118+
} else if (ccElement.configSubElementFunctionTypeguard) {
129119
obj[ccElement.configElement].forEach((subEl: unknown) => {
130120
if (
131121
!ccElement.configSubElementFunctionTypeguard!(subEl, log) ||

packages/cactus-plugin-satp-hermes/src/main/typescript/services/validation/config-validating-functions/bridges-config-validating-functions/validate-besu-config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,28 +137,28 @@ export function isBesuConfigJSON(
137137
const optionalConfigElements: chainConfigElement<unknown>[] = [
138138
{
139139
configElement: "leafId",
140-
configElementType: String,
140+
configElementType: "string",
141141
},
142142
{
143143
configElement: "keyPair",
144-
configElementType: Object,
144+
configElementType: "object",
145145
},
146146
{
147147
configElement: "claimFormats",
148-
configElementType: Array,
148+
configElementTypeguard: Array.isArray,
149149
configSubElementFunctionTypeguard: isClaimFormat,
150150
},
151151
{
152152
configElement: "wrapperContractName",
153-
configElementType: String,
153+
configElementType: "string",
154154
},
155155
{
156156
configElement: "wrapperContractAddress",
157-
configElementType: String,
157+
configElementType: "string",
158158
},
159159
{
160160
configElement: "gas",
161-
configElementType: Number,
161+
configElementType: "number",
162162
},
163163
];
164164

packages/cactus-plugin-satp-hermes/src/main/typescript/services/validation/config-validating-functions/bridges-config-validating-functions/validate-ethereum-config.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,27 +181,27 @@ export function isEthereumConfigJSON(
181181
const configOptionalFields: chainConfigElement<unknown>[] = [
182182
{
183183
configElement: "wrapperContractName",
184-
configElementType: String,
184+
configElementType: "string",
185185
},
186186
{
187187
configElement: "wrapperContractAddress",
188-
configElementType: String,
188+
configElementType: "string",
189189
},
190190
{
191191
configElement: "gasConfig",
192192
configElementTypeguard: isGasConfig,
193193
},
194194
{
195195
configElement: "leafId",
196-
configElementType: String,
196+
configElementType: "string",
197197
},
198198
{
199199
configElement: "keyPair",
200-
configElementType: Object,
200+
configElementType: "object",
201201
},
202202
{
203203
configElement: "claimFormats",
204-
configElementType: Array,
204+
configElementTypeguard: Array.isArray,
205205
configSubElementFunctionTypeguard: isClaimFormat,
206206
},
207207
];

packages/cactus-plugin-satp-hermes/src/main/typescript/services/validation/config-validating-functions/bridges-config-validating-functions/validate-fabric-config.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export function isFabricConfigJSON(
184184
const configDefaultFields: chainConfigElement<unknown>[] = [
185185
{
186186
configElement: "channelName",
187-
configElementType: String,
187+
configElementType: "string",
188188
},
189189
{
190190
configElement: "userIdentity",
@@ -200,59 +200,59 @@ export function isFabricConfigJSON(
200200
},
201201
{
202202
configElement: "caFilePath",
203-
configElementType: String,
203+
configElementType: "string",
204204
},
205205
{
206206
configElement: "coreYamlFilePath",
207-
configElementType: String,
207+
configElementType: "string",
208208
},
209209
];
210210

211211
const configOptionalFields: chainConfigElement<unknown>[] = [
212212
{
213213
configElement: "targetOrganizations",
214-
configElementType: Array,
215-
configSubElementType: Object,
214+
configElementTypeguard: Array.isArray,
215+
configSubElementType: "object",
216216
},
217217
{
218218
configElement: "ccSequence",
219-
configElementType: Number,
219+
configElementType: "number",
220220
},
221221
{
222222
configElement: "orderer",
223-
configElementType: String,
223+
configElementType: "string",
224224
},
225225
{
226226
configElement: "ordererTLSHostnameOverride",
227-
configElementType: String,
227+
configElementType: "string",
228228
},
229229
{
230230
configElement: "connTimeout",
231-
configElementType: Number,
231+
configElementType: "number",
232232
},
233233
{
234234
configElement: "signaturePolicy",
235-
configElementType: String,
235+
configElementType: "string",
236236
},
237237
{
238238
configElement: "mspId",
239-
configElementType: String,
239+
configElementType: "string",
240240
},
241241
{
242242
configElement: "wrapperContractName",
243-
configElementType: String,
243+
configElementType: "string",
244244
},
245245
{
246246
configElement: "leafId",
247-
configElementType: String,
247+
configElementType: "string",
248248
},
249249
{
250250
configElement: "keyPair",
251251
configElementTypeguard: isKeyPairJSON,
252252
},
253253
{
254254
configElement: "claimFormats",
255-
configElementType: Array,
255+
configElementTypeguard: Array.isArray,
256256
configSubElementFunctionTypeguard: isClaimFormat,
257257
},
258258
];

0 commit comments

Comments
 (0)