@@ -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 */
2525export 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) =>
103106const 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