Skip to content

Commit cf772b9

Browse files
committed
refactor: use validateCredsList method for PDSet.AcceptedCreds field
1 parent ce9193c commit cf772b9

File tree

3 files changed

+23
-44
lines changed

3 files changed

+23
-44
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,13 +457,15 @@ export function validateCredentialType(tx: Record<string, unknown>): void {
457457
* @param credentials An array of credential IDs to check for errors
458458
* @param transactionType The transaction type to include in error messages
459459
* @param isStringID Toggle for if array contains IDs instead of AuthorizeCredential objects
460+
* @param maxLengthCredentialsArray
460461
* @throws Validation Error if the formatting is incorrect
461462
*/
462463
// eslint-disable-next-line max-lines-per-function -- separating logic further will add unnecessary complexity
463464
export function validateCredentialsList(
464465
credentials: unknown,
465466
transactionType: string,
466467
isStringID: boolean,
468+
maxLengthCredentialsArray: number = MAX_CREDENTIALS_LIST_LENGTH,
467469
): void {
468470
if (credentials == null) {
469471
return
@@ -473,9 +475,9 @@ export function validateCredentialsList(
473475
`${transactionType}: Credentials must be an array`,
474476
)
475477
}
476-
if (credentials.length > MAX_CREDENTIALS_LIST_LENGTH) {
478+
if (credentials.length > maxLengthCredentialsArray) {
477479
throw new ValidationError(
478-
`${transactionType}: Credentials length cannot exceed ${MAX_CREDENTIALS_LIST_LENGTH} elements`,
480+
`${transactionType}: Credentials length cannot exceed ${maxLengthCredentialsArray} elements`,
479481
)
480482
} else if (credentials.length === 0) {
481483
throw new ValidationError(
Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { ValidationError } from '../../errors'
1+
import { isArray } from 'lodash'
2+
23
import { AuthorizeCredential } from '../common'
34

45
import {
@@ -7,8 +8,7 @@ import {
78
validateBaseTransaction,
89
validateOptionalField,
910
validateRequiredField,
10-
isAuthorizeCredential,
11-
containsDuplicates,
11+
validateCredentialsList,
1212
} from './common'
1313

1414
const ACCEPTED_CREDENTIALS_MAX_LENGTH = 10
@@ -32,38 +32,15 @@ export function validatePermissionedDomainSet(
3232
validateBaseTransaction(tx)
3333

3434
validateOptionalField(tx, 'DomainID', isString)
35-
36-
validateRequiredField(tx, 'AcceptedCredentials', (value) => {
37-
if (!Array.isArray(value)) {
38-
throw new ValidationError(
39-
'PermissionedDomainSet: AcceptedCredentials must be an array',
40-
)
41-
}
42-
43-
if (value.length > ACCEPTED_CREDENTIALS_MAX_LENGTH) {
44-
throw new ValidationError(
45-
`PermissionedDomainSet: AcceptedCredentials must have at most ${ACCEPTED_CREDENTIALS_MAX_LENGTH} Credential objects`,
46-
)
47-
} else if (value.length === 0) {
48-
throw new ValidationError(
49-
`PermissionedDomainSet: AcceptedCredentials must have at least one Credential object`,
50-
)
51-
}
52-
53-
value.forEach((credential) => {
54-
if (!isAuthorizeCredential(credential)) {
55-
throw new ValidationError(
56-
'PermissionedDomainSet: Invalid AcceptedCredentials format',
57-
)
58-
}
59-
})
60-
61-
if (containsDuplicates(value)) {
62-
throw new ValidationError(
63-
`PermissionedDomainSet: AcceptedCredentials cannot contain duplicate elements`,
64-
)
65-
}
66-
67-
return true
68-
})
35+
validateRequiredField(tx, 'AcceptedCredentials', isArray)
36+
37+
validateCredentialsList(
38+
tx.AcceptedCredentials,
39+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- known from base check
40+
tx.TransactionType as string,
41+
// PermissionedDomainSet uses AuthorizeCredential nested objects only, strings are not allowed
42+
false,
43+
// PermissionedDomainSet uses at most 10 accepted credentials. This is different from Credential-feature transactions.
44+
ACCEPTED_CREDENTIALS_MAX_LENGTH,
45+
)
6946
}

packages/xrpl/test/models/permissionedDomainSet.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('PermissionedDomainSet', function () {
6262
assert.throws(
6363
() => validatePermissionedDomainSet(tx),
6464
ValidationError,
65-
'PermissionedDomainSet: AcceptedCredentials must have at most 10 Credential objects',
65+
'PermissionedDomainSet: Credentials length cannot exceed 10 elements',
6666
)
6767
})
6868

@@ -71,7 +71,7 @@ describe('PermissionedDomainSet', function () {
7171
assert.throws(
7272
() => validatePermissionedDomainSet(tx),
7373
ValidationError,
74-
'PermissionedDomainSet: AcceptedCredentials must have at least one Credential object',
74+
'PermissionedDomainSet: Credentials cannot be an empty array',
7575
)
7676
})
7777

@@ -80,7 +80,7 @@ describe('PermissionedDomainSet', function () {
8080
assert.throws(
8181
() => validatePermissionedDomainSet(tx),
8282
ValidationError,
83-
'PermissionedDomainSet: AcceptedCredentials must be an array',
83+
'PermissionedDomainSet: invalid field AcceptedCredentials',
8484
)
8585
})
8686

@@ -89,7 +89,7 @@ describe('PermissionedDomainSet', function () {
8989
assert.throws(
9090
() => validatePermissionedDomainSet(tx),
9191
ValidationError,
92-
'PermissionedDomainSet: AcceptedCredentials cannot contain duplicate elements',
92+
'PermissionedDomainSet: Credentials cannot contain duplicate elements',
9393
)
9494
})
9595

@@ -98,7 +98,7 @@ describe('PermissionedDomainSet', function () {
9898
assert.throws(
9999
() => validatePermissionedDomainSet(tx),
100100
ValidationError,
101-
'PermissionedDomainSet: Invalid AcceptedCredentials format',
101+
'PermissionedDomainSet: Invalid Credentials format',
102102
)
103103
})
104104
})

0 commit comments

Comments
 (0)