fix validation

pull/526/head
slowbackspace 5 years ago
parent 3efa72988f
commit 7da8d4124d

@ -232,7 +232,7 @@ export const amountValidation = ($state: State): PayloadAction<State> => (
const { amount } = state;
if (amount.length < 1) {
state.errors.amount = 'Amount is not set';
} else if (amount.length > 0 && !validators.isEthereumNumber(amount)) {
} else if (amount.length > 0 && !validators.isNumber(amount)) {
state.errors.amount = 'Amount is not a number';
} else {
const isToken: boolean = state.currency !== state.networkSymbol;
@ -247,7 +247,7 @@ export const amountValidation = ($state: State): PayloadAction<State> => (
);
if (!token) return state;
if (!validators.isNumber(state.amount, parseInt(token.decimals, 0))) {
if (!validators.hasDecimals(state.amount, parseInt(token.decimals, 0))) {
state.errors.amount = `Maximum ${token.decimals} decimals allowed`;
} else if (new BigNumber(state.total).isGreaterThan(account.balance)) {
state.errors.amount = `Not enough ${state.networkSymbol} to cover transaction fee`;
@ -260,7 +260,7 @@ export const amountValidation = ($state: State): PayloadAction<State> => (
} else if (new BigNumber(state.amount).isLessThanOrEqualTo('0')) {
state.errors.amount = 'Amount is too low';
}
} else if (!validators.isEthereumNumber(state.amount)) {
} else if (!validators.hasDecimals(state.amount, 18)) {
state.errors.amount = 'Maximum 18 decimals allowed';
} else if (
new BigNumber(state.total).isGreaterThan(
@ -289,7 +289,7 @@ export const gasLimitValidation = ($state: State): PayloadAction<State> => (
const { gasLimit } = state;
if (gasLimit.length < 1) {
state.errors.gasLimit = 'Gas limit is not set';
} else if (gasLimit.length > 0 && !validators.isEthereumNumber(gasLimit)) {
} else if (gasLimit.length > 0 && !validators.isNumber(gasLimit)) {
state.errors.gasLimit = 'Gas limit is not a number';
} else {
const gl: BigNumber = new BigNumber(gasLimit);
@ -318,7 +318,7 @@ export const gasPriceValidation = ($state: State): PayloadAction<State> => (): S
const { gasPrice } = state;
if (gasPrice.length < 1) {
state.errors.gasPrice = 'Gas price is not set';
} else if (gasPrice.length > 0 && !validators.isEthereumNumber(gasPrice)) {
} else if (gasPrice.length > 0 && !validators.isNumber(gasPrice)) {
state.errors.gasPrice = 'Gas price is not a number';
} else {
const gp: BigNumber = new BigNumber(gasPrice);

@ -269,11 +269,11 @@ const amountValidation = ($state: State): PayloadAction<State> => (
const { amount } = state;
if (amount.length < 1) {
state.errors.amount = 'Amount is not set';
} else if (amount.length > 0 && !validators.isRippleNumber(amount)) {
} else if (amount.length > 0 && !validators.isNumber(amount)) {
state.errors.amount = 'Amount is not a number';
} else {
const pendingAmount: BigNumber = getPendingAmount(pending, state.networkSymbol);
if (!validators.isRippleNumber(state.amount)) {
if (!validators.hasDecimals(state.amount, 6)) {
state.errors.amount = 'Maximum 6 decimals allowed';
} else if (
new BigNumber(state.total).isGreaterThan(

@ -1,40 +1,53 @@
import * as utils from '../validators';
describe('validators utils', () => {
it('isEthereumNumber', () => {
expect(utils.isEthereumNumber('0')).toBe(true);
expect(utils.isEthereumNumber('0.0')).toBe(true);
expect(utils.isEthereumNumber('0.00000000')).toBe(true);
expect(utils.isEthereumNumber('0.00000001')).toBe(true);
expect(utils.isEthereumNumber('+0.0')).toBe(false);
expect(utils.isEthereumNumber('-0.0')).toBe(false);
expect(utils.isEthereumNumber('1')).toBe(true);
expect(utils.isEthereumNumber('+1')).toBe(false);
expect(utils.isEthereumNumber('+100000')).toBe(false);
expect(utils.isEthereumNumber('.')).toBe(false);
expect(utils.isEthereumNumber('-.1')).toBe(false);
expect(utils.isEthereumNumber('0.1')).toBe(true);
expect(utils.isEthereumNumber('0.12314841')).toBe(true);
expect(utils.isEthereumNumber('0.1381841848184814818391931933')).toBe(false); //28 decimals
expect(utils.isEthereumNumber('0.100000000000000000')).toBe(true); //18s decimals
expect(utils.isEthereumNumber('100.')).toBe(true);
expect(utils.isEthereumNumber('.1')).toBe(false);
expect(utils.isEthereumNumber('.000000001')).toBe(false);
expect(utils.isEthereumNumber('.13134818481481841')).toBe(false);
expect(utils.isEthereumNumber('001.12314841')).toBe(false);
expect(utils.isEthereumNumber('83819319391491949941')).toBe(true);
expect(utils.isEthereumNumber('-83819319391491949941')).toBe(false);
expect(utils.isEthereumNumber('+0.131831848184')).toBe(false);
expect(utils.isEthereumNumber('0.127373193981774718318371831731761626162613')).toBe(false);
expect(utils.isEthereumNumber('0.131831848184a')).toBe(false);
expect(utils.isEthereumNumber('100a')).toBe(false);
expect(utils.isEthereumNumber('.100a')).toBe(false);
expect(utils.isEthereumNumber('a.100')).toBe(false);
expect(utils.isEthereumNumber('abc')).toBe(false);
expect(utils.isEthereumNumber('1abc0')).toBe(false);
it('hasDecimals', () => {
expect(utils.hasDecimals('0', 18)).toBe(true);
expect(utils.hasDecimals('0.0', 18)).toBe(true);
expect(utils.hasDecimals('0.00000000', 18)).toBe(true);
expect(utils.hasDecimals('0.00000001', 18)).toBe(true);
expect(utils.hasDecimals('+0.0', 18)).toBe(false);
expect(utils.hasDecimals('-0.0', 18)).toBe(false);
expect(utils.hasDecimals('1', 18)).toBe(true);
expect(utils.hasDecimals('+1', 18)).toBe(false);
expect(utils.hasDecimals('+100000', 18)).toBe(false);
expect(utils.hasDecimals('.', 18)).toBe(false);
expect(utils.hasDecimals('-.1', 18)).toBe(false);
expect(utils.hasDecimals('0.1', 18)).toBe(true);
expect(utils.hasDecimals('0.12314841', 18)).toBe(true);
expect(utils.hasDecimals('0.1381841848184814818391931933', 18)).toBe(false); //28 decimals
expect(utils.hasDecimals('0.100000000000000000', 18)).toBe(true); //18s decimals
expect(utils.hasDecimals('100.', 18)).toBe(true);
expect(utils.hasDecimals('.1', 18)).toBe(false);
expect(utils.hasDecimals('.000000001', 18)).toBe(false);
expect(utils.hasDecimals('.13134818481481841', 18)).toBe(false);
expect(utils.hasDecimals('001.12314841', 18)).toBe(false);
expect(utils.hasDecimals('83819319391491949941', 18)).toBe(true);
expect(utils.hasDecimals('-83819319391491949941', 18)).toBe(false);
expect(utils.hasDecimals('+0.131831848184', 18)).toBe(false);
expect(utils.hasDecimals('0.127373193981774718318371831731761626162613', 18)).toBe(false);
expect(utils.hasDecimals('0.131831848184a', 18)).toBe(false);
expect(utils.hasDecimals('100a', 18)).toBe(false);
expect(utils.hasDecimals('.100a', 18)).toBe(false);
expect(utils.hasDecimals('a.100', 18)).toBe(false);
expect(utils.hasDecimals('abc', 18)).toBe(false);
expect(utils.hasDecimals('1abc0', 18)).toBe(false);
});
it('hasDecimals decimals=0', () => {
expect(utils.hasDecimals('0', 0)).toBe(true);
expect(utils.hasDecimals('0.1', 0)).toBe(false);
expect(utils.hasDecimals('0.12345', 0)).toBe(false);
expect(utils.hasDecimals('1', 0)).toBe(true);
expect(utils.hasDecimals('1.1', 0)).toBe(false);
expect(utils.hasDecimals('1000000', 0)).toBe(true);
expect(utils.hasDecimals('-1000000', 0)).toBe(false);
expect(utils.hasDecimals('.0', 0)).toBe(false);
expect(utils.hasDecimals('0.', 0)).toBe(false);
expect(utils.hasDecimals('.', 0)).toBe(false);
});
it('hasUppercase', () => {
@ -48,16 +61,40 @@ describe('validators utils', () => {
expect(utils.hasUppercase('0x123aBc456')).toBe(true);
});
it('isNumber decimals=0', () => {
expect(utils.isNumber('0', 0)).toBe(true);
expect(utils.isNumber('0.1', 0)).toBe(false);
expect(utils.isNumber('0.12345', 0)).toBe(false);
expect(utils.isNumber('1', 0)).toBe(true);
expect(utils.isNumber('1.1', 0)).toBe(false);
expect(utils.isNumber('1000000', 0)).toBe(true);
expect(utils.isNumber('-1000000', 0)).toBe(false);
expect(utils.isNumber('.0', 0)).toBe(false);
expect(utils.isNumber('0.', 0)).toBe(false);
expect(utils.isNumber('.', 0)).toBe(false);
it('isNumber', () => {
expect(utils.isNumber('0')).toBe(true);
expect(utils.isNumber('0.0')).toBe(true);
expect(utils.isNumber('0.00000000')).toBe(true);
expect(utils.isNumber('0.00000001')).toBe(true);
expect(utils.isNumber('+0.0')).toBe(false);
expect(utils.isNumber('-0.0')).toBe(false);
expect(utils.isNumber('1')).toBe(true);
expect(utils.isNumber('+1')).toBe(false);
expect(utils.isNumber('+100000')).toBe(false);
expect(utils.isNumber('.')).toBe(false);
expect(utils.isNumber('')).toBe(false);
expect(utils.isNumber(' ')).toBe(false);
expect(utils.isNumber('-.1')).toBe(false);
expect(utils.isNumber('0.1')).toBe(true);
expect(utils.isNumber('0.12314841')).toBe(true);
expect(utils.isNumber('0.1381841848184814818391931933')).toBe(true); //28 decimals
expect(utils.isNumber('0.100000000000000000')).toBe(true); //18s decimals
expect(utils.isNumber('100.')).toBe(true);
expect(utils.isNumber('.1')).toBe(false);
expect(utils.isNumber('.000000001')).toBe(false);
expect(utils.isNumber('.13134818481481841')).toBe(false);
expect(utils.isNumber('001.12314841')).toBe(false);
expect(utils.isNumber('83819319391491949941')).toBe(true);
expect(utils.isNumber('-83819319391491949941')).toBe(false);
expect(utils.isNumber('+0.131831848184')).toBe(false);
expect(utils.isNumber('0.131831848184a')).toBe(false);
expect(utils.isNumber('100a')).toBe(false);
expect(utils.isNumber('.100a')).toBe(false);
expect(utils.isNumber('a.100')).toBe(false);
expect(utils.isNumber('abc')).toBe(false);
expect(utils.isNumber('1abc0')).toBe(false);
});
});

@ -5,7 +5,7 @@ export const hasUppercase = (value: string) => {
return UPPERCASE_RE.test(value);
};
export const isNumber = (value: string, decimals: number) => {
export const hasDecimals = (value: string, decimals: number) => {
if (decimals === 0) {
return isAbs(value);
}
@ -21,10 +21,7 @@ export const isAbs = (value: string) => {
return ABS_RE.test(value);
};
export const isEthereumNumber = (value: string) => {
return isNumber(value, 18);
};
export const isRippleNumber = (value: string) => {
return isNumber(value, 6);
export const isNumber = (value: string) => {
const ETH_18_RE = new RegExp(`^(0|0\\.([0-9]+)?|[1-9][0-9]*\\.?([0-9]+)?)$`);
return ETH_18_RE.test(value);
};

Loading…
Cancel
Save