-
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.
- Loading branch information
Showing
11 changed files
with
87 additions
and
21 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 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 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { DataElementFormFields } from './DataElementFormFields' | ||
export { computeInitialValues } from './computeInitialValues' | ||
export { useCustomAttributesQuery } from './useCustomAttributesQuery' | ||
export { FormValues } from './types' | ||
export type { FormValues } from './types' | ||
export { validate } from './validate' |
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,28 @@ | ||
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) { | ||
console.log('> no validation issues'); | ||
return undefined | ||
} | ||
|
||
const allFormErrors = zodResult.error.issues.reduce((formErrors, error) => { | ||
const errorPath = segmentsToPath(error.path) | ||
return setIn(formErrors, errorPath, error.message) | ||
}, {}) | ||
|
||
return allFormErrors | ||
} |