-
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.
refactor: dry up form validation & zod segmentsToPath
- Loading branch information
Showing
13 changed files
with
60 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { composeAsyncValidators } from './composeAsyncValidators' | ||
export type { FormFieldValidator } from './composeAsyncValidators' | ||
export { required } from './validators' | ||
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,22 @@ | ||
import { setIn } from 'final-form' | ||
import { z } from 'zod' | ||
import { segmentsToPath } from '../zod' | ||
|
||
export function validate<FormValues>( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
zodSchema: z.ZodType<any, any, any>, | ||
values: FormValues | ||
) { | ||
const zodResult = zodSchema.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 | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { segmentsToPath } from './segmentsToPath' |
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,8 @@ | ||
// @TODO: Figure out if there's a utility for this? I couldn't find one | ||
export function segmentsToPath(segments: Array<string | number>) { | ||
return segments.reduce((path, segment) => { | ||
return typeof segment === 'number' | ||
? `${path}[${segment}]` | ||
: `${path}.${segment}` | ||
}) as string | ||
} |
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,3 +1,3 @@ | ||
export { DataElementGroupFormFields } from './DataElementGroupFormFields' | ||
export { dataElementGroupSchema } from './dataElementGroupSchema' | ||
export type { FormValues } from './types' | ||
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
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,3 +1,3 @@ | ||
export { DataElementFormFields } from './DataElementFormFields' | ||
export { dataElementSchema } from './dataElementSchema' | ||
export type { FormValues } from './types' | ||
export { validate } from './validate' |
This file was deleted.
Oops, something went wrong.