Skip to content

Commit 53f710a

Browse files
Fix issue XRPLF#2858: Improve error messages in field validation functions
1 parent effe757 commit 53f710a

File tree

1 file changed

+18
-16
lines changed
  • packages/xrpl/src/models/transactions

1 file changed

+18
-16
lines changed

packages/xrpl/src/models/transactions/common.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,20 @@ export function isXChainBridge(input: unknown): input is XChainBridge {
210210
)
211211
}
212212

213-
/* eslint-disable @typescript-eslint/restrict-template-expressions -- tx.TransactionType is checked before any calls */
214-
215213
/**
216214
* Verify the form and type of a required type for a transaction at runtime.
217215
*
218216
* @param tx - The transaction input to check the form and type of.
219217
* @param paramName - The name of the transaction parameter.
220218
* @param checkValidity - The function to use to check the type.
219+
* @param expectedType - Optional. The expected type for more specific error messages.
221220
* @throws
222221
*/
223222
export function validateRequiredField(
224223
tx: Record<string, unknown>,
225224
paramName: string,
226225
checkValidity: (inp: unknown) => boolean,
226+
expectedType?: string,
227227
): void {
228228
if (tx[paramName] == null) {
229229
throw new ValidationError(
@@ -232,8 +232,9 @@ export function validateRequiredField(
232232
}
233233

234234
if (!checkValidity(tx[paramName])) {
235+
const actualType = tx[paramName] === null ? 'null' : typeof tx[paramName]
235236
throw new ValidationError(
236-
`${tx.TransactionType}: invalid field ${paramName}`,
237+
`${tx.TransactionType}: invalid field ${paramName}${expectedType ? `: expected ${expectedType}, received ${actualType}` : ''}`,
237238
)
238239
}
239240
}
@@ -244,22 +245,23 @@ export function validateRequiredField(
244245
* @param tx - The transaction input to check the form and type of.
245246
* @param paramName - The name of the transaction parameter.
246247
* @param checkValidity - The function to use to check the type.
248+
* @param expectedType - Optional. The expected type for more specific error messages.
247249
* @throws
248250
*/
249251
export function validateOptionalField(
250252
tx: Record<string, unknown>,
251253
paramName: string,
252254
checkValidity: (inp: unknown) => boolean,
255+
expectedType?: string,
253256
): void {
254257
if (tx[paramName] !== undefined && !checkValidity(tx[paramName])) {
258+
const actualType = tx[paramName] === null ? 'null' : typeof tx[paramName]
255259
throw new ValidationError(
256-
`${tx.TransactionType}: invalid field ${paramName}`,
260+
`${tx.TransactionType}: invalid field ${paramName}${expectedType ? `: expected ${expectedType}, received ${actualType}` : ''}`,
257261
)
258262
}
259263
}
260264

261-
/* eslint-enable @typescript-eslint/restrict-template-expressions -- checked before */
262-
263265
// eslint-disable-next-line @typescript-eslint/no-empty-interface -- no global flags right now, so this is fine
264266
export interface GlobalFlags {}
265267

@@ -360,15 +362,15 @@ export function validateBaseTransaction(common: Record<string, unknown>): void {
360362
throw new ValidationError('BaseTransaction: Unknown TransactionType')
361363
}
362364

363-
validateRequiredField(common, 'Account', isString)
365+
validateRequiredField(common, 'Account', isString, 'string')
364366

365-
validateOptionalField(common, 'Fee', isString)
367+
validateOptionalField(common, 'Fee', isString, 'string')
366368

367-
validateOptionalField(common, 'Sequence', isNumber)
369+
validateOptionalField(common, 'Sequence', isNumber, 'number')
368370

369-
validateOptionalField(common, 'AccountTxnID', isString)
371+
validateOptionalField(common, 'AccountTxnID', isString, 'string')
370372

371-
validateOptionalField(common, 'LastLedgerSequence', isNumber)
373+
validateOptionalField(common, 'LastLedgerSequence', isNumber, 'number')
372374

373375
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Only used by JS
374376
const memos = common.Memos as Array<{ Memo?: unknown }> | undefined
@@ -386,15 +388,15 @@ export function validateBaseTransaction(common: Record<string, unknown>): void {
386388
throw new ValidationError('BaseTransaction: invalid Signers')
387389
}
388390

389-
validateOptionalField(common, 'SourceTag', isNumber)
391+
validateOptionalField(common, 'SourceTag', isNumber, 'number')
390392

391-
validateOptionalField(common, 'SigningPubKey', isString)
393+
validateOptionalField(common, 'SigningPubKey', isString, 'string')
392394

393-
validateOptionalField(common, 'TicketSequence', isNumber)
395+
validateOptionalField(common, 'TicketSequence', isNumber, 'number')
394396

395-
validateOptionalField(common, 'TxnSignature', isString)
397+
validateOptionalField(common, 'TxnSignature', isString, 'string')
396398

397-
validateOptionalField(common, 'NetworkID', isNumber)
399+
validateOptionalField(common, 'NetworkID', isNumber, 'number')
398400
}
399401

400402
/**

0 commit comments

Comments
 (0)