Skip to content

Commit

Permalink
feat: add validators utilities to utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
karliatto committed Jan 20, 2022
1 parent 90814c4 commit 3340270
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Input, Button, variables } from '@trezor/components';
import { Translation } from '@suite-components/Translation';
import { Network } from '@suite/types/wallet';
import { BlockbookUrl } from '@wallet-types/blockbook';
import { isUrl } from '@suite-utils/validators';
import { isUrl } from '@trezor/utils';
import { useTranslation } from '@suite-hooks/useTranslation';
import InputError from '@wallet-components/InputError';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useForm, useController } from 'react-hook-form';
import { useTranslation, useSelector, useActions } from '@suite-hooks';
import * as protocolActions from '@suite-actions/protocolActions';
import { isHex } from '@wallet-utils/ethUtils';
import { isASCII } from '@suite-utils/validators';
import { isASCII } from '@trezor/utils';
import { isAddressValid } from '@wallet-utils/validation';
import type { Account } from '@wallet-types';
import type { AoppState } from '@suite-reducers/protocolReducer';
Expand Down
130 changes: 0 additions & 130 deletions packages/suite/src/utils/suite/__tests__/validators.test.ts

This file was deleted.

44 changes: 0 additions & 44 deletions packages/suite/src/utils/suite/validators.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/suite/src/utils/wallet/ethUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as EthereumjsUtil from 'ethereumjs-util';
import { UPPERCASE_RE } from '@trezor/utils';
import BigNumber from 'bignumber.js';

export const decimalToHex = (dec: number): string => new BigNumber(dec).toString(16);
Expand All @@ -24,14 +25,13 @@ export const strip = (str: string): string => {
};

export const validateAddress = (address: string): string | null => {
const hasUpperCase = new RegExp('^(.*[A-Z].*)$');
if (address.length < 1) {
return 'Address is not set';
}
if (!EthereumjsUtil.isValidAddress(address)) {
return 'Address is not valid';
}
if (address.match(hasUpperCase) && !EthereumjsUtil.isValidChecksumAddress(address)) {
if (address.match(UPPERCASE_RE) && !EthereumjsUtil.isValidChecksumAddress(address)) {
return 'Address is not a valid checksum';
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Network } from '@wallet-types';
import { variables, CoinLogo, Input, Button } from '@trezor/components';
import { useForm } from 'react-hook-form';
import { useTranslation } from '@suite-hooks/useTranslation';
import { isUrl } from '@suite-utils/validators';
import { isUrl } from '@trezor/utils';
import InputError from '@wallet-components/InputError';
import { BlockbookUrl } from '@wallet-types/blockbook';

Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ export * from './file';
export * from './object';
export * from './random';
export * from './string';
export * from './validators';

// Potential candidates for this package

// trezor-suite/packages/suite/src/utils/suite/parseUri.ts
// trezor-suite/packages/suite/src/utils/suite/validators.ts
// trezor-suite/packages/suite/src/utils/wallet/validation.ts
// trezor-suite/packages/suite/src/utils/wallet/promiseUtils.ts
// trezor-suite/packages/suite/src/utils/wallet/ethUtils.ts
Expand Down
13 changes: 13 additions & 0 deletions packages/utils/src/validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const URL_REGEX =
/^(http|ws)s?:\/\/[a-z0-9]([a-z0-9.-]+)?(:[0-9]{1,5})?((\/)?(([a-z0-9-_])+(\/)?)+)$/i;

export const UPPERCASE_RE = new RegExp('^(.*[A-Z].*)$');

export const isUrl = (value: string): boolean => URL_REGEX.test(value);

export function isASCII(value?: string): boolean {
if (!value) return true;
return /^[\x00-\x7F]*$/.test(value); // eslint-disable-line
}

export const hasUppercase = (value: string) => UPPERCASE_RE.test(value);
31 changes: 31 additions & 0 deletions packages/utils/tests/validators.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { isASCII, hasUppercase } from '../src/validators';

describe('utils/suite/validators', () => {
describe('isASCII', () => {
it('should return true for ASCII only string', () => {
expect(isASCII('this is only ascii')).toEqual(true);
});

it('should return true when called without parameter', () => {
expect(isASCII()).toEqual(true);
});

it('should return false strings with non ASCII chars', () => {
const fooStrings = ['¥', 'železniční přejezd'];
fooStrings.forEach(str => {
expect(isASCII(str)).toEqual(false);
});
});
});

it('hasUppercase', () => {
expect(hasUppercase('0')).toBe(false);
expect(hasUppercase('abc')).toBe(false);
expect(hasUppercase('abcD')).toBe(true);
expect(hasUppercase('Abcd')).toBe(true);
expect(hasUppercase('aBcd')).toBe(true);
expect(hasUppercase('123abc123')).toBe(false);
expect(hasUppercase('0x123abc456')).toBe(false);
expect(hasUppercase('0x123aBc456')).toBe(true);
});
});

0 comments on commit 3340270

Please sign in to comment.