Skip to content

Commit a47af52

Browse files
author
Victor Wiebe
committed
fix: comments from review
1 parent eca864d commit a47af52

5 files changed

Lines changed: 72 additions & 20 deletions

File tree

src/procedures/AssignStoRole.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
StoRole,
99
} from '../types';
1010
import { PolymathError } from '../PolymathError';
11-
import { isValidBytes32CompliantString } from '~/utils';
11+
import { checkStringLength } from '../utils';
1212

1313
export class AssignStoRole extends Procedure<AssignStoRoleProcedureArgs> {
1414
public type = ProcedureType.AssignStoRole;
@@ -67,7 +67,7 @@ export class AssignStoRole extends Procedure<AssignStoRoleProcedureArgs> {
6767
});
6868
}
6969
} else {
70-
isValidBytes32CompliantString(description, 'description');
70+
checkStringLength(description, 'description');
7171
// Delegate not found. Add them here
7272
await this.addTransaction(permissionModule.addDelegate, {
7373
tag: PolyTransactionTag.ChangePermission,

src/procedures/ModifyTieredStoData.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
Currency,
1616
} from '../types';
1717
import { PolymathError } from '../PolymathError';
18-
import { areSameAddress, isValidBytes32CompliantString } from '../utils';
18+
import { areSameAddress, checkStringLength } from '../utils';
1919
import { SecurityToken, TieredSto } from '../entities';
2020
import { TieredStoFactory } from '../entities/factories';
2121

@@ -219,7 +219,7 @@ export class ModifyTieredStoData extends Procedure<ModifyTieredStoDataProcedureA
219219
polyOracleAddress = currentPolyOracleAddress;
220220
}
221221

222-
isValidBytes32CompliantString(currencySymbol, 'denominated currency symbol');
222+
checkStringLength(currencySymbol, 'denominated currency symbol');
223223

224224
if (
225225
currencySymbol !== denominatedCurrency ||

src/procedures/SetDocument.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Procedure } from './Procedure';
22
import { ProcedureType, PolyTransactionTag, SetDocumentProcedureArgs, ErrorCode } from '../types';
33
import { PolymathError } from '../PolymathError';
4-
import { isNotEmptyValidBytes32CompliantString } from '~/utils';
4+
import { checkStringLength } from '../utils';
55

66
export class SetDocument extends Procedure<SetDocumentProcedureArgs> {
77
public type = ProcedureType.SetDocument;
@@ -32,8 +32,8 @@ export class SetDocument extends Procedure<SetDocumentProcedureArgs> {
3232
});
3333
}
3434

35-
isNotEmptyValidBytes32CompliantString(name, 'name');
36-
isNotEmptyValidBytes32CompliantString(documentHash, 'document hash');
35+
checkStringLength(name, 'name', { minLength: 1, maxLength: 32 });
36+
checkStringLength(documentHash, 'document hash', { minLength: 1, maxLength: 32 });
3737

3838
/*
3939
* Transactions

src/utils/__tests__/utils.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { serialize, unserialize } from '../index';
1+
import { checkStringLength, serialize, unserialize } from '../index';
2+
import { PolymathError } from '../../PolymathError';
3+
import { ErrorCode } from '../../types';
24

35
describe('serialize and unserialize', () => {
46
const entityType = 'someEntity';
@@ -31,3 +33,46 @@ describe('serialize and unserialize', () => {
3133
expect(unserialize(serialize(entityType, inversePojo1))).toEqual(pojo1);
3234
});
3335
});
36+
37+
describe('check string length', () => {
38+
const testVariableType = 'myVar';
39+
40+
test('checkStringLength used on a non empty string, catches error above the default 32 characters', () => {
41+
try {
42+
checkStringLength('0123456789012345678901234567890123456789', testVariableType);
43+
} catch (error) {
44+
expect(error).toEqual(
45+
new PolymathError({
46+
code: ErrorCode.ProcedureValidationError,
47+
message: `You must provide a valid ${testVariableType} up to 32 characters long`,
48+
})
49+
);
50+
}
51+
});
52+
53+
test('checkStringLength used on an empty string, catches error due to not meeting custom requirement of 1 - 32 characters', () => {
54+
try {
55+
checkStringLength('', testVariableType, { minLength: 1, maxLength: 32 });
56+
} catch (error) {
57+
expect(error).toEqual(
58+
new PolymathError({
59+
code: ErrorCode.ProcedureValidationError,
60+
message: `You must provide a valid ${testVariableType} between 1 and 32 characters long`,
61+
})
62+
);
63+
}
64+
});
65+
66+
test('checkStringLength used on a 6 character string, catches error due to not meeting custom requirement of 1 - 5 characters', () => {
67+
try {
68+
checkStringLength('0123456', testVariableType, { minLength: 1, maxLength: 5 });
69+
} catch (error) {
70+
expect(error).toEqual(
71+
new PolymathError({
72+
code: ErrorCode.ProcedureValidationError,
73+
message: `You must provide a valid ${testVariableType} between 1 and 5 characters long`,
74+
})
75+
);
76+
}
77+
});
78+
});

src/utils/index.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,27 @@ export function areSameAddress(a: string, b: string) {
5050
return a.toUpperCase() === b.toUpperCase();
5151
}
5252

53-
export function isValidBytes32CompliantString(value: string, variableName: string) {
54-
if (value.length > 32) {
53+
/**
54+
* Check the length of a given string to ensure it meets correct bounds
55+
* @param value - the string itself
56+
* @param variableName - the name of the variable associated to the string itself
57+
* @param opts - optional min and max length of the string. Defaults to a minimum of 0 (empty string) and a maximum of 32 characters
58+
*/
59+
export function checkStringLength(
60+
value: string,
61+
variableName: string,
62+
opts?: { minLength: number | undefined; maxLength: number }
63+
) {
64+
const minLength = opts && opts.minLength != undefined ? opts.minLength : 0;
65+
const maxLength = opts ? opts.maxLength : 32;
66+
if (value.length < minLength || value.length > maxLength) {
5567
throw new PolymathError({
5668
code: ErrorCode.ProcedureValidationError,
57-
message: `You must provide a valid ${variableName} up to 32 characters long`,
58-
});
59-
}
60-
}
61-
62-
export function isNotEmptyValidBytes32CompliantString(value: string, variableName: string) {
63-
if (value.length < 1 || value.length > 32) {
64-
throw new PolymathError({
65-
code: ErrorCode.ProcedureValidationError,
66-
message: `You must provide a valid ${variableName} between 1 and 32 characters long`,
69+
message: `You must provide a valid ${variableName} ${
70+
opts && opts.minLength != undefined
71+
? `between ${minLength} and ${maxLength}`
72+
: `up to ${maxLength}`
73+
} characters long`,
6774
});
6875
}
6976
}

0 commit comments

Comments
 (0)