Skip to content

Stricter typing for validationSchema #4928

@shunjilin

Description

@shunjilin

Description:

I’m not sure if this issue has already been addressed or if it’s a limitation, so I apologize if this is redundant.

Currently, the validationSchema accepts any as its type, which allows any value to be assigned. In the example below:

type Form = {
  a: string;
};

useForm<{ a: string }>({
  validationSchema: "", // accepts any type
});

Solution

Would it be possible to narrow down the type of validationSchema to at least one of the following:

  • A Yup object with optional keys from Form that can be assigned to any, or
  • A record with optional keys from Form that can be assigned to any (since they accept validation rules or functions).

For reference, see docs.

Context:

I’m raising this because we had a case where we changed the Form type, but forgot to update the validationSchema. This led to an invalid property being set as required, causing the form to fail submission. Having stronger type safety for validationSchema would help prevent such issues.

Alternatives

A straightforward solution to this would be to use the typedSchema from yup or zod, but since we are already using another validation library effect-ts, we would like to avoid introducing an additional dependency.

I’d be happy to contribute if this approach seems suitable. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions