forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
42 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type {FormInputErrors, FormOnyxValues} from '@components/Form/types'; | ||
import type ONYXKEYS from '@src/ONYXKEYS'; | ||
import * as CurrencyUtils from './CurrencyUtils'; | ||
import getPermittedDecimalSeparator from './getPermittedDecimalSeparator'; | ||
import * as MoneyRequestUtils from './MoneyRequestUtils'; | ||
import * as NumberUtils from './NumberUtils'; | ||
|
||
type RateValueForm = typeof ONYXKEYS.FORMS.WORKSPACE_RATE_AND_UNIT_FORM | typeof ONYXKEYS.FORMS.POLICY_CREATE_DISTANCE_RATE_FORM; | ||
|
||
function validateRateValue(values: FormOnyxValues<RateValueForm>, currency: string, toLocaleDigit: (arg: string) => string): FormInputErrors<RateValueForm> { | ||
const errors: FormInputErrors<RateValueForm> = {}; | ||
const rate = values.rate; | ||
const parsedRate = MoneyRequestUtils.replaceAllDigits(rate, toLocaleDigit); | ||
const decimalSeparator = toLocaleDigit('.'); | ||
|
||
// Allow one more decimal place for accuracy | ||
const rateValueRegex = RegExp(String.raw`^-?\d{0,8}([${getPermittedDecimalSeparator(decimalSeparator)}]\d{1,${CurrencyUtils.getCurrencyDecimals(currency) + 1}})?$`, 'i'); | ||
if (!rateValueRegex.test(parsedRate) || parsedRate === '') { | ||
errors.rate = 'workspace.reimburse.invalidRateError'; | ||
} else if (NumberUtils.parseFloatAnyLocale(parsedRate) <= 0) { | ||
errors.rate = 'workspace.reimburse.lowRateError'; | ||
} | ||
return errors; | ||
} | ||
|
||
export default validateRateValue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters