@@ -210,20 +210,20 @@ export function isXChainBridge(input: unknown): input is XChainBridge {
210
210
)
211
211
}
212
212
213
- /* eslint-disable @typescript-eslint/restrict-template-expressions -- tx.TransactionType is checked before any calls */
214
-
215
213
/**
216
214
* Verify the form and type of a required type for a transaction at runtime.
217
215
*
218
216
* @param tx - The transaction input to check the form and type of.
219
217
* @param paramName - The name of the transaction parameter.
220
218
* @param checkValidity - The function to use to check the type.
219
+ * @param expectedType - Optional. The expected type for more specific error messages.
221
220
* @throws
222
221
*/
223
222
export function validateRequiredField (
224
223
tx : Record < string , unknown > ,
225
224
paramName : string ,
226
225
checkValidity : ( inp : unknown ) => boolean ,
226
+ expectedType ?: string ,
227
227
) : void {
228
228
if ( tx [ paramName ] == null ) {
229
229
throw new ValidationError (
@@ -232,8 +232,9 @@ export function validateRequiredField(
232
232
}
233
233
234
234
if ( ! checkValidity ( tx [ paramName ] ) ) {
235
+ const actualType = tx [ paramName ] === null ? 'null' : typeof tx [ paramName ]
235
236
throw new ValidationError (
236
- `${ tx . TransactionType } : invalid field ${ paramName } ` ,
237
+ `${ tx . TransactionType } : invalid field ${ paramName } ${ expectedType ? `: expected ${ expectedType } , received ${ actualType } ` : '' } ` ,
237
238
)
238
239
}
239
240
}
@@ -244,22 +245,23 @@ export function validateRequiredField(
244
245
* @param tx - The transaction input to check the form and type of.
245
246
* @param paramName - The name of the transaction parameter.
246
247
* @param checkValidity - The function to use to check the type.
248
+ * @param expectedType - Optional. The expected type for more specific error messages.
247
249
* @throws
248
250
*/
249
251
export function validateOptionalField (
250
252
tx : Record < string , unknown > ,
251
253
paramName : string ,
252
254
checkValidity : ( inp : unknown ) => boolean ,
255
+ expectedType ?: string ,
253
256
) : void {
254
257
if ( tx [ paramName ] !== undefined && ! checkValidity ( tx [ paramName ] ) ) {
258
+ const actualType = tx [ paramName ] === null ? 'null' : typeof tx [ paramName ]
255
259
throw new ValidationError (
256
- `${ tx . TransactionType } : invalid field ${ paramName } ` ,
260
+ `${ tx . TransactionType } : invalid field ${ paramName } ${ expectedType ? `: expected ${ expectedType } , received ${ actualType } ` : '' } ` ,
257
261
)
258
262
}
259
263
}
260
264
261
- /* eslint-enable @typescript-eslint/restrict-template-expressions -- checked before */
262
-
263
265
// eslint-disable-next-line @typescript-eslint/no-empty-interface -- no global flags right now, so this is fine
264
266
export interface GlobalFlags { }
265
267
@@ -360,15 +362,15 @@ export function validateBaseTransaction(common: Record<string, unknown>): void {
360
362
throw new ValidationError ( 'BaseTransaction: Unknown TransactionType' )
361
363
}
362
364
363
- validateRequiredField ( common , 'Account' , isString )
365
+ validateRequiredField ( common , 'Account' , isString , 'string' )
364
366
365
- validateOptionalField ( common , 'Fee' , isString )
367
+ validateOptionalField ( common , 'Fee' , isString , 'string' )
366
368
367
- validateOptionalField ( common , 'Sequence' , isNumber )
369
+ validateOptionalField ( common , 'Sequence' , isNumber , 'number' )
368
370
369
- validateOptionalField ( common , 'AccountTxnID' , isString )
371
+ validateOptionalField ( common , 'AccountTxnID' , isString , 'string' )
370
372
371
- validateOptionalField ( common , 'LastLedgerSequence' , isNumber )
373
+ validateOptionalField ( common , 'LastLedgerSequence' , isNumber , 'number' )
372
374
373
375
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Only used by JS
374
376
const memos = common . Memos as Array < { Memo ?: unknown } > | undefined
@@ -386,15 +388,15 @@ export function validateBaseTransaction(common: Record<string, unknown>): void {
386
388
throw new ValidationError ( 'BaseTransaction: invalid Signers' )
387
389
}
388
390
389
- validateOptionalField ( common , 'SourceTag' , isNumber )
391
+ validateOptionalField ( common , 'SourceTag' , isNumber , 'number' )
390
392
391
- validateOptionalField ( common , 'SigningPubKey' , isString )
393
+ validateOptionalField ( common , 'SigningPubKey' , isString , 'string' )
392
394
393
- validateOptionalField ( common , 'TicketSequence' , isNumber )
395
+ validateOptionalField ( common , 'TicketSequence' , isNumber , 'number' )
394
396
395
- validateOptionalField ( common , 'TxnSignature' , isString )
397
+ validateOptionalField ( common , 'TxnSignature' , isString , 'string' )
396
398
397
- validateOptionalField ( common , 'NetworkID' , isNumber )
399
+ validateOptionalField ( common , 'NetworkID' , isNumber , 'number' )
398
400
}
399
401
400
402
/**
0 commit comments