Skip to content

Commit 13d7b8e

Browse files
authored
Merge pull request #11150 from Gabriel-Malenowitch/fix/getSimpleValidationResolver-types
Fix: add generic type parameters to getSimpleValidationResolver
2 parents b86e2ef + d5e562b commit 13d7b8e

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

packages/ra-core/src/form/validation/getSimpleValidationResolver.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { FieldValues } from 'react-hook-form';
55
* to a validation resolver compatible with react-hook-form.
66
*
77
* @example
8-
* const validate = (values: any) => {
8+
* const validate = (values: { username: string }) => {
99
* if (values.username == null || values.username.trim() === '') {
1010
* return { username: 'Required' };
1111
* }
12-
* }
12+
* };
1313
*
1414
* const validationResolver = getSimpleValidationResolver(validate);
1515
*
@@ -23,7 +23,10 @@ import { FieldValues } from 'react-hook-form';
2323
* );
2424
*/
2525
export const getSimpleValidationResolver =
26-
(validate: ValidateForm) => async (data: FieldValues) => {
26+
<TFieldValues extends FieldValues = FieldValues>(
27+
validate: ValidateForm<TFieldValues>
28+
) =>
29+
async (data: TFieldValues) => {
2730
const errors = await validate(data);
2831

2932
// If there are no errors, early return the form values
@@ -103,6 +106,9 @@ const isRaTranslationObj = (obj: object) =>
103106
const isEmptyObject = (obj: object) =>
104107
obj == null || Object.getOwnPropertyNames(obj).length === 0;
105108

106-
export type ValidateForm = (
107-
data: FieldValues
108-
) => FieldValues | Promise<FieldValues>;
109+
export type ValidateForm<TFieldValues extends FieldValues = FieldValues> = (
110+
data: TFieldValues
111+
) =>
112+
| Partial<Record<keyof TFieldValues, any>>
113+
| undefined
114+
| Promise<Partial<Record<keyof TFieldValues, any>> | undefined>;

0 commit comments

Comments
 (0)