Skip to content

Commit ff504aa

Browse files
committed
remove zod address schema cleanup
1 parent 648380f commit ff504aa

File tree

5 files changed

+27
-72
lines changed

5 files changed

+27
-72
lines changed

apps/dashboard/src/app/(dashboard)/(chain)/[chain_id]/[contractAddress]/modules/components/Claimable.tsx

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,14 @@ import { useCallback } from "react";
2828
import { useFieldArray, useForm } from "react-hook-form";
2929
import { toast } from "sonner";
3030
import { type PreparedTransaction, sendAndConfirmTransaction } from "thirdweb";
31-
import { isAddress } from "thirdweb";
3231
import { ClaimableERC721, ClaimableERC1155 } from "thirdweb/modules";
3332
import { useActiveAccount, useReadContract } from "thirdweb/react";
3433
import { z } from "zod";
34+
import { addressSchema } from "../zod-schemas";
3535
import { CurrencySelector } from "./CurrencySelector";
3636
import { ModuleCardUI, type ModuleCardUIProps } from "./module-card";
3737
import type { ModuleInstanceProps } from "./module-instance";
3838

39-
const zodAddress = z.string().refine(
40-
(v) => {
41-
if (isAddress(v)) {
42-
return true;
43-
}
44-
return false;
45-
},
46-
{ message: "Invalid Address" },
47-
);
48-
4939
export type ClaimCondition = {
5040
availableSupply: bigint;
5141
allowlistMerkleRoot: `0x${string}`;
@@ -252,7 +242,7 @@ export function ClaimableModuleUI(
252242
}
253243

254244
const configFormSchema = z.object({
255-
primarySaleRecipient: zodAddress,
245+
primarySaleRecipient: addressSchema,
256246

257247
tokenId: z.string().optional(),
258248

@@ -273,7 +263,7 @@ const configFormSchema = z.object({
273263
startTime: z.date().optional(),
274264
endTime: z.date().optional(),
275265

276-
allowList: z.array(z.object({ address: zodAddress })).optional(),
266+
allowList: z.array(z.object({ address: addressSchema })).optional(),
277267
});
278268

279269
export type ConfigFormValues = z.infer<typeof configFormSchema>;
@@ -550,7 +540,7 @@ const mintFormSchema = z.object({
550540
quantity: z.string().refine((v) => v.length > 0 && Number(v) >= 0, {
551541
message: "Invalid quantity",
552542
}),
553-
recipient: zodAddress,
543+
recipient: addressSchema,
554544
});
555545

556546
export type MintFormValues = z.infer<typeof mintFormSchema>;

apps/dashboard/src/app/(dashboard)/(chain)/[chain_id]/[contractAddress]/modules/components/Mintable.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ import { useCallback } from "react";
2929
import { useForm } from "react-hook-form";
3030
import { toast } from "sonner";
3131
import { type PreparedTransaction, sendAndConfirmTransaction } from "thirdweb";
32-
import { isAddress } from "thirdweb";
3332
import { MintableERC721, MintableERC1155 } from "thirdweb/modules";
3433
import { useReadContract } from "thirdweb/react";
3534
import type { NFTMetadataInputLimited } from "types/modified-types";
3635
import { parseAttributes } from "utils/parseAttributes";
3736
import { z } from "zod";
37+
import { addressSchema } from "../zod-schemas";
3838
import { ModuleCardUI, type ModuleCardUIProps } from "./module-card";
3939
import type { ModuleInstanceProps } from "./module-instance";
4040
import { AdvancedNFTMetadataFormGroup } from "./nft/AdvancedNFTMetadataFormGroup";
@@ -215,18 +215,7 @@ export function MintableModuleUI(
215215
}
216216

217217
const primarySaleRecipientFormSchema = z.object({
218-
primarySaleRecipient: z.string().refine(
219-
(v) => {
220-
// don't return `isAddress(v)` directly to avoid typecasting address as `0x${string}`
221-
if (isAddress(v)) {
222-
return true;
223-
}
224-
return false;
225-
},
226-
{
227-
message: "Invalid Address",
228-
},
229-
),
218+
primarySaleRecipient: addressSchema,
230219
});
231220

232221
function PrimarySalesSection(props: {

apps/dashboard/src/app/(dashboard)/(chain)/[chain_id]/[contractAddress]/modules/components/Royalty.tsx

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ import { CircleAlertIcon } from "lucide-react";
2424
import { useCallback } from "react";
2525
import { useForm } from "react-hook-form";
2626
import { toast } from "sonner";
27-
import { isAddress, sendAndConfirmTransaction } from "thirdweb";
27+
import { sendAndConfirmTransaction } from "thirdweb";
2828
import { RoyaltyERC721 } from "thirdweb/modules";
2929
import { useReadContract } from "thirdweb/react";
3030
import { z } from "zod";
31+
import { addressSchema } from "../zod-schemas";
3132
import { ModuleCardUI, type ModuleCardUIProps } from "./module-card";
3233
import type { ModuleInstanceProps } from "./module-instance";
3334

@@ -192,15 +193,7 @@ const royaltyInfoFormSchema = z.object({
192193
.string()
193194
.min(1, { message: "Invalid Token ID" })
194195
.refine((v) => Number(v) >= 0, { message: "Invalid Token ID" }),
195-
recipient: z.string().refine(
196-
(v) => {
197-
if (isAddress(v)) {
198-
return true;
199-
}
200-
return false;
201-
},
202-
{ message: "Invalid Address" },
203-
),
196+
recipient: addressSchema,
204197
bps: z
205198
.string()
206199
.min(1, { message: "Invalid BPS" })
@@ -306,31 +299,11 @@ function RoyaltyInfoPerTokenSection(props: {
306299
}
307300

308301
const defaultRoyaltyFormSchema = z.object({
309-
recipient: z.string().refine(
310-
(v) => {
311-
// return true if it is an empty string to emulate optional condition
312-
// don't return `isAddress(v)` directly to avoid typecasting address as `0x${string}
313-
if (!v || isAddress(v)) {
314-
return true;
315-
}
316-
return false;
317-
},
318-
{ message: "Invalid Address" },
319-
),
302+
recipient: addressSchema,
320303
bps: z.string().refine((v) => v.length === 0 || Number(v) >= 0, {
321304
message: "Invalid BPS",
322305
}),
323-
transferValidator: z.string().refine(
324-
(v) => {
325-
// return true if it is an empty string to emulate optional condition
326-
// don't return `isAddress(v)` directly to avoid typecasting address as `0x${string}
327-
if (!v || isAddress(v)) {
328-
return true;
329-
}
330-
return false;
331-
},
332-
{ message: "Invalid Address" },
333-
),
306+
transferValidator: addressSchema,
334307
});
335308

336309
export type RoyaltyConfigFormValues = z.infer<typeof defaultRoyaltyFormSchema>;

apps/dashboard/src/app/(dashboard)/(chain)/[chain_id]/[contractAddress]/modules/components/Transferable.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,18 @@ import { CircleAlertIcon, PlusIcon, Trash2Icon } from "lucide-react";
2020
import { useCallback } from "react";
2121
import { useFieldArray, useForm } from "react-hook-form";
2222
import { toast } from "sonner";
23-
import { isAddress, sendAndConfirmTransaction } from "thirdweb";
23+
import { sendAndConfirmTransaction } from "thirdweb";
2424
import { TransferableERC721 } from "thirdweb/modules";
2525
import { useReadContract } from "thirdweb/react";
2626
import { z } from "zod";
27+
import { addressSchema } from "../zod-schemas";
2728
import { ModuleCardUI, type ModuleCardUIProps } from "./module-card";
2829
import type { ModuleInstanceProps } from "./module-instance";
2930

3031
const formSchema = z.object({
3132
allowList: z.array(
3233
z.object({
33-
address: z.string().refine(
34-
(v) => {
35-
// don't return `isAddress(v)` directly to avoid typecasting address as `0x${string}`
36-
if (isAddress(v)) {
37-
return true;
38-
}
39-
return false;
40-
},
41-
{
42-
message: "Invalid Address",
43-
},
44-
),
34+
address: addressSchema,
4535
}),
4636
),
4737
isRestricted: z.boolean(),
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { isAddress } from "thirdweb";
2+
import { z } from "zod";
3+
4+
export const addressSchema = z.string().refine(
5+
(v) => {
6+
// not returning the return value of isAddress directly on purpose to prevent type narrowing to `0x${string}`
7+
if (isAddress(v)) {
8+
return true;
9+
}
10+
return false;
11+
},
12+
{ message: "Invalid Address" },
13+
);

0 commit comments

Comments
 (0)