-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dashboard): Add handleValidationIssues util (#6755)
- Loading branch information
Showing
2 changed files
with
28 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { FieldValues, UseFormSetError } from 'react-hook-form'; | ||
|
||
type ValidationIssues = Record< | ||
string, | ||
{ | ||
issueType: string; | ||
message: string; | ||
variableName: string; | ||
}[] | ||
>; | ||
|
||
type HandleValidationIssuesProps<T extends FieldValues> = { | ||
fields: T; | ||
issues: ValidationIssues; | ||
setError: UseFormSetError<T>; | ||
}; | ||
export const handleValidationIssues = <T extends FieldValues>(props: HandleValidationIssuesProps<T>) => { | ||
const { fields, issues, setError } = props; | ||
|
||
(Object.keys(issues) as Array<keyof typeof issues>).map((issueKey) => { | ||
if (issueKey in fields) { | ||
setError(issueKey as any, { message: issues[issueKey][0]?.message || 'Unknown error' }); | ||
} else { | ||
console.log(`Issue for ${issueKey} found and does not correspond to a field`); | ||
} | ||
}); | ||
}; |