Skip to content

Commit

Permalink
refactor: improve readability of bigNumberGreaterThanZeroSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
tmrdlt committed Jan 22, 2025
1 parent f137816 commit de7cfa0
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/lib/form-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import { expandDecimals } from '@/lib/utils';
import BigNumber from 'bignumber.js';
import { z } from 'zod';

const getParentChainFromLocalStorage = () =>
localStorage &&
parentChains
.filter((c) => c.symbol === getFromLocalStorage<Step2FormValues>(INITIATOR_STEP_2_STORAGE_KEY)?.parent)
.at(0);

const bigNumberGreaterThanZeroSchema = ({
fieldName = undefined,
withDecimals = true,
Expand All @@ -23,18 +29,8 @@ const bigNumberGreaterThanZeroSchema = ({
.string({ message: errorMessage ? errorMessage : `${fieldName} is required` })
.nonempty(errorMessage ? errorMessage : `${fieldName} is required`)
.transform((n, ctx) => {
const decimals =
(withDecimals &&
((localStorage &&
parentChains
.filter(
(c) =>
c.symbol ===
getFromLocalStorage<Step2FormValues>(INITIATOR_STEP_2_STORAGE_KEY)?.parent
)
.at(0)?.decimals) ||
18)) ||
0;
const decimals = withDecimals ? getParentChainFromLocalStorage()?.decimals || 18 : 0;

if (!BigNumber(n).c) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
Expand Down

0 comments on commit de7cfa0

Please sign in to comment.