Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Fix isHexString validation #8556

Merged
merged 5 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion elements/lisk-validator/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const isHexString = (data: unknown): boolean => {
return false;
}

return data === '' || /^[a-f0-9]+$/i.test(data);
return data === '' || /^([0-9a-f]{2})+$/i.test(data);
};

export const isEncryptedPassphrase = (data: string): boolean => {
Expand Down
15 changes: 13 additions & 2 deletions elements/lisk-validator/test/validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ describe('validation', () => {

describe('#isHexString', () => {
it('should return true when valid hex was provided', () => {
return expect(
isHexString('215b667a32a5cd51a94c9c2046c11fffb08c65748febec099451e3b164452bc'),
expect(
isHexString('215b667a32a5cd51a94c9c2046c11fffb08c65748febec099451e3b164452b'),
bobanm marked this conversation as resolved.
Show resolved Hide resolved
).toBeTrue();
expect(
isHexString('215B667A32a5cd51A94c9c2046c11fffb08c65748febec099451e3b164452B'),
).toBeTrue();
});

Expand All @@ -92,6 +95,14 @@ describe('validation', () => {
isHexString('zzzzzzza32a5cd51a94c9c2046c11fffb08c65748febec099451e3b164452bc'),
).toBeFalse();
});

it('should return true when input is empty', () => {
return expect(isHexString('')).toBeTrue();
});

it('should return false when input length is odd', () => {
return expect(isHexString('ff1')).toBeFalse();
});
});

describe('#isEncryptedPassphrase', () => {
Expand Down