Skip to content

Commit

Permalink
allow '' to pass hex check
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad-Altabba committed Nov 28, 2022
1 parent 1d1a15d commit c0eff62
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/web3-validator/src/validation/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import { ValidInputTypes } from '../types';
export const isString = (value: ValidInputTypes) => typeof value === 'string';

export const isHexStrict = (hex: ValidInputTypes) =>
typeof hex === 'string' && /^((-)?0x[0-9a-f]+|(0x)?)$/i.test(hex);
typeof hex === 'string' && /^((-)?0x[0-9a-f]+|(0x))$/i.test(hex);

export const isHex = (hex: ValidInputTypes): boolean =>
typeof hex === 'number' ||
typeof hex === 'bigint' ||
(typeof hex === 'string' && /^((-0x|0x|-)?[0-9a-f]+|(0x)?)$/i.test(hex));
(typeof hex === 'string' && /^((-0x|0x|-)?[0-9a-f]+|(0x))$/i.test(hex));

export const isHexString8Bytes = (value: string, prefixed = true) =>
prefixed ? isHexStrict(value) && value.length === 18 : isHex(value) && value.length === 16;
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-validator/test/fixtures/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ export const validHexStrictData: any[] = [
...validHexStrictDataWithNumber.map(tuple => tuple[0]),
'-0xdec0518fa672a70027b04c286582e543ab17319fbdd384fa7bc8f3d5a542c0b',
'0x',
'',
];

export const invalidHexData: any[] = [
'Heeäööä👅D34ɝɣ24Єͽ',
'',
'-',
'-0x',
'x',
Expand Down
3 changes: 2 additions & 1 deletion packages/web3-validator/test/unit/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ describe('utils', () => {

it.each(validHexStrictData)('valid hex strings', input => {
expect(numberToHex(input)).toEqual(
(input === '' ? '0x' : (input as string)).toLowerCase(),
// if input is '' then numberToHex would return "0x0"
(input === '' ? '0x0' : (input as string)).toLowerCase(),
);
});

Expand Down

0 comments on commit c0eff62

Please sign in to comment.