fix: error message consitency (2486) (#2507)
fix: error message consitency (2486)
This commit is contained in:
parent
904a1b7805
commit
1211d27011
@ -8,7 +8,6 @@ import {
|
|||||||
useOrderMargin,
|
useOrderMargin,
|
||||||
useMaximumPositionSize,
|
useMaximumPositionSize,
|
||||||
useCalculateSlippage,
|
useCalculateSlippage,
|
||||||
validateAmount,
|
|
||||||
} from '@vegaprotocol/deal-ticket';
|
} from '@vegaprotocol/deal-ticket';
|
||||||
import { InputError } from '@vegaprotocol/ui-toolkit';
|
import { InputError } from '@vegaprotocol/ui-toolkit';
|
||||||
import { BigNumber } from 'bignumber.js';
|
import { BigNumber } from 'bignumber.js';
|
||||||
@ -23,6 +22,7 @@ import {
|
|||||||
addDecimalsFormatNumber,
|
addDecimalsFormatNumber,
|
||||||
addDecimal,
|
addDecimal,
|
||||||
formatNumber,
|
formatNumber,
|
||||||
|
validateAmount,
|
||||||
} from '@vegaprotocol/react-helpers';
|
} from '@vegaprotocol/react-helpers';
|
||||||
import {
|
import {
|
||||||
useOrderSubmit,
|
useOrderSubmit,
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import { FormGroup, Input, InputError } from '@vegaprotocol/ui-toolkit';
|
import { FormGroup, Input, InputError } from '@vegaprotocol/ui-toolkit';
|
||||||
import { t, toDecimal } from '@vegaprotocol/react-helpers';
|
import { t, toDecimal, validateAmount } from '@vegaprotocol/react-helpers';
|
||||||
import type { DealTicketAmountProps } from './deal-ticket-amount';
|
import type { DealTicketAmountProps } from './deal-ticket-amount';
|
||||||
import { validateAmount } from '../../utils';
|
|
||||||
|
|
||||||
export type DealTicketLimitAmountProps = Omit<
|
export type DealTicketLimitAmountProps = Omit<
|
||||||
DealTicketAmountProps,
|
DealTicketAmountProps,
|
||||||
|
@ -2,9 +2,10 @@ import {
|
|||||||
addDecimalsFormatNumber,
|
addDecimalsFormatNumber,
|
||||||
t,
|
t,
|
||||||
toDecimal,
|
toDecimal,
|
||||||
|
validateAmount,
|
||||||
} from '@vegaprotocol/react-helpers';
|
} from '@vegaprotocol/react-helpers';
|
||||||
import { Input, InputError, Tooltip } from '@vegaprotocol/ui-toolkit';
|
import { Input, InputError, Tooltip } from '@vegaprotocol/ui-toolkit';
|
||||||
import { isMarketInAuction, validateAmount } from '../../utils';
|
import { isMarketInAuction } from '../../utils';
|
||||||
import type { DealTicketAmountProps } from './deal-ticket-amount';
|
import type { DealTicketAmountProps } from './deal-ticket-amount';
|
||||||
import { getMarketPrice } from '../../utils/get-price';
|
import { getMarketPrice } from '../../utils/get-price';
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
export * from './get-default-order';
|
export * from './get-default-order';
|
||||||
export * from './is-market-in-auction';
|
export * from './is-market-in-auction';
|
||||||
export * from './validate-amount';
|
|
||||||
export * from './validate-expiration';
|
export * from './validate-expiration';
|
||||||
export * from './validate-market-state';
|
export * from './validate-market-state';
|
||||||
export * from './validate-market-trading-mode';
|
export * from './validate-market-trading-mode';
|
||||||
|
@ -5,6 +5,7 @@ import {
|
|||||||
getDateTimeFormat,
|
getDateTimeFormat,
|
||||||
addDecimal,
|
addDecimal,
|
||||||
addDecimalsFormatNumber,
|
addDecimalsFormatNumber,
|
||||||
|
validateAmount,
|
||||||
} from '@vegaprotocol/react-helpers';
|
} from '@vegaprotocol/react-helpers';
|
||||||
import * as Schema from '@vegaprotocol/types';
|
import * as Schema from '@vegaprotocol/types';
|
||||||
import {
|
import {
|
||||||
@ -50,6 +51,7 @@ export const OrderEditDialog = ({
|
|||||||
|
|
||||||
const step = toDecimal(order.market?.decimalPlaces ?? 0);
|
const step = toDecimal(order.market?.decimalPlaces ?? 0);
|
||||||
const stepSize = toDecimal(order.market?.positionDecimalPlaces ?? 0);
|
const stepSize = toDecimal(order.market?.positionDecimalPlaces ?? 0);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog
|
<Dialog
|
||||||
open={isOpen}
|
open={isOpen}
|
||||||
@ -97,6 +99,7 @@ export const OrderEditDialog = ({
|
|||||||
onSubmit={handleSubmit(onSubmit)}
|
onSubmit={handleSubmit(onSubmit)}
|
||||||
data-testid="edit-order"
|
data-testid="edit-order"
|
||||||
className="w-full mt-4"
|
className="w-full mt-4"
|
||||||
|
noValidate
|
||||||
>
|
>
|
||||||
<div className="flex flex-col md:flex-row gap-4">
|
<div className="flex flex-col md:flex-row gap-4">
|
||||||
<FormGroup label={t('Price')} labelFor="limitPrice" className="grow">
|
<FormGroup label={t('Price')} labelFor="limitPrice" className="grow">
|
||||||
@ -110,6 +113,7 @@ export const OrderEditDialog = ({
|
|||||||
Number(value) > 0
|
Number(value) > 0
|
||||||
? true
|
? true
|
||||||
: t('The price cannot be negative'),
|
: t('The price cannot be negative'),
|
||||||
|
validate: validateAmount(step, t('Price')),
|
||||||
},
|
},
|
||||||
})}
|
})}
|
||||||
id="limitPrice"
|
id="limitPrice"
|
||||||
@ -129,6 +133,7 @@ export const OrderEditDialog = ({
|
|||||||
validate: {
|
validate: {
|
||||||
min: (value) =>
|
min: (value) =>
|
||||||
Number(value) > 0 ? true : t('The size cannot be negative'),
|
Number(value) > 0 ? true : t('The size cannot be negative'),
|
||||||
|
validate: validateAmount(stepSize, t('Size')),
|
||||||
},
|
},
|
||||||
})}
|
})}
|
||||||
id="size"
|
id="size"
|
||||||
|
56
libs/react-helpers/src/lib/validate/common.ts
Normal file
56
libs/react-helpers/src/lib/validate/common.ts
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
import BigNumber from 'bignumber.js';
|
||||||
|
import { ethers } from 'ethers';
|
||||||
|
import { t } from '../i18n';
|
||||||
|
|
||||||
|
export const required = (value: string) => {
|
||||||
|
if (value === null || value === undefined || value === '') {
|
||||||
|
return t('Required');
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const ethereumAddress = (value: string) => {
|
||||||
|
if (!ethers.utils.isAddress(value)) {
|
||||||
|
return t('Invalid Ethereum address');
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const vegaPublicKey = (value: string) => {
|
||||||
|
if (value.length !== 64 || !/^[A-Fa-f0-9]*$/i.test(value)) {
|
||||||
|
return t('Invalid Vega key');
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const minSafe = (min: BigNumber) => (value: string) => {
|
||||||
|
if (new BigNumber(value).isLessThan(min)) {
|
||||||
|
return t('Value is below minimum');
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const maxSafe = (max: BigNumber) => (value: string) => {
|
||||||
|
if (new BigNumber(value).isGreaterThan(max)) {
|
||||||
|
return t('Value is above maximum');
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const suitableForSyntaxHighlighter = (str: string) => {
|
||||||
|
try {
|
||||||
|
const test = JSON.parse(str);
|
||||||
|
return test && Object.keys(test).length > 0;
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const validateJson = (value: string) => {
|
||||||
|
try {
|
||||||
|
JSON.parse(value);
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
return t('Must be valid JSON');
|
||||||
|
}
|
||||||
|
};
|
@ -1,56 +1,2 @@
|
|||||||
import BigNumber from 'bignumber.js';
|
export * from './common';
|
||||||
import { ethers } from 'ethers';
|
export * from './validate-amount';
|
||||||
import { t } from '../i18n';
|
|
||||||
|
|
||||||
export const required = (value: string) => {
|
|
||||||
if (value === null || value === undefined || value === '') {
|
|
||||||
return t('Required');
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const ethereumAddress = (value: string) => {
|
|
||||||
if (!ethers.utils.isAddress(value)) {
|
|
||||||
return t('Invalid Ethereum address');
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const vegaPublicKey = (value: string) => {
|
|
||||||
if (value.length !== 64 || !/^[A-Fa-f0-9]*$/i.test(value)) {
|
|
||||||
return t('Invalid Vega key');
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const minSafe = (min: BigNumber) => (value: string) => {
|
|
||||||
if (new BigNumber(value).isLessThan(min)) {
|
|
||||||
return t('Value is below minimum');
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const maxSafe = (max: BigNumber) => (value: string) => {
|
|
||||||
if (new BigNumber(value).isGreaterThan(max)) {
|
|
||||||
return t('Value is above maximum');
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const suitableForSyntaxHighlighter = (str: string) => {
|
|
||||||
try {
|
|
||||||
const test = JSON.parse(str);
|
|
||||||
return test && Object.keys(test).length > 0;
|
|
||||||
} catch (e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const validateJson = (value: string) => {
|
|
||||||
try {
|
|
||||||
JSON.parse(value);
|
|
||||||
return true;
|
|
||||||
} catch (e) {
|
|
||||||
return t('Must be valid JSON');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { t } from '@vegaprotocol/react-helpers';
|
import { t } from '../i18n';
|
||||||
|
|
||||||
export const validateAmount = (step: number, field: string) => {
|
export const validateAmount = (step: number, field: string) => {
|
||||||
const [, stepDecimals = ''] = String(step).split('.');
|
const [, stepDecimals = ''] = String(step).split('.');
|
Loading…
Reference in New Issue
Block a user