From 192f118e2c8a0d13f95adc485c4373395af425b4 Mon Sep 17 00:00:00 2001 From: Mohammer5 Date: Tue, 27 Feb 2024 17:51:06 +0800 Subject: [PATCH] refactor: dry up form validation & zod segmentsToPath (post-rebase) --- src/pages/dataElementGroupSets/Edit.tsx | 11 +++++--- src/pages/dataElementGroupSets/New.tsx | 11 +++++--- src/pages/dataElementGroupSets/form/index.ts | 2 +- .../dataElementGroupSets/form/validate.ts | 27 ------------------- 4 files changed, 17 insertions(+), 34 deletions(-) delete mode 100644 src/pages/dataElementGroupSets/form/validate.ts diff --git a/src/pages/dataElementGroupSets/Edit.tsx b/src/pages/dataElementGroupSets/Edit.tsx index 04470f49..7a8d3700 100644 --- a/src/pages/dataElementGroupSets/Edit.tsx +++ b/src/pages/dataElementGroupSets/Edit.tsx @@ -10,12 +10,15 @@ import { StandardFormActions, StandardFormSection, } from '../../components' -import { SCHEMA_SECTIONS, getSectionPath } from '../../lib' +import { SCHEMA_SECTIONS, getSectionPath, validate } from '../../lib' import { JsonPatchOperation } from '../../types' import { DataElementGroupSet } from '../../types/generated' import { createJsonPatchOperations } from './edit/' import classes from './Edit.module.css' -import { DataElementGroupSetFormFields, validate } from './form' +import { + DataElementGroupSetFormFields, + dataElementGroupSetSchema, +} from './form' import type { FormValues } from './form' type FinalFormFormApi = FormApi @@ -129,7 +132,9 @@ export const Component = () => {
{ + return validate(dataElementGroupSetSchema, values) + }} initialValues={initialValues} > {({ handleSubmit, submitting, submitError }) => ( diff --git a/src/pages/dataElementGroupSets/New.tsx b/src/pages/dataElementGroupSets/New.tsx index ccef5e4e..d9002490 100644 --- a/src/pages/dataElementGroupSets/New.tsx +++ b/src/pages/dataElementGroupSets/New.tsx @@ -6,8 +6,11 @@ import React, { useEffect, useRef } from 'react' import { Form } from 'react-final-form' import { useNavigate } from 'react-router-dom' import { StandardFormActions, StandardFormSection } from '../../components' -import { SCHEMA_SECTIONS, getSectionPath } from '../../lib' -import { DataElementGroupSetFormFields, validate } from './form' +import { SCHEMA_SECTIONS, getSectionPath, validate } from '../../lib' +import { + DataElementGroupSetFormFields, + dataElementGroupSetSchema, +} from './form' import type { FormValues } from './form' import classes from './New.module.css' @@ -52,7 +55,9 @@ export function Component() { { + return validate(dataElementGroupSetSchema, values) + }} initialValues={initialValues} > {({ handleSubmit, submitting, submitError }) => ( diff --git a/src/pages/dataElementGroupSets/form/index.ts b/src/pages/dataElementGroupSets/form/index.ts index e12100af..b1e81159 100644 --- a/src/pages/dataElementGroupSets/form/index.ts +++ b/src/pages/dataElementGroupSets/form/index.ts @@ -1,3 +1,3 @@ export { DataElementGroupSetFormFields } from './DataElementGroupSetFormFields' +export { dataElementGroupSetSchema } from './dataElementGroupSetSchema' export type { FormValues } from './types' -export { validate } from './validate' diff --git a/src/pages/dataElementGroupSets/form/validate.ts b/src/pages/dataElementGroupSets/form/validate.ts deleted file mode 100644 index efe2d856..00000000 --- a/src/pages/dataElementGroupSets/form/validate.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { setIn } from 'final-form' -import { dataElementGroupSetSchema } from './dataElementGroupSetSchema' -import type { FormValues } from './types' - -// @TODO: Figure out if there's a utility for this? I couldn't find one -function segmentsToPath(segments: Array) { - return segments.reduce((path, segment) => { - return typeof segment === 'number' - ? `${path}[${segment}]` - : `${path}.${segment}` - }) as string -} - -export function validate(values: FormValues) { - const zodResult = dataElementGroupSetSchema.safeParse(values) - - if (zodResult.success !== false) { - return undefined - } - - const allFormErrors = zodResult.error.issues.reduce((formErrors, error) => { - const errorPath = segmentsToPath(error.path) - return setIn(formErrors, errorPath, error.message) - }, {}) - - return allFormErrors -}