Skip to content

Commit 569ec96

Browse files
Fix: add generic type parameters to getSimpleValidationResolver
1 parent bd7e28f commit 569ec96

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

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

Lines changed: 11 additions & 8 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, TErrors = any>(
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
@@ -45,8 +48,8 @@ export const getSimpleValidationResolver =
4548

4649
// Else return the errors and no values
4750
return {
48-
values: {},
49-
errors: transformedErrors,
51+
values: {} as TFieldValues,
52+
errors: transformedErrors as TErrors,
5053
};
5154
};
5255

@@ -103,6 +106,6 @@ 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+
) => TFieldValues | Promise<TFieldValues>;

0 commit comments

Comments
 (0)