Skip to content

Commit

Permalink
Add better errors and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroFish91 committed Jan 26, 2024
1 parent 790fbbe commit 81fa32b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 6 additions & 1 deletion utils/src/utils/validationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ export namespace validationUtils {
}

export function hasValidCharLength(value: string, rc?: RangeConstraints): boolean {
const lowerLimitIncl = (!rc?.lowerLimitIncl || rc.lowerLimitIncl < 1) ? 1 : rc.lowerLimitIncl;
const lowerLimitIncl = (!rc?.lowerLimitIncl) ? 1 : rc.lowerLimitIncl;
const upperLimitIncl = (!rc?.upperLimitIncl || rc.upperLimitIncl > Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : rc.upperLimitIncl;

if (lowerLimitIncl < 1 || upperLimitIncl < 1) {
throw new Error(vscode.l10n.t('Specified character lengths should be 1 character or greater.'));
}

if (lowerLimitIncl > upperLimitIncl) {
throw new Error(vscode.l10n.t('The minimum specified character length should not exceed the maximum specified character length.'));
}
Expand Down
10 changes: 9 additions & 1 deletion utils/test/validationUtils/hasValidCharLength.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,13 @@ export function hasValidCharLengthTest() {
assert.equal(validationUtils.hasValidCharLength(value, constraints), false);
}

assert.throws(() => validationUtils.hasValidCharLength('abcdefg', { lowerLimitIncl: 5, upperLimitIncl: -5 }));
const errorValues: CharLengthParams[] = [
{ value: 'abcd', constraints: { lowerLimitIncl: -1, upperLimitIncl: 4 } },
{ value: 'abcd', constraints: { lowerLimitIncl: 1, upperLimitIncl: -4 } },
{ value: 'abcd', constraints: { lowerLimitIncl: 5, upperLimitIncl: 4 } },
];

for (const { value, constraints } of errorValues) {
assert.throws(() => validationUtils.hasValidCharLength(value, constraints));
}
}

0 comments on commit 81fa32b

Please sign in to comment.