-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose all field errors as an array
- Loading branch information
1 parent
7c0901d
commit 1e4cb50
Showing
8 changed files
with
52 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { FormValue, FormState, FieldPath } from '../types'; | ||
import { isTraversable } from '../utils'; | ||
|
||
/** | ||
* Get all errors for a field. | ||
*/ | ||
export function getFieldErrors< | ||
V extends FormValue, | ||
P extends FieldPath<V>, | ||
>(formState: FormState<V>, fieldPath: P): string[] { | ||
const { value: formValue } = formState; | ||
const { errorFieldPaths } = formState.__internal.fieldStates; | ||
|
||
if (errorFieldPaths.has(fieldPath)) { | ||
return errorFieldPaths.get(fieldPath) ?? []; | ||
} | ||
|
||
// No need to check descendants if the value is not an object or array. | ||
if (isTraversable(formValue)) { | ||
for (const [errorFieldPath, errors] of errorFieldPaths) { | ||
if (errorFieldPath.startsWith(fieldPath)) { | ||
return errors; | ||
} | ||
} | ||
} | ||
|
||
return []; | ||
} |
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,5 +1,5 @@ | ||
export interface FieldStates { | ||
dirtyFieldPaths: Set<string>; | ||
touchedFieldPaths: Set<string>; | ||
errorFieldPaths: Map<string, string>; | ||
errorFieldPaths: Map<string, string[]>; | ||
} |