-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(de zod schema): add
.optional()
to optional nested value fields
- Loading branch information
Showing
7 changed files
with
63 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |