Skip to content

[TypeScript] Add generic type parameters to getSimpleValidationResolver#11150

Merged
slax57 merged 2 commits intomarmelab:masterfrom
Gabriel-Malenowitch:fix/getSimpleValidationResolver-types
Feb 9, 2026
Merged

[TypeScript] Add generic type parameters to getSimpleValidationResolver#11150
slax57 merged 2 commits intomarmelab:masterfrom
Gabriel-Malenowitch:fix/getSimpleValidationResolver-types

Conversation

@Gabriel-Malenowitch
Copy link
Contributor

@Gabriel-Malenowitch Gabriel-Malenowitch commented Feb 4, 2026

Problem

Fix #11149

The getSimpleValidationResolver function does not currently accept generic type parameters. This prevents developers from strictly typing their form values and validation functions.

When using getSimpleValidationResolver with a specific TypeScript interface, it defaults to FieldValues, causing type mismatches and TypeScript errors when passing the generated resolver to useForm or React Admin form components (like <SimpleForm>).

Solution

I have updated getSimpleValidationResolver and the ValidateForm type definition to accept a generic type parameter <TFormValues extends FieldValues = FieldValues>.

This change allows consumers to specify the exact shape of their form values when creating a validation resolver, ensuring type safety and compatibility with react-hook-form.

How To Test

You can verify the fix by creating a strongly typed form and validation function. The following code should no longer produce TypeScript errors:

import { getSimpleValidationResolver, SimpleForm, TextInput, NumberInput } from 'react-admin';

// 1. Define the interface
interface FormValues {
    name: string;
    email: string;
    age: number;
}

// 2. Define the validator
const validate = (values: FormValues) => {
    const errors: Partial<Record<keyof FormValues, string>> = {};
    if (!values.name) errors.name = 'Required';
    if (!values.email) errors.email = 'Required';
    return errors;
};

// 3. Create the resolver with the generic type
const resolver = getSimpleValidationResolver<FormValues>(validate);

// 4. Use it in a SimpleForm (should not throw TS errors)
export const MyForm = () => (
    <SimpleForm<FormValues> resolver={resolver}>
        <TextInput source="name" />
        <TextInput source="email" />
        <NumberInput source="age" />
    </SimpleForm>
);

@Gabriel-Malenowitch
Copy link
Contributor Author

Issue Ref #11149

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds generic type parameters to the getSimpleValidationResolver function and ValidateForm type to enable better TypeScript support for strongly-typed form validation. This allows developers to specify the exact shape of their form values when creating validation resolvers, ensuring type safety and compatibility with react-hook-form.

Changes:

  • Added generic type parameters <TFieldValues, TErrors> to getSimpleValidationResolver
  • Updated ValidateForm type to accept generic TFieldValues parameter
  • Updated JSDoc example to demonstrate typed validation function usage
  • Added type assertions for return values to support the generic types

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@slax57 slax57 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I tested your example and I still get a TS error.

Image

Can you double check?

Comment on lines 51 to 52
values: {} as TFieldValues,
errors: transformedErrors as TErrors,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you are missing some return paths (e.g. line 34 or 46)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Truly, thanks!
Now it is solved:
image

Commit: 19d910a

export const N = () => {
    // 1. Define the interface
    interface FormValues {
        name: string;
        email: string;
        age: number;
    }

    // 2. Define the validator
    const validateA = (values: FormValues) => {
        const errors: Partial<Record<keyof FormValues, string>> = {};
        if (!values.name) errors.name = 'Required';
        if (!values.email) errors.email = 'Required';
        return errors;
    };

    // 3. Create the resolver with the generic type
    const resolver = getSimpleValidationResolver<FormValues>(validateA);
    return null;
};

@Gabriel-Malenowitch Gabriel-Malenowitch force-pushed the fix/getSimpleValidationResolver-types branch from 569ec96 to 19d910a Compare February 6, 2026 17:31
@slax57 slax57 added this to the 5.14.2 milestone Feb 9, 2026
@slax57 slax57 merged commit 13d7b8e into marmelab:master Feb 9, 2026
7 of 9 checks passed
@slax57 slax57 changed the title Fix: add generic type parameters to getSimpleValidationResolver [TypeScript] Add generic type parameters to getSimpleValidationResolver Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Add generic type parameters to getSimpleValidationResolver for better TypeScript support

3 participants