From 56a4c93cc14989850b3cd706c9d55629593c8a6e Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Mon, 22 Jul 2024 12:22:56 +0200 Subject: [PATCH] Add initial form utils --- lib/form.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 lib/form.ts diff --git a/lib/form.ts b/lib/form.ts new file mode 100644 index 0000000..d48757c --- /dev/null +++ b/lib/form.ts @@ -0,0 +1,28 @@ +import { FieldSchemaInput } from "@/components/forms/CreateTxForm/Fields/types"; +import { z } from "zod"; + +export const prettyFieldName = (fieldName: string) => { + const splitName = fieldName.split(/(?=[A-Z])/).join(" "); + const capitalizedName = splitName.charAt(0).toUpperCase() + splitName.slice(1); + + return capitalizedName; +}; + +export const getField = (_fieldName: string) => { + /* Will return a FormField component per fieldName */ + return () => null; +}; + +const getFieldSchema = (_fieldName: string) => { + /* Will return a zod schema getter per fieldName */ + return (_schemaInput: unknown) => null; +}; + +export const getMsgSchema = (fieldNames: readonly string[], schemaInput: FieldSchemaInput) => { + const fieldEntries = fieldNames.map((fieldName) => [ + fieldName, + getFieldSchema(fieldName)(schemaInput), + ]); + + return z.object(Object.fromEntries(fieldEntries)); +};