Skip to content

Commit

Permalink
refactor: use validateCredsList method for PDSet.AcceptedCreds field
Browse files Browse the repository at this point in the history
  • Loading branch information
ckeshava committed Jan 15, 2025
1 parent ce9193c commit cf772b9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 44 deletions.
6 changes: 4 additions & 2 deletions packages/xrpl/src/models/transactions/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,13 +457,15 @@ export function validateCredentialType(tx: Record<string, unknown>): void {
* @param credentials An array of credential IDs to check for errors
* @param transactionType The transaction type to include in error messages
* @param isStringID Toggle for if array contains IDs instead of AuthorizeCredential objects
* @param maxLengthCredentialsArray
* @throws Validation Error if the formatting is incorrect
*/
// eslint-disable-next-line max-lines-per-function -- separating logic further will add unnecessary complexity
export function validateCredentialsList(
credentials: unknown,
transactionType: string,
isStringID: boolean,
maxLengthCredentialsArray: number = MAX_CREDENTIALS_LIST_LENGTH,
): void {
if (credentials == null) {
return
Expand All @@ -473,9 +475,9 @@ export function validateCredentialsList(
`${transactionType}: Credentials must be an array`,
)
}
if (credentials.length > MAX_CREDENTIALS_LIST_LENGTH) {
if (credentials.length > maxLengthCredentialsArray) {
throw new ValidationError(
`${transactionType}: Credentials length cannot exceed ${MAX_CREDENTIALS_LIST_LENGTH} elements`,
`${transactionType}: Credentials length cannot exceed ${maxLengthCredentialsArray} elements`,
)
} else if (credentials.length === 0) {
throw new ValidationError(
Expand Down
51 changes: 14 additions & 37 deletions packages/xrpl/src/models/transactions/permissionedDomainSet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ValidationError } from '../../errors'
import { isArray } from 'lodash'

import { AuthorizeCredential } from '../common'

import {
Expand All @@ -7,8 +8,7 @@ import {
validateBaseTransaction,
validateOptionalField,
validateRequiredField,
isAuthorizeCredential,
containsDuplicates,
validateCredentialsList,
} from './common'

const ACCEPTED_CREDENTIALS_MAX_LENGTH = 10
Expand All @@ -32,38 +32,15 @@ export function validatePermissionedDomainSet(
validateBaseTransaction(tx)

validateOptionalField(tx, 'DomainID', isString)

validateRequiredField(tx, 'AcceptedCredentials', (value) => {
if (!Array.isArray(value)) {
throw new ValidationError(
'PermissionedDomainSet: AcceptedCredentials must be an array',
)
}

if (value.length > ACCEPTED_CREDENTIALS_MAX_LENGTH) {
throw new ValidationError(
`PermissionedDomainSet: AcceptedCredentials must have at most ${ACCEPTED_CREDENTIALS_MAX_LENGTH} Credential objects`,
)
} else if (value.length === 0) {
throw new ValidationError(
`PermissionedDomainSet: AcceptedCredentials must have at least one Credential object`,
)
}

value.forEach((credential) => {
if (!isAuthorizeCredential(credential)) {
throw new ValidationError(
'PermissionedDomainSet: Invalid AcceptedCredentials format',
)
}
})

if (containsDuplicates(value)) {
throw new ValidationError(
`PermissionedDomainSet: AcceptedCredentials cannot contain duplicate elements`,
)
}

return true
})
validateRequiredField(tx, 'AcceptedCredentials', isArray)

validateCredentialsList(
tx.AcceptedCredentials,
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- known from base check
tx.TransactionType as string,
// PermissionedDomainSet uses AuthorizeCredential nested objects only, strings are not allowed
false,
// PermissionedDomainSet uses at most 10 accepted credentials. This is different from Credential-feature transactions.
ACCEPTED_CREDENTIALS_MAX_LENGTH,
)
}
10 changes: 5 additions & 5 deletions packages/xrpl/test/models/permissionedDomainSet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('PermissionedDomainSet', function () {
assert.throws(
() => validatePermissionedDomainSet(tx),
ValidationError,
'PermissionedDomainSet: AcceptedCredentials must have at most 10 Credential objects',
'PermissionedDomainSet: Credentials length cannot exceed 10 elements',
)
})

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

Expand All @@ -80,7 +80,7 @@ describe('PermissionedDomainSet', function () {
assert.throws(
() => validatePermissionedDomainSet(tx),
ValidationError,
'PermissionedDomainSet: AcceptedCredentials must be an array',
'PermissionedDomainSet: invalid field AcceptedCredentials',
)
})

Expand All @@ -89,7 +89,7 @@ describe('PermissionedDomainSet', function () {
assert.throws(
() => validatePermissionedDomainSet(tx),
ValidationError,
'PermissionedDomainSet: AcceptedCredentials cannot contain duplicate elements',
'PermissionedDomainSet: Credentials cannot contain duplicate elements',
)
})

Expand All @@ -98,7 +98,7 @@ describe('PermissionedDomainSet', function () {
assert.throws(
() => validatePermissionedDomainSet(tx),
ValidationError,
'PermissionedDomainSet: Invalid AcceptedCredentials format',
'PermissionedDomainSet: Invalid Credentials format',
)
})
})

0 comments on commit cf772b9

Please sign in to comment.