From 14eaaf83d39f2322191173a7dacaf31cac09cf1f Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Thu, 1 Aug 2024 13:08:10 +0200 Subject: [PATCH] Add hexa support to FieldString --- .../forms/CreateTxForm/Fields/FieldString.tsx | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/components/forms/CreateTxForm/Fields/FieldString.tsx b/components/forms/CreateTxForm/Fields/FieldString.tsx index 1d999a9a..ef0fe798 100644 --- a/components/forms/CreateTxForm/Fields/FieldString.tsx +++ b/components/forms/CreateTxForm/Fields/FieldString.tsx @@ -1,6 +1,7 @@ import { FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { prettyFieldName } from "@/lib/form"; +import { fromHex } from "@cosmjs/encoding"; import * as z from "zod"; import type { FieldProps } from "./types"; @@ -9,11 +10,19 @@ const isFieldString = (fieldName: string) => const isOptionalFieldString = (fieldName: string) => fieldName === "label"; +const isHexFieldString = (fieldName: string) => fieldName === "salt"; + export const getFieldString = (fieldName: string) => - isFieldString(fieldName) || isOptionalFieldString(fieldName) ? FieldString : null; + isFieldString(fieldName) || isOptionalFieldString(fieldName) || isHexFieldString(fieldName) + ? FieldString + : null; export const getFieldStringSchema = (fieldName: string) => { - if (!isFieldString(fieldName) && !isOptionalFieldString(fieldName)) { + if ( + !isFieldString(fieldName) && + !isOptionalFieldString(fieldName) && + !isHexFieldString(fieldName) + ) { return null; } @@ -30,6 +39,21 @@ export const getFieldStringSchema = (fieldName: string) => { return z.optional(zodString); } + if (isHexFieldString(fieldName)) { + return zodString.transform((val, ctx) => { + try { + return fromHex(val); + } catch { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: "Must be an hexadecimal string", + }); + + return z.NEVER; + } + }); + } + return null; };