Skip to content

Commit

Permalink
Add hexa support to FieldString
Browse files Browse the repository at this point in the history
  • Loading branch information
abefernan committed Aug 1, 2024
1 parent 8b2b200 commit 14eaaf8
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions components/forms/CreateTxForm/Fields/FieldString.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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;
}

Expand All @@ -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;
};

Expand Down

0 comments on commit 14eaaf8

Please sign in to comment.