Skip to content

Commit

Permalink
fix(de zod schema): add .optional() to optional nested value fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed Nov 23, 2023
1 parent 911879e commit 561fa30
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 74 deletions.
3 changes: 1 addition & 2 deletions src/pages/dataElements/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Attribute, DataElement } from '../../types/generated'
import { createJsonPatchOperations } from './edit/'
import classes from './Edit.module.css'
import { useCustomAttributesQuery } from './fields'
import { DataElementFormFields, useValidate } from './form'
import { DataElementFormFields, validate } from './form'
import type { FormValues } from './form'

type FinalFormFormApi = FormApi<FormValues>
Expand Down Expand Up @@ -144,7 +144,6 @@ export const Component = () => {
const dataElementQuery = useDataElementQuery(dataElementId)
const customAttributesQuery = useCustomAttributesQuery()
const patchDirtyFields = usePatchDirtyFields()
const validate = useValidate()

async function onSubmit(values: FormValues, form: FinalFormFormApi) {
const errors = await patchDirtyFields({
Expand Down
3 changes: 1 addition & 2 deletions src/pages/dataElements/New.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { SCHEMA_SECTIONS, getSectionPath, useSchemas } from '../../lib'
import { Attribute } from '../../types/generated'
import { useCustomAttributesQuery } from './fields'
import { DataElementFormFields, useValidate } from './form'
import { DataElementFormFields, validate } from './form'
import type { FormValues } from './form'
import classes from './New.module.css'

Expand Down Expand Up @@ -98,7 +98,6 @@ function formatFormValues({ values }: { values: FormValues }) {
}

export const Component = () => {
const validate = useValidate()
const dataEngine = useDataEngine()
const navigate = useNavigate()
const customAttributesQuery = useCustomAttributesQuery()
Expand Down
34 changes: 0 additions & 34 deletions src/pages/dataElements/form/createDataElementSchema.ts

This file was deleted.

33 changes: 33 additions & 0 deletions src/pages/dataElements/form/dataElementSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { z } from 'zod'
import { DataElement } from '../../../types/generated'

export const dataElementSchema = z
.object({
name: z.string().trim(),
shortName: z.string().trim(),
code: z.string().trim(),
description: z.string().trim(),
formName: z.string().trim(),
url: z.string().trim(),
fieldMask: z.string().trim(),
style: z.object({
color: z.string().optional(),
icon: z.string().optional(),
}),
domainType: z.union([z.literal('AGGREGATE'), z.literal('TRACKER')]),
valueType: z.nativeEnum(DataElement.valueType),
aggregationType: z.nativeEnum(DataElement.aggregationType),
optionSet: z.object({ id: z.string() }),
commentOptionSet: z.object({ id: z.string() }),
legendSets: z.array(z.object({ id: z.string() })),
aggregationLevels: z.array(z.number()),
attributeValues: z.array(
z.object({
value: z.string().optional(),
attribute: z.object({
id: z.string(),
}),
})
),
})
.partial()
2 changes: 1 addition & 1 deletion src/pages/dataElements/form/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { DataElementFormFields } from './DataElementFormFields'
export type { FormValues } from './types'
export { useValidate } from './useValidate'
export { validate } from './validate'
35 changes: 0 additions & 35 deletions src/pages/dataElements/form/useValidate.ts

This file was deleted.

27 changes: 27 additions & 0 deletions src/pages/dataElements/form/validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { setIn } from 'final-form'
import { dataElementSchema } from './dataElementSchema'
import type { FormValues } from './types'

// @TODO: Figure out if there's a utility for this? I couldn't find one
function segmentsToPath(segments: Array<string | number>) {
return segments.reduce((path, segment) => {
return typeof segment === 'number'
? `${path}[${segment}]`
: `${path}.${segment}`
}) as string
}

export function validate(values: FormValues) {
const zodResult = dataElementSchema.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
}

0 comments on commit 561fa30

Please sign in to comment.