Skip to content

Commit

Permalink
Refine validation
Browse files Browse the repository at this point in the history
  • Loading branch information
carina-akaia committed Jun 23, 2024
1 parent ef476ed commit b2787ab
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
3 changes: 0 additions & 3 deletions src/common/api/types.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/modules/donation/hooks/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const useDonationForm = ({

const form = useForm<DonationInputs>({
resolver: zodResolver(donationSchema),
mode: "onChange",
defaultValues,
});

Expand Down Expand Up @@ -77,8 +78,7 @@ export const useDonationForm = ({
);

console.table({ isDisabled, hasChanges });
console.table(currentValues);
console.table(defaultValues);
console.table(form.formState.errors);

return {
hasChanges,
Expand Down
20 changes: 11 additions & 9 deletions src/modules/donation/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {
infer as FromSchema,
array,
boolean,
coerce,
literal,
nativeEnum,
number,
object,
preprocess,
string,
} from "zod";

Expand Down Expand Up @@ -72,15 +74,15 @@ export const donationTokenSchema = literal(NEAR_TOKEN_DENOM)
.default(NEAR_TOKEN_DENOM)
.describe('Either "NEAR" or FT contract account id.');

export const donationAmountSchema = number()
.positive()
.finite()
.gt(0.0, "Cannot be zero.")
.default(0.1)
.refine(
(n) => !number().int().safeParse(n).success,
"Must be a floating point number.",
);
export const donationAmountSchema = preprocess(
(x) => parseFloat(x as string),

number({ message: "Must be a positive number." })
.positive("Must be a positive number.")
.finite()
.safe()
.refine((n) => number().safeParse(n).success, "Must be a positive number."),
);

export const donationSchema = object({
tokenId: donationTokenSchema,
Expand Down

0 comments on commit b2787ab

Please sign in to comment.