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 28, 2022
1 parent c92c4a7 commit b8c4550
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 195 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { useActions, useSelector, useTranslation } from '@suite-hooks';
import { isUrl } from '@suite-utils/validators';
import { isUrl } from '@trezor/utils';
import { setBackend as setBackendAction } from '@settings-actions/walletSettingsActions';
import type { Network } from '@wallet-types';
import type { BackendType } from '@wallet-reducers/settingsReducer';
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 Expand Up @@ -114,7 +114,7 @@ export const useSignVerifyForm = (page: 'sign' | 'verify', account?: Account) =>
? translationString('DATA_NOT_VALID_HEX')
: undefined,
ascii: (message: string) =>
!formValues.hex && !isASCII(message)
!formValues.hex && !isAscii(message)
? translationString('TR_ASCII_ONLY')
: undefined,
},
Expand Down
12 changes: 6 additions & 6 deletions packages/suite/src/reducers/suite/resizeReducer.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import produce from 'immer';
import * as variables from '@trezor/components/lib/config/variables'; // can't import from index cause it would import all UI components
import { RESIZE } from '@suite-actions/constants';
import { getNumberFromPxString } from '@trezor/utils';
import { getNumberFromPixelString } from '@trezor/utils';
import { Action } from '@suite-types';

const sizes = {
UNAVAILABLE: getNumberFromPxString(variables.SCREEN_SIZE.UNAVAILABLE),
SMALL: getNumberFromPxString(variables.SCREEN_SIZE.SM),
MEDIUM: getNumberFromPxString(variables.SCREEN_SIZE.MD),
LARGE: getNumberFromPxString(variables.SCREEN_SIZE.LG),
XLARGE: getNumberFromPxString(variables.SCREEN_SIZE.XL),
UNAVAILABLE: getNumberFromPixelString(variables.SCREEN_SIZE.UNAVAILABLE),
SMALL: getNumberFromPixelString(variables.SCREEN_SIZE.SM),
MEDIUM: getNumberFromPixelString(variables.SCREEN_SIZE.MD),
LARGE: getNumberFromPixelString(variables.SCREEN_SIZE.LG),
XLARGE: getNumberFromPixelString(variables.SCREEN_SIZE.XL),
};

const getSize = (screenWidth: number | null): State['size'] => {
Expand Down
5 changes: 3 additions & 2 deletions packages/suite/src/reducers/suite/suiteReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Action, TrezorDevice, Lock, SuiteThemeColors } from '@suite-types';
import type { Locale } from '@suite-config/languages';
import { isWeb, getWindowWidth } from '@suite-utils/env';
import { ensureLocale } from '@suite-utils/l10n';
import { getNumberFromPxString } from '@trezor/utils';
import { getNumberFromPixelString } from '@trezor/utils';

export interface DebugModeOptions {
invityAPIUrl?: string;
Expand Down Expand Up @@ -79,7 +79,8 @@ const initialState: SuiteState = {
taprootBannerClosed: false,
securityStepsHidden: false,
dashboardGraphHidden: false,
dashboardAssetsGridMode: getWindowWidth() < getNumberFromPxString(variables.SCREEN_SIZE.SM),
dashboardAssetsGridMode:
getWindowWidth() < getNumberFromPixelString(variables.SCREEN_SIZE.SM),
},
settings: {
theme: {
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 { hasUppercaseLetter } 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 (hasUppercaseLetter(address) && !EthereumjsUtil.isValidChecksumAddress(address)) {
return 'Address is not a valid checksum';
}
return null;
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/src/getNumberFromPixelString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const getNumberFromPixelString = (size: string): number =>
parseInt(size.replace('px', ''), 10);
1 change: 0 additions & 1 deletion packages/utils/src/getNumberFromPxString.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/utils/src/hasUppercaseLetter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const HAS_UPPERCASE_LATER_REGEXP = new RegExp('^(.*[A-Z].*)$');

export const hasUppercaseLetter = (value: string) => HAS_UPPERCASE_LATER_REGEXP.test(value);
5 changes: 4 additions & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ export * from './getRandomNumberInRange';
export * from './capitalizeFirstLetter';
export * from './countBytesInString';
export * from './truncateMiddle';
export * from './getNumberFromPxString';
export * from './getNumberFromPixelString';
export * from './isUrl';
export * from './hasUppercaseLetter';
export * from './isAscii';
4 changes: 4 additions & 0 deletions packages/utils/src/isAscii.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function isAscii(value?: string): boolean {
if (!value) return true;
return /^[\x00-\x7F]*$/.test(value); // eslint-disable-line no-control-regex
}
4 changes: 4 additions & 0 deletions packages/utils/src/isUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const URL_REGEXP =
/^(http|ws)s?:\/\/[a-z0-9]([a-z0-9.-]+)?(:[0-9]{1,5})?((\/)?(([a-z0-9-_])+(\/)?)+)$/i;

export const isUrl = (value: string): boolean => URL_REGEXP.test(value);
6 changes: 6 additions & 0 deletions packages/utils/tests/getNumberFromPixelString.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { getNumberFromPixelString } from '../src/getNumberFromPixelString';

it('getNumberFromPixelString', () => {
expect(getNumberFromPixelString('1px')).toBe(1);
expect(getNumberFromPixelString('1')).toBe(1);
});
6 changes: 0 additions & 6 deletions packages/utils/tests/getNumberFromPxString.test.ts

This file was deleted.

14 changes: 14 additions & 0 deletions packages/utils/tests/hasUppercaseLetter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { hasUppercaseLetter } from '../src/hasUppercaseLetter';

describe('hasUppercaseLetter', () => {
it('hasUppercaseLetter', () => {
expect(hasUppercaseLetter('0')).toBe(false);
expect(hasUppercaseLetter('abc')).toBe(false);
expect(hasUppercaseLetter('abcD')).toBe(true);
expect(hasUppercaseLetter('Abcd')).toBe(true);
expect(hasUppercaseLetter('aBcd')).toBe(true);
expect(hasUppercaseLetter('123abc123')).toBe(false);
expect(hasUppercaseLetter('0x123abc456')).toBe(false);
expect(hasUppercaseLetter('0x123aBc456')).toBe(true);
});
});
18 changes: 18 additions & 0 deletions packages/utils/tests/isAscii.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { isAscii } from '../src/isAscii';

describe('isAscii', () => {
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', () => {
expect(isAscii('¥')).toEqual(false);
expect(isAscii('železniční přejezd')).toEqual(false);
});
});
});

0 comments on commit b8c4550

Please sign in to comment.