From d8c98788155ae13ab1548bad656d1b74983cb172 Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Fri, 26 Jul 2024 16:10:37 +0200 Subject: [PATCH] Add specific helpers to FieldAddress --- .../CreateTxForm/Fields/FieldAddress.tsx | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/components/forms/CreateTxForm/Fields/FieldAddress.tsx b/components/forms/CreateTxForm/Fields/FieldAddress.tsx index a6fe54f..af70d39 100644 --- a/components/forms/CreateTxForm/Fields/FieldAddress.tsx +++ b/components/forms/CreateTxForm/Fields/FieldAddress.tsx @@ -3,19 +3,43 @@ import { Input } from "@/components/ui/input"; import { useChains } from "@/context/ChainsContext"; import { exampleAddress } from "@/lib/displayHelpers"; import { prettyFieldName } from "@/lib/form"; +import { assert } from "@cosmjs/utils"; import * as z from "zod"; import type { FieldProps, FieldSchemaInput } from "./types"; -export const getFieldAddressSchema = ({ chain }: FieldSchemaInput) => { - if (!chain) { - throw new Error("Could not get field address schema because of missing chain parameter"); +const isFieldAddress = (fieldName: string) => + fieldName.toLowerCase().includes("address") || + ["depositor", "proposer", "voter", "sender", "receiver", "contract", "newAdmin"].includes( + fieldName, + ); + +const isOptionalFieldAddress = (fieldName: string) => fieldName === "admin"; + +export const getFieldAddress = (fieldName: string) => + isFieldAddress(fieldName) || isOptionalFieldAddress(fieldName) ? FieldAddress : null; + +export const getFieldAddressSchema = (fieldName: string, { chain }: FieldSchemaInput) => { + assert(chain, "Could not get field address schema because of missing chain parameter"); + + if (!isFieldAddress(fieldName) && !isOptionalFieldAddress(fieldName)) { + return null; } - return z + const zodString = z .string({ invalid_type_error: "Must be a string", required_error: "Required" }) .trim() .min(1, "Required") .startsWith(chain.addressPrefix, `Invalid prefix for ${chain.chainDisplayName}`); + + if (isFieldAddress(fieldName)) { + return zodString; + } + + if (isOptionalFieldAddress(fieldName)) { + return z.optional(zodString); + } + + return null; }; export default function FieldAddress({ form, fieldFormName }: FieldProps) {