Skip to content

Commit

Permalink
refactor: dry up form validation & zod segmentsToPath (post-rebase)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed Feb 27, 2024
1 parent 959f80e commit 0463ad2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 34 deletions.
11 changes: 8 additions & 3 deletions src/pages/dataElementGroupSets/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<FormValues>
Expand Down Expand Up @@ -129,7 +132,9 @@ export const Component = () => {
<Form
validateOnBlur
onSubmit={onSubmit}
validate={validate}
validate={(values: FormValues) => {
return validate(dataElementGroupSetSchema, values)
}}
initialValues={initialValues}
>
{({ handleSubmit, submitting, submitError }) => (
Expand Down
11 changes: 8 additions & 3 deletions src/pages/dataElementGroupSets/New.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -52,7 +55,9 @@ export function Component() {
<Form
validateOnBlur
onSubmit={onSubmit}
validate={validate}
validate={(values: FormValues) => {
return validate(dataElementGroupSetSchema, values)
}}
initialValues={initialValues}
>
{({ handleSubmit, submitting, submitError }) => (
Expand Down
2 changes: 1 addition & 1 deletion src/pages/dataElementGroupSets/form/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { DataElementGroupSetFormFields } from './DataElementGroupSetFormFields'
export { dataElementGroupSetSchema } from './dataElementGroupSetSchema'
export type { FormValues } from './types'
export { validate } from './validate'
27 changes: 0 additions & 27 deletions src/pages/dataElementGroupSets/form/validate.ts

This file was deleted.

0 comments on commit 0463ad2

Please sign in to comment.