diff --git a/apps/console-lite/src/app/components/deal-ticket/deal-ticket-steps.tsx b/apps/console-lite/src/app/components/deal-ticket/deal-ticket-steps.tsx index d72a74f8c..c1f9fa4a2 100644 --- a/apps/console-lite/src/app/components/deal-ticket/deal-ticket-steps.tsx +++ b/apps/console-lite/src/app/components/deal-ticket/deal-ticket-steps.tsx @@ -8,7 +8,6 @@ import { useOrderMargin, useMaximumPositionSize, useCalculateSlippage, - validateAmount, } from '@vegaprotocol/deal-ticket'; import { InputError } from '@vegaprotocol/ui-toolkit'; import { BigNumber } from 'bignumber.js'; @@ -23,6 +22,7 @@ import { addDecimalsFormatNumber, addDecimal, formatNumber, + validateAmount, } from '@vegaprotocol/react-helpers'; import { useOrderSubmit, diff --git a/libs/deal-ticket/src/components/deal-ticket/deal-ticket-limit-amount.tsx b/libs/deal-ticket/src/components/deal-ticket/deal-ticket-limit-amount.tsx index 47fb348c0..dfd792ef6 100644 --- a/libs/deal-ticket/src/components/deal-ticket/deal-ticket-limit-amount.tsx +++ b/libs/deal-ticket/src/components/deal-ticket/deal-ticket-limit-amount.tsx @@ -1,7 +1,6 @@ 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 { validateAmount } from '../../utils'; export type DealTicketLimitAmountProps = Omit< DealTicketAmountProps, diff --git a/libs/deal-ticket/src/components/deal-ticket/deal-ticket-market-amount.tsx b/libs/deal-ticket/src/components/deal-ticket/deal-ticket-market-amount.tsx index 6ad576f73..bcccf768f 100644 --- a/libs/deal-ticket/src/components/deal-ticket/deal-ticket-market-amount.tsx +++ b/libs/deal-ticket/src/components/deal-ticket/deal-ticket-market-amount.tsx @@ -2,9 +2,10 @@ import { addDecimalsFormatNumber, t, toDecimal, + validateAmount, } from '@vegaprotocol/react-helpers'; 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 { getMarketPrice } from '../../utils/get-price'; diff --git a/libs/deal-ticket/src/utils/index.ts b/libs/deal-ticket/src/utils/index.ts index 1095489a5..d121f44a9 100644 --- a/libs/deal-ticket/src/utils/index.ts +++ b/libs/deal-ticket/src/utils/index.ts @@ -1,6 +1,5 @@ export * from './get-default-order'; export * from './is-market-in-auction'; -export * from './validate-amount'; export * from './validate-expiration'; export * from './validate-market-state'; export * from './validate-market-trading-mode'; diff --git a/libs/orders/src/lib/components/order-list/order-edit-dialog.tsx b/libs/orders/src/lib/components/order-list/order-edit-dialog.tsx index 5ed25ff12..a0aa068a3 100644 --- a/libs/orders/src/lib/components/order-list/order-edit-dialog.tsx +++ b/libs/orders/src/lib/components/order-list/order-edit-dialog.tsx @@ -5,6 +5,7 @@ import { getDateTimeFormat, addDecimal, addDecimalsFormatNumber, + validateAmount, } from '@vegaprotocol/react-helpers'; import * as Schema from '@vegaprotocol/types'; import { @@ -50,6 +51,7 @@ export const OrderEditDialog = ({ const step = toDecimal(order.market?.decimalPlaces ?? 0); const stepSize = toDecimal(order.market?.positionDecimalPlaces ?? 0); + return (
@@ -110,6 +113,7 @@ export const OrderEditDialog = ({ Number(value) > 0 ? true : t('The price cannot be negative'), + validate: validateAmount(step, t('Price')), }, })} id="limitPrice" @@ -129,6 +133,7 @@ export const OrderEditDialog = ({ validate: { min: (value) => Number(value) > 0 ? true : t('The size cannot be negative'), + validate: validateAmount(stepSize, t('Size')), }, })} id="size" diff --git a/libs/react-helpers/src/lib/validate/common.ts b/libs/react-helpers/src/lib/validate/common.ts new file mode 100644 index 000000000..acbdeb957 --- /dev/null +++ b/libs/react-helpers/src/lib/validate/common.ts @@ -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'); + } +}; diff --git a/libs/react-helpers/src/lib/validate/index.ts b/libs/react-helpers/src/lib/validate/index.ts index acbdeb957..2b21ad686 100644 --- a/libs/react-helpers/src/lib/validate/index.ts +++ b/libs/react-helpers/src/lib/validate/index.ts @@ -1,56 +1,2 @@ -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'); - } -}; +export * from './common'; +export * from './validate-amount'; diff --git a/libs/deal-ticket/src/utils/validate-amount.ts b/libs/react-helpers/src/lib/validate/validate-amount.ts similarity index 90% rename from libs/deal-ticket/src/utils/validate-amount.ts rename to libs/react-helpers/src/lib/validate/validate-amount.ts index 10991f078..af556f340 100644 --- a/libs/deal-ticket/src/utils/validate-amount.ts +++ b/libs/react-helpers/src/lib/validate/validate-amount.ts @@ -1,4 +1,4 @@ -import { t } from '@vegaprotocol/react-helpers'; +import { t } from '../i18n'; export const validateAmount = (step: number, field: string) => { const [, stepDecimals = ''] = String(step).split('.');